remark-docx 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +68 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +485 -0
- package/lib/index.mjs +459 -0
- package/lib/models/mdast.d.ts +21 -0
- package/lib/plugin.d.ts +5 -0
- package/lib/transformer.d.ts +17 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 inokawa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# remark-docx
|
|
2
|
+
|
|
3
|
+
  
|
|
4
|
+
|
|
5
|
+
[remark](https://github.com/remarkjs/remark) plugin to transform markdown to docx.
|
|
6
|
+
|
|
7
|
+
### 🚧 WIP 🚧
|
|
8
|
+
|
|
9
|
+
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.
|
|
10
|
+
|
|
11
|
+
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).
|
|
12
|
+
|
|
13
|
+
## Demo
|
|
14
|
+
|
|
15
|
+
https://inokawa.github.io/remark-docx/
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install remark-docx
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Browser
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
import { unified } from "unified";
|
|
29
|
+
import markdown from "remark-parse";
|
|
30
|
+
import docx, { Packer } from "remark-docx";
|
|
31
|
+
import { saveAs } from "file-saver";
|
|
32
|
+
|
|
33
|
+
const processor = unified().use(markdown).use(docx);
|
|
34
|
+
|
|
35
|
+
const text = "# hello world";
|
|
36
|
+
|
|
37
|
+
(async () => {
|
|
38
|
+
const doc = await processor.process(text);
|
|
39
|
+
const blob = await Packer.toBlob(doc.result);
|
|
40
|
+
saveAs(blob, "example.docx");
|
|
41
|
+
})();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Node.js
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
import { unified } from "unified";
|
|
48
|
+
import markdown from "remark-parse";
|
|
49
|
+
import docx, { Packer } from "remark-docx";
|
|
50
|
+
import * as fs from "fs";
|
|
51
|
+
|
|
52
|
+
const processor = unified().use(markdown).use(docx);
|
|
53
|
+
|
|
54
|
+
const text = "# hello world";
|
|
55
|
+
|
|
56
|
+
(async () => {
|
|
57
|
+
const doc = await processor.process(text);
|
|
58
|
+
const buffer = await Packer.toBuffer(doc.result);
|
|
59
|
+
fs.writeFileSync("example.docx", buffer);
|
|
60
|
+
})();
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Options
|
|
64
|
+
|
|
65
|
+
| Key | Default | Type | Description |
|
|
66
|
+
| ------------- | --------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
67
|
+
| docProperties | undefined | object | Override properties of document. |
|
|
68
|
+
| 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). |
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var unistUtilVisit = require('unist-util-visit');
|
|
6
|
+
var docx = require('docx');
|
|
7
|
+
|
|
8
|
+
function _interopNamespace(e) {
|
|
9
|
+
if (e && e.__esModule) return e;
|
|
10
|
+
var n = Object.create(null);
|
|
11
|
+
if (e) {
|
|
12
|
+
Object.keys(e).forEach(function (k) {
|
|
13
|
+
if (k !== 'default') {
|
|
14
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return e[k]; }
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
n["default"] = e;
|
|
23
|
+
return Object.freeze(n);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var docx__namespace = /*#__PURE__*/_interopNamespace(docx);
|
|
27
|
+
|
|
28
|
+
/*! *****************************************************************************
|
|
29
|
+
Copyright (c) Microsoft Corporation.
|
|
30
|
+
|
|
31
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
32
|
+
purpose with or without fee is hereby granted.
|
|
33
|
+
|
|
34
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
35
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
36
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
37
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
38
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
39
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
40
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
41
|
+
***************************************************************************** */
|
|
42
|
+
|
|
43
|
+
var __assign = function() {
|
|
44
|
+
__assign = Object.assign || function __assign(t) {
|
|
45
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
46
|
+
s = arguments[i];
|
|
47
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
48
|
+
}
|
|
49
|
+
return t;
|
|
50
|
+
};
|
|
51
|
+
return __assign.apply(this, arguments);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
55
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
56
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
57
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
58
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
59
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
60
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function __generator(thisArg, body) {
|
|
65
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
66
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
67
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
68
|
+
function step(op) {
|
|
69
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
70
|
+
while (_) try {
|
|
71
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
72
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
73
|
+
switch (op[0]) {
|
|
74
|
+
case 0: case 1: t = op; break;
|
|
75
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
76
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
77
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
78
|
+
default:
|
|
79
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
80
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
81
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
82
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
83
|
+
if (t[2]) _.ops.pop();
|
|
84
|
+
_.trys.pop(); continue;
|
|
85
|
+
}
|
|
86
|
+
op = body.call(thisArg, _);
|
|
87
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
88
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
var ORDERED_LIST_REF = "ordered";
|
|
93
|
+
var INDENT = 0.5;
|
|
94
|
+
var DEFAULT_NUMBERINGS = [
|
|
95
|
+
{
|
|
96
|
+
level: 0,
|
|
97
|
+
format: docx__namespace.LevelFormat.DECIMAL,
|
|
98
|
+
text: "%1.",
|
|
99
|
+
alignment: docx__namespace.AlignmentType.START,
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
level: 1,
|
|
103
|
+
format: docx__namespace.LevelFormat.DECIMAL,
|
|
104
|
+
text: "%2.",
|
|
105
|
+
alignment: docx__namespace.AlignmentType.START,
|
|
106
|
+
style: {
|
|
107
|
+
paragraph: {
|
|
108
|
+
indent: { start: docx.convertInchesToTwip(INDENT * 1) },
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
level: 2,
|
|
114
|
+
format: docx__namespace.LevelFormat.DECIMAL,
|
|
115
|
+
text: "%3.",
|
|
116
|
+
alignment: docx__namespace.AlignmentType.START,
|
|
117
|
+
style: {
|
|
118
|
+
paragraph: {
|
|
119
|
+
indent: { start: docx.convertInchesToTwip(INDENT * 2) },
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
level: 3,
|
|
125
|
+
format: docx__namespace.LevelFormat.DECIMAL,
|
|
126
|
+
text: "%4.",
|
|
127
|
+
alignment: docx__namespace.AlignmentType.START,
|
|
128
|
+
style: {
|
|
129
|
+
paragraph: {
|
|
130
|
+
indent: { start: docx.convertInchesToTwip(INDENT * 3) },
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
level: 4,
|
|
136
|
+
format: docx__namespace.LevelFormat.DECIMAL,
|
|
137
|
+
text: "%5.",
|
|
138
|
+
alignment: docx__namespace.AlignmentType.START,
|
|
139
|
+
style: {
|
|
140
|
+
paragraph: {
|
|
141
|
+
indent: { start: docx.convertInchesToTwip(INDENT * 4) },
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
level: 5,
|
|
147
|
+
format: docx__namespace.LevelFormat.DECIMAL,
|
|
148
|
+
text: "%6.",
|
|
149
|
+
alignment: docx__namespace.AlignmentType.START,
|
|
150
|
+
style: {
|
|
151
|
+
paragraph: {
|
|
152
|
+
indent: { start: docx.convertInchesToTwip(INDENT * 5) },
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
];
|
|
157
|
+
var error = function (message) {
|
|
158
|
+
throw new Error(message);
|
|
159
|
+
};
|
|
160
|
+
function mdastToDocx(node, opts, images) {
|
|
161
|
+
return buildDocxRoot(node, opts, images);
|
|
162
|
+
}
|
|
163
|
+
function buildDocxRoot(root, opts, images) {
|
|
164
|
+
var ctx = { deco: {}, images: images };
|
|
165
|
+
var nodes = convertNodes(root.children, ctx, opts);
|
|
166
|
+
return new docx__namespace.Document(__assign(__assign({}, opts.docProperties), { sections: [{ children: nodes }], numbering: {
|
|
167
|
+
config: [
|
|
168
|
+
{
|
|
169
|
+
reference: ORDERED_LIST_REF,
|
|
170
|
+
levels: DEFAULT_NUMBERINGS,
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
} }));
|
|
174
|
+
}
|
|
175
|
+
function convertNodes(nodes, ctx, opts) {
|
|
176
|
+
var _a;
|
|
177
|
+
var results = [];
|
|
178
|
+
for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
|
|
179
|
+
var node = nodes_1[_i];
|
|
180
|
+
switch (node.type) {
|
|
181
|
+
case "paragraph":
|
|
182
|
+
results.push(buildParagraph(node, ctx, opts));
|
|
183
|
+
break;
|
|
184
|
+
case "heading":
|
|
185
|
+
results.push(buildHeading(node, ctx, opts));
|
|
186
|
+
break;
|
|
187
|
+
case "thematicBreak":
|
|
188
|
+
results.push(buildThematicBreak());
|
|
189
|
+
break;
|
|
190
|
+
case "blockquote":
|
|
191
|
+
results.push.apply(results, buildBlockquote(node, ctx, opts));
|
|
192
|
+
break;
|
|
193
|
+
case "list":
|
|
194
|
+
results.push.apply(results, buildList(node, ctx, opts));
|
|
195
|
+
break;
|
|
196
|
+
case "listItem":
|
|
197
|
+
error("unreachable");
|
|
198
|
+
break;
|
|
199
|
+
case "table":
|
|
200
|
+
results.push(buildTable(node, ctx, opts));
|
|
201
|
+
break;
|
|
202
|
+
case "tableRow":
|
|
203
|
+
error("unreachable");
|
|
204
|
+
break;
|
|
205
|
+
case "tableCell":
|
|
206
|
+
error("unreachable");
|
|
207
|
+
break;
|
|
208
|
+
case "html":
|
|
209
|
+
results.push(buildHtml(node));
|
|
210
|
+
break;
|
|
211
|
+
case "code":
|
|
212
|
+
results.push(buildCode(node));
|
|
213
|
+
break;
|
|
214
|
+
case "yaml":
|
|
215
|
+
// FIXME: unimplemented
|
|
216
|
+
break;
|
|
217
|
+
case "toml":
|
|
218
|
+
// FIXME: unimplemented
|
|
219
|
+
break;
|
|
220
|
+
case "definition":
|
|
221
|
+
// FIXME: unimplemented
|
|
222
|
+
break;
|
|
223
|
+
case "footnoteDefinition":
|
|
224
|
+
// FIXME: unimplemented
|
|
225
|
+
break;
|
|
226
|
+
case "text":
|
|
227
|
+
results.push(buildText(node.value, ctx.deco));
|
|
228
|
+
break;
|
|
229
|
+
case "emphasis":
|
|
230
|
+
case "strong":
|
|
231
|
+
case "delete": {
|
|
232
|
+
var type = node.type, children = node.children;
|
|
233
|
+
results.push.apply(results, convertNodes(children, __assign(__assign({}, ctx), { deco: __assign(__assign({}, ctx.deco), (_a = {}, _a[type] = true, _a)) }), opts));
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
case "inlineCode":
|
|
237
|
+
// FIXME: transform to text for now
|
|
238
|
+
results.push(buildText(node.value, ctx.deco));
|
|
239
|
+
break;
|
|
240
|
+
case "break":
|
|
241
|
+
results.push(buildBreak());
|
|
242
|
+
break;
|
|
243
|
+
case "link":
|
|
244
|
+
results.push(buildLink(node, ctx, opts));
|
|
245
|
+
break;
|
|
246
|
+
case "image":
|
|
247
|
+
results.push(buildImage(node, ctx.images));
|
|
248
|
+
break;
|
|
249
|
+
case "linkReference":
|
|
250
|
+
// FIXME: unimplemented
|
|
251
|
+
break;
|
|
252
|
+
case "imageReference":
|
|
253
|
+
// FIXME: unimplemented
|
|
254
|
+
break;
|
|
255
|
+
case "footnote":
|
|
256
|
+
results.push(buildFootnote(node, ctx, opts));
|
|
257
|
+
break;
|
|
258
|
+
case "footnoteReference":
|
|
259
|
+
// FIXME: unimplemented
|
|
260
|
+
break;
|
|
261
|
+
case "math":
|
|
262
|
+
results.push(buildMath(node));
|
|
263
|
+
break;
|
|
264
|
+
case "inlineMath":
|
|
265
|
+
results.push(buildInlineMath(node));
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return results;
|
|
270
|
+
}
|
|
271
|
+
function buildParagraph(node, ctx, opts) {
|
|
272
|
+
node.type; var children = node.children;
|
|
273
|
+
var list = ctx.list;
|
|
274
|
+
return new docx__namespace.Paragraph(__assign({ children: convertNodes(children, ctx, opts) }, (list &&
|
|
275
|
+
(list.ordered
|
|
276
|
+
? {
|
|
277
|
+
numbering: {
|
|
278
|
+
reference: ORDERED_LIST_REF,
|
|
279
|
+
level: list.level,
|
|
280
|
+
},
|
|
281
|
+
}
|
|
282
|
+
: {
|
|
283
|
+
bullet: {
|
|
284
|
+
level: list.level,
|
|
285
|
+
},
|
|
286
|
+
}))));
|
|
287
|
+
}
|
|
288
|
+
function buildHeading(node, ctx, opts) {
|
|
289
|
+
node.type; var children = node.children, depth = node.depth;
|
|
290
|
+
var heading;
|
|
291
|
+
switch (depth) {
|
|
292
|
+
case 1:
|
|
293
|
+
heading = docx__namespace.HeadingLevel.TITLE;
|
|
294
|
+
break;
|
|
295
|
+
case 2:
|
|
296
|
+
heading = docx__namespace.HeadingLevel.HEADING_1;
|
|
297
|
+
break;
|
|
298
|
+
case 3:
|
|
299
|
+
heading = docx__namespace.HeadingLevel.HEADING_2;
|
|
300
|
+
break;
|
|
301
|
+
case 4:
|
|
302
|
+
heading = docx__namespace.HeadingLevel.HEADING_3;
|
|
303
|
+
break;
|
|
304
|
+
case 5:
|
|
305
|
+
heading = docx__namespace.HeadingLevel.HEADING_4;
|
|
306
|
+
break;
|
|
307
|
+
case 6:
|
|
308
|
+
heading = docx__namespace.HeadingLevel.HEADING_5;
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
return new docx__namespace.Paragraph({
|
|
312
|
+
heading: heading,
|
|
313
|
+
children: convertNodes(children, ctx, opts),
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
function buildThematicBreak(node) {
|
|
317
|
+
return new docx__namespace.Paragraph({
|
|
318
|
+
thematicBreak: true,
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
function buildBlockquote(node, ctx, opts) {
|
|
322
|
+
// FIXME: do nothing for now
|
|
323
|
+
return convertNodes(node.children, ctx, opts);
|
|
324
|
+
}
|
|
325
|
+
function buildList(node, ctx, opts) {
|
|
326
|
+
node.type; var children = node.children, ordered = node.ordered; node.start; node.spread;
|
|
327
|
+
var list = {
|
|
328
|
+
level: ctx.list ? ctx.list.level + 1 : 0,
|
|
329
|
+
ordered: !!ordered,
|
|
330
|
+
};
|
|
331
|
+
return children.reduce(function (acc, item) {
|
|
332
|
+
acc.push.apply(acc, buildListItem(item, __assign(__assign({}, ctx), { list: list }), opts));
|
|
333
|
+
return acc;
|
|
334
|
+
}, []);
|
|
335
|
+
}
|
|
336
|
+
function buildListItem(node, ctx, opts) {
|
|
337
|
+
node.type; var children = node.children; node.checked; node.spread;
|
|
338
|
+
return convertNodes(children, ctx, opts);
|
|
339
|
+
}
|
|
340
|
+
function buildTable(node, ctx, opts) {
|
|
341
|
+
node.type; var children = node.children, align = node.align;
|
|
342
|
+
var cellAligns = align === null || align === void 0 ? void 0 : align.map(function (a) {
|
|
343
|
+
switch (a) {
|
|
344
|
+
case "left":
|
|
345
|
+
return docx__namespace.AlignmentType.LEFT;
|
|
346
|
+
case "right":
|
|
347
|
+
return docx__namespace.AlignmentType.RIGHT;
|
|
348
|
+
case "center":
|
|
349
|
+
return docx__namespace.AlignmentType.CENTER;
|
|
350
|
+
default:
|
|
351
|
+
return docx__namespace.AlignmentType.LEFT;
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
return new docx__namespace.Table({
|
|
355
|
+
rows: children.map(function (r) {
|
|
356
|
+
return buildTableRow(r, ctx, opts, cellAligns);
|
|
357
|
+
}),
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
function buildTableRow(node, ctx, opts, cellAligns) {
|
|
361
|
+
node.type; var children = node.children;
|
|
362
|
+
return new docx__namespace.TableRow({
|
|
363
|
+
children: children.map(function (c, i) {
|
|
364
|
+
return buildTableCell(c, ctx, opts, cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i]);
|
|
365
|
+
}),
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
function buildTableCell(node, ctx, opts, align) {
|
|
369
|
+
node.type; var children = node.children;
|
|
370
|
+
return new docx__namespace.TableCell({
|
|
371
|
+
children: [
|
|
372
|
+
new docx__namespace.Paragraph({
|
|
373
|
+
alignment: align,
|
|
374
|
+
children: convertNodes(children, ctx, opts),
|
|
375
|
+
}),
|
|
376
|
+
],
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
function buildHtml(node) {
|
|
380
|
+
node.type; var value = node.value;
|
|
381
|
+
// FIXME: transform to text for now
|
|
382
|
+
return new docx__namespace.Paragraph({
|
|
383
|
+
children: [buildText(value, {})],
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
function buildCode(node) {
|
|
387
|
+
node.type; var value = node.value; node.lang; node.meta;
|
|
388
|
+
// FIXME: transform to text for now
|
|
389
|
+
return new docx__namespace.Paragraph({
|
|
390
|
+
children: [buildText(value, {})],
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
function buildMath(node) {
|
|
394
|
+
node.type; var value = node.value;
|
|
395
|
+
// FIXME: transform to text for now
|
|
396
|
+
return new docx__namespace.Paragraph({
|
|
397
|
+
children: [new docx__namespace.TextRun(value)],
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
function buildInlineMath(node) {
|
|
401
|
+
node.type; var value = node.value;
|
|
402
|
+
// FIXME: transform to text for now
|
|
403
|
+
return new docx__namespace.TextRun(value);
|
|
404
|
+
}
|
|
405
|
+
function buildText(text, deco) {
|
|
406
|
+
return new docx__namespace.TextRun({
|
|
407
|
+
text: text,
|
|
408
|
+
bold: deco.strong,
|
|
409
|
+
italics: deco.emphasis,
|
|
410
|
+
strike: deco.delete,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
function buildBreak(node) {
|
|
414
|
+
return new docx__namespace.TextRun({ text: "", break: 1 });
|
|
415
|
+
}
|
|
416
|
+
function buildLink(node, ctx, opts) {
|
|
417
|
+
node.type; var children = node.children, url = node.url; node.title;
|
|
418
|
+
return new docx__namespace.ExternalHyperlink({
|
|
419
|
+
link: url,
|
|
420
|
+
children: convertNodes(children, ctx, opts),
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
function buildImage(node, images) {
|
|
424
|
+
node.type; var url = node.url; node.title; node.alt;
|
|
425
|
+
if (!images[url]) {
|
|
426
|
+
return error("Fetch image was failed: ".concat(url));
|
|
427
|
+
}
|
|
428
|
+
var _a = images[url], image = _a.image, width = _a.width, height = _a.height;
|
|
429
|
+
return new docx__namespace.ImageRun({
|
|
430
|
+
data: image,
|
|
431
|
+
transformation: {
|
|
432
|
+
width: width,
|
|
433
|
+
height: height,
|
|
434
|
+
},
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
function buildFootnote(node, ctx, opts) {
|
|
438
|
+
node.type; var children = node.children;
|
|
439
|
+
// FIXME: transform to paragraph for now
|
|
440
|
+
return new docx__namespace.Paragraph({
|
|
441
|
+
children: convertNodes(children, ctx, opts),
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
var plugin = function (opts) {
|
|
446
|
+
var _this = this;
|
|
447
|
+
if (opts === void 0) { opts = {}; }
|
|
448
|
+
var images = {};
|
|
449
|
+
this.Compiler = function (node) {
|
|
450
|
+
return mdastToDocx(node, opts, images);
|
|
451
|
+
};
|
|
452
|
+
return function (node) { return __awaiter(_this, void 0, void 0, function () {
|
|
453
|
+
var imageResolver, imageList, imageDatas;
|
|
454
|
+
return __generator(this, function (_a) {
|
|
455
|
+
switch (_a.label) {
|
|
456
|
+
case 0:
|
|
457
|
+
imageResolver = opts.imageResolver;
|
|
458
|
+
if (!imageResolver) {
|
|
459
|
+
throw new Error("options.imageResolver is not defined.");
|
|
460
|
+
}
|
|
461
|
+
imageList = [];
|
|
462
|
+
unistUtilVisit.visit(node, "image", function (node) {
|
|
463
|
+
imageList.push(node);
|
|
464
|
+
});
|
|
465
|
+
return [4 /*yield*/, Promise.all(imageList.map(function (_a) {
|
|
466
|
+
var url = _a.url;
|
|
467
|
+
return imageResolver(url);
|
|
468
|
+
}))];
|
|
469
|
+
case 1:
|
|
470
|
+
imageDatas = _a.sent();
|
|
471
|
+
images = imageList.reduce(function (acc, img, i) {
|
|
472
|
+
acc[img.url] = imageDatas[i];
|
|
473
|
+
return acc;
|
|
474
|
+
}, {});
|
|
475
|
+
return [2 /*return*/, node];
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
}); };
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
Object.defineProperty(exports, 'Packer', {
|
|
482
|
+
enumerable: true,
|
|
483
|
+
get: function () { return docx.Packer; }
|
|
484
|
+
});
|
|
485
|
+
exports["default"] = plugin;
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
import { visit } from 'unist-util-visit';
|
|
2
|
+
import * as docx from 'docx';
|
|
3
|
+
import { convertInchesToTwip } from 'docx';
|
|
4
|
+
export { Packer } from 'docx';
|
|
5
|
+
|
|
6
|
+
/*! *****************************************************************************
|
|
7
|
+
Copyright (c) Microsoft Corporation.
|
|
8
|
+
|
|
9
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
10
|
+
purpose with or without fee is hereby granted.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
13
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
14
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
15
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
16
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
17
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
18
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
19
|
+
***************************************************************************** */
|
|
20
|
+
|
|
21
|
+
var __assign = function() {
|
|
22
|
+
__assign = Object.assign || function __assign(t) {
|
|
23
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
24
|
+
s = arguments[i];
|
|
25
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
26
|
+
}
|
|
27
|
+
return t;
|
|
28
|
+
};
|
|
29
|
+
return __assign.apply(this, arguments);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
33
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
34
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
35
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
36
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
37
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
38
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function __generator(thisArg, body) {
|
|
43
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
44
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
45
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
46
|
+
function step(op) {
|
|
47
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
48
|
+
while (_) try {
|
|
49
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
50
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
51
|
+
switch (op[0]) {
|
|
52
|
+
case 0: case 1: t = op; break;
|
|
53
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
54
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
55
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
56
|
+
default:
|
|
57
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
58
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
59
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
60
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
61
|
+
if (t[2]) _.ops.pop();
|
|
62
|
+
_.trys.pop(); continue;
|
|
63
|
+
}
|
|
64
|
+
op = body.call(thisArg, _);
|
|
65
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
66
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
var ORDERED_LIST_REF = "ordered";
|
|
71
|
+
var INDENT = 0.5;
|
|
72
|
+
var DEFAULT_NUMBERINGS = [
|
|
73
|
+
{
|
|
74
|
+
level: 0,
|
|
75
|
+
format: docx.LevelFormat.DECIMAL,
|
|
76
|
+
text: "%1.",
|
|
77
|
+
alignment: docx.AlignmentType.START,
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
level: 1,
|
|
81
|
+
format: docx.LevelFormat.DECIMAL,
|
|
82
|
+
text: "%2.",
|
|
83
|
+
alignment: docx.AlignmentType.START,
|
|
84
|
+
style: {
|
|
85
|
+
paragraph: {
|
|
86
|
+
indent: { start: convertInchesToTwip(INDENT * 1) },
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
level: 2,
|
|
92
|
+
format: docx.LevelFormat.DECIMAL,
|
|
93
|
+
text: "%3.",
|
|
94
|
+
alignment: docx.AlignmentType.START,
|
|
95
|
+
style: {
|
|
96
|
+
paragraph: {
|
|
97
|
+
indent: { start: convertInchesToTwip(INDENT * 2) },
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
level: 3,
|
|
103
|
+
format: docx.LevelFormat.DECIMAL,
|
|
104
|
+
text: "%4.",
|
|
105
|
+
alignment: docx.AlignmentType.START,
|
|
106
|
+
style: {
|
|
107
|
+
paragraph: {
|
|
108
|
+
indent: { start: convertInchesToTwip(INDENT * 3) },
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
level: 4,
|
|
114
|
+
format: docx.LevelFormat.DECIMAL,
|
|
115
|
+
text: "%5.",
|
|
116
|
+
alignment: docx.AlignmentType.START,
|
|
117
|
+
style: {
|
|
118
|
+
paragraph: {
|
|
119
|
+
indent: { start: convertInchesToTwip(INDENT * 4) },
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
level: 5,
|
|
125
|
+
format: docx.LevelFormat.DECIMAL,
|
|
126
|
+
text: "%6.",
|
|
127
|
+
alignment: docx.AlignmentType.START,
|
|
128
|
+
style: {
|
|
129
|
+
paragraph: {
|
|
130
|
+
indent: { start: convertInchesToTwip(INDENT * 5) },
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
];
|
|
135
|
+
var error = function (message) {
|
|
136
|
+
throw new Error(message);
|
|
137
|
+
};
|
|
138
|
+
function mdastToDocx(node, opts, images) {
|
|
139
|
+
return buildDocxRoot(node, opts, images);
|
|
140
|
+
}
|
|
141
|
+
function buildDocxRoot(root, opts, images) {
|
|
142
|
+
var ctx = { deco: {}, images: images };
|
|
143
|
+
var nodes = convertNodes(root.children, ctx, opts);
|
|
144
|
+
return new docx.Document(__assign(__assign({}, opts.docProperties), { sections: [{ children: nodes }], numbering: {
|
|
145
|
+
config: [
|
|
146
|
+
{
|
|
147
|
+
reference: ORDERED_LIST_REF,
|
|
148
|
+
levels: DEFAULT_NUMBERINGS,
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
} }));
|
|
152
|
+
}
|
|
153
|
+
function convertNodes(nodes, ctx, opts) {
|
|
154
|
+
var _a;
|
|
155
|
+
var results = [];
|
|
156
|
+
for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
|
|
157
|
+
var node = nodes_1[_i];
|
|
158
|
+
switch (node.type) {
|
|
159
|
+
case "paragraph":
|
|
160
|
+
results.push(buildParagraph(node, ctx, opts));
|
|
161
|
+
break;
|
|
162
|
+
case "heading":
|
|
163
|
+
results.push(buildHeading(node, ctx, opts));
|
|
164
|
+
break;
|
|
165
|
+
case "thematicBreak":
|
|
166
|
+
results.push(buildThematicBreak());
|
|
167
|
+
break;
|
|
168
|
+
case "blockquote":
|
|
169
|
+
results.push.apply(results, buildBlockquote(node, ctx, opts));
|
|
170
|
+
break;
|
|
171
|
+
case "list":
|
|
172
|
+
results.push.apply(results, buildList(node, ctx, opts));
|
|
173
|
+
break;
|
|
174
|
+
case "listItem":
|
|
175
|
+
error("unreachable");
|
|
176
|
+
break;
|
|
177
|
+
case "table":
|
|
178
|
+
results.push(buildTable(node, ctx, opts));
|
|
179
|
+
break;
|
|
180
|
+
case "tableRow":
|
|
181
|
+
error("unreachable");
|
|
182
|
+
break;
|
|
183
|
+
case "tableCell":
|
|
184
|
+
error("unreachable");
|
|
185
|
+
break;
|
|
186
|
+
case "html":
|
|
187
|
+
results.push(buildHtml(node));
|
|
188
|
+
break;
|
|
189
|
+
case "code":
|
|
190
|
+
results.push(buildCode(node));
|
|
191
|
+
break;
|
|
192
|
+
case "yaml":
|
|
193
|
+
// FIXME: unimplemented
|
|
194
|
+
break;
|
|
195
|
+
case "toml":
|
|
196
|
+
// FIXME: unimplemented
|
|
197
|
+
break;
|
|
198
|
+
case "definition":
|
|
199
|
+
// FIXME: unimplemented
|
|
200
|
+
break;
|
|
201
|
+
case "footnoteDefinition":
|
|
202
|
+
// FIXME: unimplemented
|
|
203
|
+
break;
|
|
204
|
+
case "text":
|
|
205
|
+
results.push(buildText(node.value, ctx.deco));
|
|
206
|
+
break;
|
|
207
|
+
case "emphasis":
|
|
208
|
+
case "strong":
|
|
209
|
+
case "delete": {
|
|
210
|
+
var type = node.type, children = node.children;
|
|
211
|
+
results.push.apply(results, convertNodes(children, __assign(__assign({}, ctx), { deco: __assign(__assign({}, ctx.deco), (_a = {}, _a[type] = true, _a)) }), opts));
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
case "inlineCode":
|
|
215
|
+
// FIXME: transform to text for now
|
|
216
|
+
results.push(buildText(node.value, ctx.deco));
|
|
217
|
+
break;
|
|
218
|
+
case "break":
|
|
219
|
+
results.push(buildBreak());
|
|
220
|
+
break;
|
|
221
|
+
case "link":
|
|
222
|
+
results.push(buildLink(node, ctx, opts));
|
|
223
|
+
break;
|
|
224
|
+
case "image":
|
|
225
|
+
results.push(buildImage(node, ctx.images));
|
|
226
|
+
break;
|
|
227
|
+
case "linkReference":
|
|
228
|
+
// FIXME: unimplemented
|
|
229
|
+
break;
|
|
230
|
+
case "imageReference":
|
|
231
|
+
// FIXME: unimplemented
|
|
232
|
+
break;
|
|
233
|
+
case "footnote":
|
|
234
|
+
results.push(buildFootnote(node, ctx, opts));
|
|
235
|
+
break;
|
|
236
|
+
case "footnoteReference":
|
|
237
|
+
// FIXME: unimplemented
|
|
238
|
+
break;
|
|
239
|
+
case "math":
|
|
240
|
+
results.push(buildMath(node));
|
|
241
|
+
break;
|
|
242
|
+
case "inlineMath":
|
|
243
|
+
results.push(buildInlineMath(node));
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return results;
|
|
248
|
+
}
|
|
249
|
+
function buildParagraph(node, ctx, opts) {
|
|
250
|
+
node.type; var children = node.children;
|
|
251
|
+
var list = ctx.list;
|
|
252
|
+
return new docx.Paragraph(__assign({ children: convertNodes(children, ctx, opts) }, (list &&
|
|
253
|
+
(list.ordered
|
|
254
|
+
? {
|
|
255
|
+
numbering: {
|
|
256
|
+
reference: ORDERED_LIST_REF,
|
|
257
|
+
level: list.level,
|
|
258
|
+
},
|
|
259
|
+
}
|
|
260
|
+
: {
|
|
261
|
+
bullet: {
|
|
262
|
+
level: list.level,
|
|
263
|
+
},
|
|
264
|
+
}))));
|
|
265
|
+
}
|
|
266
|
+
function buildHeading(node, ctx, opts) {
|
|
267
|
+
node.type; var children = node.children, depth = node.depth;
|
|
268
|
+
var heading;
|
|
269
|
+
switch (depth) {
|
|
270
|
+
case 1:
|
|
271
|
+
heading = docx.HeadingLevel.TITLE;
|
|
272
|
+
break;
|
|
273
|
+
case 2:
|
|
274
|
+
heading = docx.HeadingLevel.HEADING_1;
|
|
275
|
+
break;
|
|
276
|
+
case 3:
|
|
277
|
+
heading = docx.HeadingLevel.HEADING_2;
|
|
278
|
+
break;
|
|
279
|
+
case 4:
|
|
280
|
+
heading = docx.HeadingLevel.HEADING_3;
|
|
281
|
+
break;
|
|
282
|
+
case 5:
|
|
283
|
+
heading = docx.HeadingLevel.HEADING_4;
|
|
284
|
+
break;
|
|
285
|
+
case 6:
|
|
286
|
+
heading = docx.HeadingLevel.HEADING_5;
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
return new docx.Paragraph({
|
|
290
|
+
heading: heading,
|
|
291
|
+
children: convertNodes(children, ctx, opts),
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
function buildThematicBreak(node) {
|
|
295
|
+
return new docx.Paragraph({
|
|
296
|
+
thematicBreak: true,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
function buildBlockquote(node, ctx, opts) {
|
|
300
|
+
// FIXME: do nothing for now
|
|
301
|
+
return convertNodes(node.children, ctx, opts);
|
|
302
|
+
}
|
|
303
|
+
function buildList(node, ctx, opts) {
|
|
304
|
+
node.type; var children = node.children, ordered = node.ordered; node.start; node.spread;
|
|
305
|
+
var list = {
|
|
306
|
+
level: ctx.list ? ctx.list.level + 1 : 0,
|
|
307
|
+
ordered: !!ordered,
|
|
308
|
+
};
|
|
309
|
+
return children.reduce(function (acc, item) {
|
|
310
|
+
acc.push.apply(acc, buildListItem(item, __assign(__assign({}, ctx), { list: list }), opts));
|
|
311
|
+
return acc;
|
|
312
|
+
}, []);
|
|
313
|
+
}
|
|
314
|
+
function buildListItem(node, ctx, opts) {
|
|
315
|
+
node.type; var children = node.children; node.checked; node.spread;
|
|
316
|
+
return convertNodes(children, ctx, opts);
|
|
317
|
+
}
|
|
318
|
+
function buildTable(node, ctx, opts) {
|
|
319
|
+
node.type; var children = node.children, align = node.align;
|
|
320
|
+
var cellAligns = align === null || align === void 0 ? void 0 : align.map(function (a) {
|
|
321
|
+
switch (a) {
|
|
322
|
+
case "left":
|
|
323
|
+
return docx.AlignmentType.LEFT;
|
|
324
|
+
case "right":
|
|
325
|
+
return docx.AlignmentType.RIGHT;
|
|
326
|
+
case "center":
|
|
327
|
+
return docx.AlignmentType.CENTER;
|
|
328
|
+
default:
|
|
329
|
+
return docx.AlignmentType.LEFT;
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
return new docx.Table({
|
|
333
|
+
rows: children.map(function (r) {
|
|
334
|
+
return buildTableRow(r, ctx, opts, cellAligns);
|
|
335
|
+
}),
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
function buildTableRow(node, ctx, opts, cellAligns) {
|
|
339
|
+
node.type; var children = node.children;
|
|
340
|
+
return new docx.TableRow({
|
|
341
|
+
children: children.map(function (c, i) {
|
|
342
|
+
return buildTableCell(c, ctx, opts, cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i]);
|
|
343
|
+
}),
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
function buildTableCell(node, ctx, opts, align) {
|
|
347
|
+
node.type; var children = node.children;
|
|
348
|
+
return new docx.TableCell({
|
|
349
|
+
children: [
|
|
350
|
+
new docx.Paragraph({
|
|
351
|
+
alignment: align,
|
|
352
|
+
children: convertNodes(children, ctx, opts),
|
|
353
|
+
}),
|
|
354
|
+
],
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
function buildHtml(node) {
|
|
358
|
+
node.type; var value = node.value;
|
|
359
|
+
// FIXME: transform to text for now
|
|
360
|
+
return new docx.Paragraph({
|
|
361
|
+
children: [buildText(value, {})],
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
function buildCode(node) {
|
|
365
|
+
node.type; var value = node.value; node.lang; node.meta;
|
|
366
|
+
// FIXME: transform to text for now
|
|
367
|
+
return new docx.Paragraph({
|
|
368
|
+
children: [buildText(value, {})],
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
function buildMath(node) {
|
|
372
|
+
node.type; var value = node.value;
|
|
373
|
+
// FIXME: transform to text for now
|
|
374
|
+
return new docx.Paragraph({
|
|
375
|
+
children: [new docx.TextRun(value)],
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
function buildInlineMath(node) {
|
|
379
|
+
node.type; var value = node.value;
|
|
380
|
+
// FIXME: transform to text for now
|
|
381
|
+
return new docx.TextRun(value);
|
|
382
|
+
}
|
|
383
|
+
function buildText(text, deco) {
|
|
384
|
+
return new docx.TextRun({
|
|
385
|
+
text: text,
|
|
386
|
+
bold: deco.strong,
|
|
387
|
+
italics: deco.emphasis,
|
|
388
|
+
strike: deco.delete,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
function buildBreak(node) {
|
|
392
|
+
return new docx.TextRun({ text: "", break: 1 });
|
|
393
|
+
}
|
|
394
|
+
function buildLink(node, ctx, opts) {
|
|
395
|
+
node.type; var children = node.children, url = node.url; node.title;
|
|
396
|
+
return new docx.ExternalHyperlink({
|
|
397
|
+
link: url,
|
|
398
|
+
children: convertNodes(children, ctx, opts),
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
function buildImage(node, images) {
|
|
402
|
+
node.type; var url = node.url; node.title; node.alt;
|
|
403
|
+
if (!images[url]) {
|
|
404
|
+
return error("Fetch image was failed: ".concat(url));
|
|
405
|
+
}
|
|
406
|
+
var _a = images[url], image = _a.image, width = _a.width, height = _a.height;
|
|
407
|
+
return new docx.ImageRun({
|
|
408
|
+
data: image,
|
|
409
|
+
transformation: {
|
|
410
|
+
width: width,
|
|
411
|
+
height: height,
|
|
412
|
+
},
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
function buildFootnote(node, ctx, opts) {
|
|
416
|
+
node.type; var children = node.children;
|
|
417
|
+
// FIXME: transform to paragraph for now
|
|
418
|
+
return new docx.Paragraph({
|
|
419
|
+
children: convertNodes(children, ctx, opts),
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
var plugin = function (opts) {
|
|
424
|
+
var _this = this;
|
|
425
|
+
if (opts === void 0) { opts = {}; }
|
|
426
|
+
var images = {};
|
|
427
|
+
this.Compiler = function (node) {
|
|
428
|
+
return mdastToDocx(node, opts, images);
|
|
429
|
+
};
|
|
430
|
+
return function (node) { return __awaiter(_this, void 0, void 0, function () {
|
|
431
|
+
var imageResolver, imageList, imageDatas;
|
|
432
|
+
return __generator(this, function (_a) {
|
|
433
|
+
switch (_a.label) {
|
|
434
|
+
case 0:
|
|
435
|
+
imageResolver = opts.imageResolver;
|
|
436
|
+
if (!imageResolver) {
|
|
437
|
+
throw new Error("options.imageResolver is not defined.");
|
|
438
|
+
}
|
|
439
|
+
imageList = [];
|
|
440
|
+
visit(node, "image", function (node) {
|
|
441
|
+
imageList.push(node);
|
|
442
|
+
});
|
|
443
|
+
return [4 /*yield*/, Promise.all(imageList.map(function (_a) {
|
|
444
|
+
var url = _a.url;
|
|
445
|
+
return imageResolver(url);
|
|
446
|
+
}))];
|
|
447
|
+
case 1:
|
|
448
|
+
imageDatas = _a.sent();
|
|
449
|
+
images = imageList.reduce(function (acc, img, i) {
|
|
450
|
+
acc[img.url] = imageDatas[i];
|
|
451
|
+
return acc;
|
|
452
|
+
}, {});
|
|
453
|
+
return [2 /*return*/, node];
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
}); };
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
export { plugin as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Parent, Literal, Root, Paragraph, Heading, ThematicBreak, Blockquote, List, ListItem, Table, TableRow, TableCell, HTML, Code, YAML, Definition, FootnoteDefinition, Text, Emphasis, Strong, Delete, InlineCode, Break, Link, Image, LinkReference, ImageReference, Footnote, FootnoteReference, Resource, Association, Reference, Alternative } from "mdast";
|
|
2
|
+
export type { Parent, Literal, Root, Paragraph, Heading, ThematicBreak, Blockquote, List, ListItem, Table, TableRow, TableCell, HTML, Code, YAML, Definition, FootnoteDefinition, Text, Emphasis, Strong, Delete, InlineCode, Break, Link, Image, LinkReference, ImageReference, Footnote, FootnoteReference, Resource, Association, Reference, Alternative, };
|
|
3
|
+
export interface TOML extends Literal {
|
|
4
|
+
type: "toml";
|
|
5
|
+
}
|
|
6
|
+
export interface Math extends Literal {
|
|
7
|
+
type: "math";
|
|
8
|
+
}
|
|
9
|
+
export interface InlineMath extends Literal {
|
|
10
|
+
type: "inlineMath";
|
|
11
|
+
}
|
|
12
|
+
export declare type Content = TopLevelContent | ListContent | TableContent | RowContent | PhrasingContent;
|
|
13
|
+
export declare type TopLevelContent = BlockContent | FrontmatterContent | DefinitionContent;
|
|
14
|
+
export declare type BlockContent = Paragraph | Heading | ThematicBreak | Blockquote | List | Table | HTML | Code | Math;
|
|
15
|
+
export declare type FrontmatterContent = YAML | TOML;
|
|
16
|
+
export declare type DefinitionContent = Definition | FootnoteDefinition;
|
|
17
|
+
export declare type ListContent = ListItem;
|
|
18
|
+
export declare type TableContent = TableRow;
|
|
19
|
+
export declare type RowContent = TableCell;
|
|
20
|
+
export declare type PhrasingContent = StaticPhrasingContent | Link | LinkReference;
|
|
21
|
+
export declare type StaticPhrasingContent = Text | Emphasis | Strong | Delete | HTML | InlineCode | Break | Image | ImageReference | Footnote | FootnoteReference | InlineMath;
|
package/lib/plugin.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as docx from "docx";
|
|
2
|
+
import { IPropertiesOptions } from "docx/build/file/core-properties";
|
|
3
|
+
import * as mdast from "./models/mdast";
|
|
4
|
+
export declare type ImageDataMap = {
|
|
5
|
+
[url: string]: ImageData;
|
|
6
|
+
};
|
|
7
|
+
export declare type ImageData = {
|
|
8
|
+
image: docx.IImageOptions["data"];
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
};
|
|
12
|
+
export declare type ImageResolver = (url: string) => Promise<ImageData> | ImageData;
|
|
13
|
+
export declare type Opts = {
|
|
14
|
+
docProperties?: Omit<IPropertiesOptions, "sections" | "numbering">;
|
|
15
|
+
imageResolver?: ImageResolver;
|
|
16
|
+
};
|
|
17
|
+
export declare function mdastToDocx(node: mdast.Root, opts: Opts, images: ImageDataMap): docx.File;
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "remark-docx",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "remark plugin to transform markdown to docx.",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"module": "lib/index.mjs",
|
|
7
|
+
"types": "lib/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"lib"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "rollup -c",
|
|
13
|
+
"test": "jest",
|
|
14
|
+
"tsc": "tsc -p . --noEmit",
|
|
15
|
+
"tsc:d": "tsc -p . --outDir lib -d --emitDeclarationOnly",
|
|
16
|
+
"storybook": "start-storybook -p 6006",
|
|
17
|
+
"storybook:build": "build-storybook",
|
|
18
|
+
"prepublishOnly": "rimraf lib && npm run build && npm run tsc:d"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"docx": "^7.3.0",
|
|
22
|
+
"unist-util-visit": "^4.1.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@babel/core": "7.15.5",
|
|
26
|
+
"@babel/preset-env": "7.15.6",
|
|
27
|
+
"@babel/preset-typescript": "7.15.0",
|
|
28
|
+
"@rollup/plugin-typescript": "8.2.5",
|
|
29
|
+
"@storybook/addon-essentials": "6.4.9",
|
|
30
|
+
"@storybook/addon-links": "6.4.9",
|
|
31
|
+
"@storybook/react": "6.4.9",
|
|
32
|
+
"@types/jest": "^27.4.0",
|
|
33
|
+
"@types/mdast": "^3.0.10",
|
|
34
|
+
"@types/unist": "2.0.6",
|
|
35
|
+
"babel-jest": "27.4.6",
|
|
36
|
+
"file-saver": "^2.0.5",
|
|
37
|
+
"jest": "27.4.7",
|
|
38
|
+
"react": "17.0.2",
|
|
39
|
+
"react-dom": "17.0.2",
|
|
40
|
+
"react-is": "17.0.2",
|
|
41
|
+
"remark-footnotes": "4.0.1",
|
|
42
|
+
"remark-frontmatter": "4.0.1",
|
|
43
|
+
"remark-gfm": "3.0.1",
|
|
44
|
+
"remark-math": "5.1.1",
|
|
45
|
+
"remark-parse": "10.0.1",
|
|
46
|
+
"rimraf": "3.0.2",
|
|
47
|
+
"rollup": "2.63.0",
|
|
48
|
+
"tslib": "2.3.1",
|
|
49
|
+
"typescript": "4.5.4",
|
|
50
|
+
"unified": "10.1.1"
|
|
51
|
+
},
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "git+https://github.com/inokawa/remark-docx.git"
|
|
55
|
+
},
|
|
56
|
+
"keywords": [
|
|
57
|
+
"unist",
|
|
58
|
+
"remark",
|
|
59
|
+
"mdast",
|
|
60
|
+
"markdown",
|
|
61
|
+
"docx",
|
|
62
|
+
"word",
|
|
63
|
+
"office"
|
|
64
|
+
],
|
|
65
|
+
"author": "inokawa <stratoooo-taster@yahoo.co.jp> (https://github.com/inokawa/)",
|
|
66
|
+
"license": "MIT",
|
|
67
|
+
"bugs": {
|
|
68
|
+
"url": "https://github.com/inokawa/remark-docx/issues"
|
|
69
|
+
},
|
|
70
|
+
"homepage": "https://github.com/inokawa/remark-docx#readme"
|
|
71
|
+
}
|