Skip to content

Commit

Permalink
Add implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Jan 9, 2022
1 parent 549f309 commit 564472d
Show file tree
Hide file tree
Showing 33 changed files with 126,352 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: check

on:
pull_request:
push:
branches:
- main

jobs:
setup:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2

- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: "16.x"

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: npm install
- run: npm run tsc
- run: npm run test
- run: npm run build
35 changes: 35 additions & 0 deletions .github/workflows/demo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: demo

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2

- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: "16.x"

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: npm install
- run: npm run storybook:build

- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./storybook-static
publish_branch: demo
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
node_modules
7 changes: 7 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
stories: [
"../stories/**/*.stories.mdx",
"../stories/**/*.stories.@(js|jsx|ts|tsx)",
],
addons: ["@storybook/addon-links", "@storybook/addon-essentials"],
};
3 changes: 3 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
};
69 changes: 68 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,68 @@
# remark-docx
# remark-docx

![npm](https://img.shields.io/npm/v/remark-docx) ![check](https://github.com/inokawa/remark-docx/workflows/check/badge.svg) ![demo](https://github.com/inokawa/remark-docx/workflows/demo/badge.svg)

[remark](https://github.com/remarkjs/remark) plugin to transform markdown to docx.

### 🚧 WIP 🚧

The goal is to support all nodes in [mdast](https://github.com/syntax-tree/mdast) syntax tree, but currently transformation and stylings may not be well.

If you have some feature requests or improvements, please create a [issue](https://github.com/inokawa/remark-docx/issues) or [PR](https://github.com/inokawa/remark-docx/pulls).

## Demo

https://inokawa.github.io/remark-docx/

## Install

```sh
npm install remark-docx
```

## Usage

### Browser

```javascript
import { unified } from "unified";
import markdown from "remark-parse";
import docx, { Packer } from "remark-docx";
import { saveAs } from "file-saver";

const processor = unified().use(markdown).use(docx);

const text = "# hello world";

(async () => {
const doc = await processor.process(text);
const blob = await Packer.toBlob(doc.result);
saveAs(blob, "example.docx");
})();
```

### Node.js

```javascript
import { unified } from "unified";
import markdown from "remark-parse";
import docx, { Packer } from "remark-docx";
import * as fs from "fs";

const processor = unified().use(markdown).use(docx);

const text = "# hello world";

(async () => {
const doc = await processor.process(text);
const buffer = await Packer.toBuffer(doc.result);
fs.writeFileSync("example.docx", buffer);
})();
```

## Options

| Key | Default | Type | Description |
| ------------- | --------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| docProperties | undefined | object | Override properties of document. |
| imageResolver | undefined | ImageResolver | **You must set** if your markdown includes images. See example for [Node.js](https://github.com/inokawa/remark-docx/blob/main/src/index.spec.ts) and [browser](https://github.com/inokawa/remark-docx/blob/main/stories/playground.stories.tsx). |
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
95 changes: 95 additions & 0 deletions fixtures/article.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: "This is frontmatter"
date: 2020-04-30 12:34
categories: [JavaScript, React]
---

# heading 1

## heading 2

### heading 3

#### heading 4

##### heading 5

###### heading 6

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

aaaaaaaa**bold**_emphasis_~~delete~~`inline code`

- list

- list
- list
- list
- list

- [ ] not checked
- [x] checked

1. ordered list
1. ordered list
1. ordered list
1. aaa
1. aaa
1. eeeee
1. eeeee
1. aaa
1. ordered list

This is [link to GitHub.com](https://github.com/).

This is ![image](https://github.githubassets.com/images/modules/logos_page/Octocat.png).

> quote
> quote
> quote
> quote
> quote
>
> > quoted quote
| Left align | Right align | Center align |
| :--------- | ----------: | :----------: |
| This | This | This |
| column | column | column |
| will | will | will |
| be | be | be |
| left | right | center |
| aligned | aligned | aligned |

<div style="color:red;">
This is <u>HTML</u>
</div>

---

---

```javascript
function $initHighlight(block, cls) {
try {
if (cls.search(/\bno\-highlight\b/) != -1)
return process(block, true, 0x0F) +
` class="${cls}"`;
} catch (e) {
/* handle exception */
}
for (var i = 0 / 2; i < classes.length; i++) {
if (checkCondition(classes[i]) === undefined)
console.log('undefined');
}

return (
<div>
<web-component>{block}</web-component>
</div>
)
}

export $initHighlight;
```
3 changes: 3 additions & 0 deletions fixtures/node-break.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

***
62 changes: 62 additions & 0 deletions fixtures/node-code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
```css metadata="12345"
@font-face {
font-family: Chunkfive;
src: url("Chunkfive.otf");
}

body,
.usertext {
color: #f0f0f0;
background: #600;
font-family: Chunkfive, sans;
--heading-1: 30px/32px Helvetica, sans-serif;
}

@import url(print.css);
@media print {
a[href^="http"]::after {
content: attr(href);
}
}
```

```javascript
function $initHighlight(block, cls) {
try {
if (cls.search(/\bno\-highlight\b/) != -1)
return process(block, true, 0x0F) +
` class="${cls}"`;
} catch (e) {
/* handle exception */
}
for (var i = 0 / 2; i < classes.length; i++) {
if (checkCondition(classes[i]) === undefined)
console.log('undefined');
}

return (
<div>
<web-component>{block}</web-component>
</div>
)
}

export $initHighlight;
```

```typescript
class MyClass {
public static myValue: string;
constructor(init: string) {
this.myValue = init;
}
}
import fs = require("fs");
module MyModule {
export interface MyInterface extends Other {
myProperty: any;
}
}
declare magicNumber number;
myArray.forEach(() => { }); // fat arrow syntax
```
3 changes: 3 additions & 0 deletions fixtures/node-footnotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is footnote[^1]. And this is also ^[inline footnote].

[^1]: Here is the footnote.
8 changes: 8 additions & 0 deletions fixtures/node-frontmatter-toml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+++
title = "This is toml"
date = "2020-04-30 22:34"
+++

# Other markdown

blablabla
9 changes: 9 additions & 0 deletions fixtures/node-frontmatter-yaml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: "This is yaml"
date: 2020-04-30 22:34
categories: [JavaScript, React]
---

# Other markdown

blablabla
23 changes: 23 additions & 0 deletions fixtures/node-heading.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Alpha

aaaa

## Bravo

bbbb

### Charlie

cccc

## Delta

dddd

### E

#### F

##### G

###### H
6 changes: 6 additions & 0 deletions fixtures/node-math.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following
equation.

$$
L = \frac{1}{2} \rho v^2 S C_L
$$
9 changes: 9 additions & 0 deletions fixtures/node-ml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div>
<span>This is <b>html</b></span>
</div>

<!--comment-->

<mailto:foobarbaz>

aaaaaa
4 changes: 4 additions & 0 deletions fixtures/node-paragraph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Loading

0 comments on commit 564472d

Please sign in to comment.