remark-docx 0.0.1 → 0.0.5
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/README.md +20 -11
- package/lib/index.d.ts +0 -1
- package/lib/index.js +90 -72
- package/lib/index.mjs +90 -67
- package/lib/transformer.d.ts +3 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
  
|
|
4
4
|
|
|
5
|
-
[remark](https://github.com/remarkjs/remark) plugin to
|
|
5
|
+
[remark](https://github.com/remarkjs/remark) plugin to compile markdown to docx.
|
|
6
6
|
|
|
7
7
|
### 🚧 WIP 🚧
|
|
8
8
|
|
|
@@ -27,16 +27,16 @@ npm install remark-docx
|
|
|
27
27
|
```javascript
|
|
28
28
|
import { unified } from "unified";
|
|
29
29
|
import markdown from "remark-parse";
|
|
30
|
-
import docx
|
|
30
|
+
import docx from "remark-docx";
|
|
31
31
|
import { saveAs } from "file-saver";
|
|
32
32
|
|
|
33
|
-
const processor = unified().use(markdown).use(docx);
|
|
33
|
+
const processor = unified().use(markdown).use(docx, { output: "blob" });
|
|
34
34
|
|
|
35
35
|
const text = "# hello world";
|
|
36
36
|
|
|
37
37
|
(async () => {
|
|
38
38
|
const doc = await processor.process(text);
|
|
39
|
-
const blob = await
|
|
39
|
+
const blob = await doc.result;
|
|
40
40
|
saveAs(blob, "example.docx");
|
|
41
41
|
})();
|
|
42
42
|
```
|
|
@@ -46,23 +46,32 @@ const text = "# hello world";
|
|
|
46
46
|
```javascript
|
|
47
47
|
import { unified } from "unified";
|
|
48
48
|
import markdown from "remark-parse";
|
|
49
|
-
import docx
|
|
49
|
+
import docx from "remark-docx";
|
|
50
50
|
import * as fs from "fs";
|
|
51
51
|
|
|
52
|
-
const processor = unified().use(markdown).use(docx);
|
|
52
|
+
const processor = unified().use(markdown).use(docx, { output: "buffer" });
|
|
53
53
|
|
|
54
54
|
const text = "# hello world";
|
|
55
55
|
|
|
56
56
|
(async () => {
|
|
57
57
|
const doc = await processor.process(text);
|
|
58
|
-
const buffer = await
|
|
58
|
+
const buffer = await doc.result;
|
|
59
59
|
fs.writeFileSync("example.docx", buffer);
|
|
60
60
|
})();
|
|
61
61
|
```
|
|
62
62
|
|
|
63
63
|
## Options
|
|
64
64
|
|
|
65
|
-
| Key
|
|
66
|
-
|
|
|
67
|
-
|
|
|
68
|
-
| imageResolver
|
|
65
|
+
| Key | Default | Type | Description |
|
|
66
|
+
| -------------- | --------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
67
|
+
| output | "buffer" | `"buffer"` `"blob"` `"raw"` | Set output type of `VFile.result`. `buffer` is `Promise<ArrayBuffer>`. `blob` is `Promise<Blob>`. `raw` is internal data for testing. |
|
|
68
|
+
| imageResolver | undefined | ImageResolver? | **You must set** if your markdown includes images. See example for [browser](https://github.com/inokawa/remark-docx/blob/main/stories/playground.stories.tsx) and [Node.js](https://github.com/inokawa/remark-docx/blob/main/src/index.spec.ts). |
|
|
69
|
+
| title | undefined | string? | |
|
|
70
|
+
| subject | undefined | string? | |
|
|
71
|
+
| creator | undefined | string? | |
|
|
72
|
+
| keywords | undefined | string? | |
|
|
73
|
+
| description | undefined | string? | |
|
|
74
|
+
| lastModifiedBy | undefined | string? | |
|
|
75
|
+
| revision | undefined | string? | |
|
|
76
|
+
| styles | undefined | IStylesOptions? | |
|
|
77
|
+
| background | undefined | IDocumentBackgroundOptions? | |
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var unistUtilVisit = require('unist-util-visit');
|
|
6
4
|
var docx = require('docx');
|
|
7
5
|
|
|
@@ -157,47 +155,65 @@ var DEFAULT_NUMBERINGS = [
|
|
|
157
155
|
var error = function (message) {
|
|
158
156
|
throw new Error(message);
|
|
159
157
|
};
|
|
160
|
-
function mdastToDocx(node,
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
function buildDocxRoot(root, opts, images) {
|
|
158
|
+
function mdastToDocx(node, _a, images) {
|
|
159
|
+
var output = _a.output, title = _a.title, subject = _a.subject, creator = _a.creator, keywords = _a.keywords, description = _a.description, lastModifiedBy = _a.lastModifiedBy, revision = _a.revision, styles = _a.styles, background = _a.background;
|
|
164
160
|
var ctx = { deco: {}, images: images };
|
|
165
|
-
var nodes = convertNodes(
|
|
166
|
-
|
|
161
|
+
var nodes = convertNodes(node.children, ctx);
|
|
162
|
+
var doc = new docx__namespace.Document({
|
|
163
|
+
title: title,
|
|
164
|
+
subject: subject,
|
|
165
|
+
creator: creator,
|
|
166
|
+
keywords: keywords,
|
|
167
|
+
description: description,
|
|
168
|
+
lastModifiedBy: lastModifiedBy,
|
|
169
|
+
revision: revision,
|
|
170
|
+
styles: styles,
|
|
171
|
+
background: background,
|
|
172
|
+
sections: [{ children: nodes }],
|
|
173
|
+
numbering: {
|
|
167
174
|
config: [
|
|
168
175
|
{
|
|
169
176
|
reference: ORDERED_LIST_REF,
|
|
170
177
|
levels: DEFAULT_NUMBERINGS,
|
|
171
178
|
},
|
|
172
179
|
],
|
|
173
|
-
}
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
switch (output !== null && output !== void 0 ? output : "buffer") {
|
|
183
|
+
case "buffer":
|
|
184
|
+
return docx.Packer.toBuffer(doc);
|
|
185
|
+
case "blob":
|
|
186
|
+
return docx.Packer.toBlob(doc);
|
|
187
|
+
case "raw":
|
|
188
|
+
return doc;
|
|
189
|
+
}
|
|
174
190
|
}
|
|
175
|
-
function convertNodes(nodes, ctx
|
|
191
|
+
function convertNodes(nodes, ctx) {
|
|
176
192
|
var _a;
|
|
177
193
|
var results = [];
|
|
178
194
|
for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
|
|
179
195
|
var node = nodes_1[_i];
|
|
180
196
|
switch (node.type) {
|
|
181
197
|
case "paragraph":
|
|
182
|
-
results.push(buildParagraph(node, ctx
|
|
198
|
+
results.push(buildParagraph(node, ctx));
|
|
183
199
|
break;
|
|
184
200
|
case "heading":
|
|
185
|
-
results.push(buildHeading(node, ctx
|
|
201
|
+
results.push(buildHeading(node, ctx));
|
|
186
202
|
break;
|
|
187
203
|
case "thematicBreak":
|
|
188
|
-
results.push(buildThematicBreak());
|
|
204
|
+
results.push(buildThematicBreak(node));
|
|
189
205
|
break;
|
|
190
206
|
case "blockquote":
|
|
191
|
-
results.push.apply(results, buildBlockquote(node, ctx
|
|
207
|
+
results.push.apply(results, buildBlockquote(node, ctx));
|
|
192
208
|
break;
|
|
193
209
|
case "list":
|
|
194
|
-
results.push.apply(results, buildList(node, ctx
|
|
210
|
+
results.push.apply(results, buildList(node, ctx));
|
|
195
211
|
break;
|
|
196
212
|
case "listItem":
|
|
197
213
|
error("unreachable");
|
|
198
214
|
break;
|
|
199
215
|
case "table":
|
|
200
|
-
results.push(buildTable(node, ctx
|
|
216
|
+
results.push(buildTable(node, ctx));
|
|
201
217
|
break;
|
|
202
218
|
case "tableRow":
|
|
203
219
|
error("unreachable");
|
|
@@ -230,7 +246,7 @@ function convertNodes(nodes, ctx, opts) {
|
|
|
230
246
|
case "strong":
|
|
231
247
|
case "delete": {
|
|
232
248
|
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)) })
|
|
249
|
+
results.push.apply(results, convertNodes(children, __assign(__assign({}, ctx), { deco: __assign(__assign({}, ctx.deco), (_a = {}, _a[type] = true, _a)) })));
|
|
234
250
|
break;
|
|
235
251
|
}
|
|
236
252
|
case "inlineCode":
|
|
@@ -238,10 +254,10 @@ function convertNodes(nodes, ctx, opts) {
|
|
|
238
254
|
results.push(buildText(node.value, ctx.deco));
|
|
239
255
|
break;
|
|
240
256
|
case "break":
|
|
241
|
-
results.push(buildBreak());
|
|
257
|
+
results.push(buildBreak(node));
|
|
242
258
|
break;
|
|
243
259
|
case "link":
|
|
244
|
-
results.push(buildLink(node, ctx
|
|
260
|
+
results.push(buildLink(node, ctx));
|
|
245
261
|
break;
|
|
246
262
|
case "image":
|
|
247
263
|
results.push(buildImage(node, ctx.images));
|
|
@@ -253,7 +269,7 @@ function convertNodes(nodes, ctx, opts) {
|
|
|
253
269
|
// FIXME: unimplemented
|
|
254
270
|
break;
|
|
255
271
|
case "footnote":
|
|
256
|
-
results.push(buildFootnote(node, ctx
|
|
272
|
+
results.push(buildFootnote(node, ctx));
|
|
257
273
|
break;
|
|
258
274
|
case "footnoteReference":
|
|
259
275
|
// FIXME: unimplemented
|
|
@@ -268,10 +284,10 @@ function convertNodes(nodes, ctx, opts) {
|
|
|
268
284
|
}
|
|
269
285
|
return results;
|
|
270
286
|
}
|
|
271
|
-
function buildParagraph(
|
|
272
|
-
|
|
287
|
+
function buildParagraph(_a, ctx) {
|
|
288
|
+
_a.type; var children = _a.children;
|
|
273
289
|
var list = ctx.list;
|
|
274
|
-
return new docx__namespace.Paragraph(__assign({ children: convertNodes(children, ctx
|
|
290
|
+
return new docx__namespace.Paragraph(__assign({ children: convertNodes(children, ctx) }, (list &&
|
|
275
291
|
(list.ordered
|
|
276
292
|
? {
|
|
277
293
|
numbering: {
|
|
@@ -285,8 +301,8 @@ function buildParagraph(node, ctx, opts) {
|
|
|
285
301
|
},
|
|
286
302
|
}))));
|
|
287
303
|
}
|
|
288
|
-
function buildHeading(
|
|
289
|
-
|
|
304
|
+
function buildHeading(_a, ctx) {
|
|
305
|
+
_a.type; var children = _a.children, depth = _a.depth;
|
|
290
306
|
var heading;
|
|
291
307
|
switch (depth) {
|
|
292
308
|
case 1:
|
|
@@ -310,35 +326,37 @@ function buildHeading(node, ctx, opts) {
|
|
|
310
326
|
}
|
|
311
327
|
return new docx__namespace.Paragraph({
|
|
312
328
|
heading: heading,
|
|
313
|
-
children: convertNodes(children, ctx
|
|
329
|
+
children: convertNodes(children, ctx),
|
|
314
330
|
});
|
|
315
331
|
}
|
|
316
|
-
function buildThematicBreak(
|
|
332
|
+
function buildThematicBreak(_a) {
|
|
333
|
+
_a.type;
|
|
317
334
|
return new docx__namespace.Paragraph({
|
|
318
335
|
thematicBreak: true,
|
|
319
336
|
});
|
|
320
337
|
}
|
|
321
|
-
function buildBlockquote(
|
|
338
|
+
function buildBlockquote(_a, ctx) {
|
|
339
|
+
_a.type; var children = _a.children;
|
|
322
340
|
// FIXME: do nothing for now
|
|
323
|
-
return convertNodes(
|
|
341
|
+
return convertNodes(children, ctx);
|
|
324
342
|
}
|
|
325
|
-
function buildList(
|
|
326
|
-
|
|
343
|
+
function buildList(_a, ctx) {
|
|
344
|
+
_a.type; var children = _a.children, ordered = _a.ordered; _a.start; _a.spread;
|
|
327
345
|
var list = {
|
|
328
346
|
level: ctx.list ? ctx.list.level + 1 : 0,
|
|
329
347
|
ordered: !!ordered,
|
|
330
348
|
};
|
|
331
349
|
return children.reduce(function (acc, item) {
|
|
332
|
-
acc.push.apply(acc, buildListItem(item, __assign(__assign({}, ctx), { list: list })
|
|
350
|
+
acc.push.apply(acc, buildListItem(item, __assign(__assign({}, ctx), { list: list })));
|
|
333
351
|
return acc;
|
|
334
352
|
}, []);
|
|
335
353
|
}
|
|
336
|
-
function buildListItem(
|
|
337
|
-
|
|
338
|
-
return convertNodes(children, ctx
|
|
354
|
+
function buildListItem(_a, ctx) {
|
|
355
|
+
_a.type; var children = _a.children; _a.checked; _a.spread;
|
|
356
|
+
return convertNodes(children, ctx);
|
|
339
357
|
}
|
|
340
|
-
function buildTable(
|
|
341
|
-
|
|
358
|
+
function buildTable(_a, ctx) {
|
|
359
|
+
_a.type; var children = _a.children, align = _a.align;
|
|
342
360
|
var cellAligns = align === null || align === void 0 ? void 0 : align.map(function (a) {
|
|
343
361
|
switch (a) {
|
|
344
362
|
case "left":
|
|
@@ -353,52 +371,52 @@ function buildTable(node, ctx, opts) {
|
|
|
353
371
|
});
|
|
354
372
|
return new docx__namespace.Table({
|
|
355
373
|
rows: children.map(function (r) {
|
|
356
|
-
return buildTableRow(r, ctx,
|
|
374
|
+
return buildTableRow(r, ctx, cellAligns);
|
|
357
375
|
}),
|
|
358
376
|
});
|
|
359
377
|
}
|
|
360
|
-
function buildTableRow(
|
|
361
|
-
|
|
378
|
+
function buildTableRow(_a, ctx, cellAligns) {
|
|
379
|
+
_a.type; var children = _a.children;
|
|
362
380
|
return new docx__namespace.TableRow({
|
|
363
381
|
children: children.map(function (c, i) {
|
|
364
|
-
return buildTableCell(c, ctx,
|
|
382
|
+
return buildTableCell(c, ctx, cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i]);
|
|
365
383
|
}),
|
|
366
384
|
});
|
|
367
385
|
}
|
|
368
|
-
function buildTableCell(
|
|
369
|
-
|
|
386
|
+
function buildTableCell(_a, ctx, align) {
|
|
387
|
+
_a.type; var children = _a.children;
|
|
370
388
|
return new docx__namespace.TableCell({
|
|
371
389
|
children: [
|
|
372
390
|
new docx__namespace.Paragraph({
|
|
373
391
|
alignment: align,
|
|
374
|
-
children: convertNodes(children, ctx
|
|
392
|
+
children: convertNodes(children, ctx),
|
|
375
393
|
}),
|
|
376
394
|
],
|
|
377
395
|
});
|
|
378
396
|
}
|
|
379
|
-
function buildHtml(
|
|
380
|
-
|
|
397
|
+
function buildHtml(_a) {
|
|
398
|
+
_a.type; var value = _a.value;
|
|
381
399
|
// FIXME: transform to text for now
|
|
382
400
|
return new docx__namespace.Paragraph({
|
|
383
401
|
children: [buildText(value, {})],
|
|
384
402
|
});
|
|
385
403
|
}
|
|
386
|
-
function buildCode(
|
|
387
|
-
|
|
404
|
+
function buildCode(_a) {
|
|
405
|
+
_a.type; var value = _a.value; _a.lang; _a.meta;
|
|
388
406
|
// FIXME: transform to text for now
|
|
389
407
|
return new docx__namespace.Paragraph({
|
|
390
408
|
children: [buildText(value, {})],
|
|
391
409
|
});
|
|
392
410
|
}
|
|
393
|
-
function buildMath(
|
|
394
|
-
|
|
411
|
+
function buildMath(_a) {
|
|
412
|
+
_a.type; var value = _a.value;
|
|
395
413
|
// FIXME: transform to text for now
|
|
396
414
|
return new docx__namespace.Paragraph({
|
|
397
415
|
children: [new docx__namespace.TextRun(value)],
|
|
398
416
|
});
|
|
399
417
|
}
|
|
400
|
-
function buildInlineMath(
|
|
401
|
-
|
|
418
|
+
function buildInlineMath(_a) {
|
|
419
|
+
_a.type; var value = _a.value;
|
|
402
420
|
// FIXME: transform to text for now
|
|
403
421
|
return new docx__namespace.TextRun(value);
|
|
404
422
|
}
|
|
@@ -410,22 +428,23 @@ function buildText(text, deco) {
|
|
|
410
428
|
strike: deco.delete,
|
|
411
429
|
});
|
|
412
430
|
}
|
|
413
|
-
function buildBreak(
|
|
431
|
+
function buildBreak(_a) {
|
|
432
|
+
_a.type;
|
|
414
433
|
return new docx__namespace.TextRun({ text: "", break: 1 });
|
|
415
434
|
}
|
|
416
|
-
function buildLink(
|
|
417
|
-
|
|
435
|
+
function buildLink(_a, ctx) {
|
|
436
|
+
_a.type; var children = _a.children, url = _a.url; _a.title;
|
|
418
437
|
return new docx__namespace.ExternalHyperlink({
|
|
419
438
|
link: url,
|
|
420
|
-
children: convertNodes(children, ctx
|
|
439
|
+
children: convertNodes(children, ctx),
|
|
421
440
|
});
|
|
422
441
|
}
|
|
423
|
-
function buildImage(
|
|
424
|
-
|
|
442
|
+
function buildImage(_a, images) {
|
|
443
|
+
_a.type; var url = _a.url; _a.title; _a.alt;
|
|
425
444
|
if (!images[url]) {
|
|
426
445
|
return error("Fetch image was failed: ".concat(url));
|
|
427
446
|
}
|
|
428
|
-
var
|
|
447
|
+
var _b = images[url], image = _b.image, width = _b.width, height = _b.height;
|
|
429
448
|
return new docx__namespace.ImageRun({
|
|
430
449
|
data: image,
|
|
431
450
|
transformation: {
|
|
@@ -434,11 +453,11 @@ function buildImage(node, images) {
|
|
|
434
453
|
},
|
|
435
454
|
});
|
|
436
455
|
}
|
|
437
|
-
function buildFootnote(
|
|
438
|
-
|
|
456
|
+
function buildFootnote(_a, ctx) {
|
|
457
|
+
_a.type; var children = _a.children;
|
|
439
458
|
// FIXME: transform to paragraph for now
|
|
440
459
|
return new docx__namespace.Paragraph({
|
|
441
|
-
children: convertNodes(children, ctx
|
|
460
|
+
children: convertNodes(children, ctx),
|
|
442
461
|
});
|
|
443
462
|
}
|
|
444
463
|
|
|
@@ -450,18 +469,21 @@ var plugin = function (opts) {
|
|
|
450
469
|
return mdastToDocx(node, opts, images);
|
|
451
470
|
};
|
|
452
471
|
return function (node) { return __awaiter(_this, void 0, void 0, function () {
|
|
453
|
-
var
|
|
472
|
+
var imageList, imageResolver, imageDatas;
|
|
454
473
|
return __generator(this, function (_a) {
|
|
455
474
|
switch (_a.label) {
|
|
456
475
|
case 0:
|
|
457
|
-
imageResolver = opts.imageResolver;
|
|
458
|
-
if (!imageResolver) {
|
|
459
|
-
throw new Error("options.imageResolver is not defined.");
|
|
460
|
-
}
|
|
461
476
|
imageList = [];
|
|
462
477
|
unistUtilVisit.visit(node, "image", function (node) {
|
|
463
478
|
imageList.push(node);
|
|
464
479
|
});
|
|
480
|
+
if (imageList.length === 0) {
|
|
481
|
+
return [2 /*return*/, node];
|
|
482
|
+
}
|
|
483
|
+
imageResolver = opts.imageResolver;
|
|
484
|
+
if (!imageResolver) {
|
|
485
|
+
throw new Error("options.imageResolver is not defined.");
|
|
486
|
+
}
|
|
465
487
|
return [4 /*yield*/, Promise.all(imageList.map(function (_a) {
|
|
466
488
|
var url = _a.url;
|
|
467
489
|
return imageResolver(url);
|
|
@@ -478,8 +500,4 @@ var plugin = function (opts) {
|
|
|
478
500
|
}); };
|
|
479
501
|
};
|
|
480
502
|
|
|
481
|
-
|
|
482
|
-
enumerable: true,
|
|
483
|
-
get: function () { return docx.Packer; }
|
|
484
|
-
});
|
|
485
|
-
exports["default"] = plugin;
|
|
503
|
+
module.exports = plugin;
|
package/lib/index.mjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { visit } from 'unist-util-visit';
|
|
2
2
|
import * as docx from 'docx';
|
|
3
|
-
import { convertInchesToTwip } from 'docx';
|
|
4
|
-
export { Packer } from 'docx';
|
|
3
|
+
import { convertInchesToTwip, Packer } from 'docx';
|
|
5
4
|
|
|
6
5
|
/*! *****************************************************************************
|
|
7
6
|
Copyright (c) Microsoft Corporation.
|
|
@@ -135,47 +134,65 @@ var DEFAULT_NUMBERINGS = [
|
|
|
135
134
|
var error = function (message) {
|
|
136
135
|
throw new Error(message);
|
|
137
136
|
};
|
|
138
|
-
function mdastToDocx(node,
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
function buildDocxRoot(root, opts, images) {
|
|
137
|
+
function mdastToDocx(node, _a, images) {
|
|
138
|
+
var output = _a.output, title = _a.title, subject = _a.subject, creator = _a.creator, keywords = _a.keywords, description = _a.description, lastModifiedBy = _a.lastModifiedBy, revision = _a.revision, styles = _a.styles, background = _a.background;
|
|
142
139
|
var ctx = { deco: {}, images: images };
|
|
143
|
-
var nodes = convertNodes(
|
|
144
|
-
|
|
140
|
+
var nodes = convertNodes(node.children, ctx);
|
|
141
|
+
var doc = new docx.Document({
|
|
142
|
+
title: title,
|
|
143
|
+
subject: subject,
|
|
144
|
+
creator: creator,
|
|
145
|
+
keywords: keywords,
|
|
146
|
+
description: description,
|
|
147
|
+
lastModifiedBy: lastModifiedBy,
|
|
148
|
+
revision: revision,
|
|
149
|
+
styles: styles,
|
|
150
|
+
background: background,
|
|
151
|
+
sections: [{ children: nodes }],
|
|
152
|
+
numbering: {
|
|
145
153
|
config: [
|
|
146
154
|
{
|
|
147
155
|
reference: ORDERED_LIST_REF,
|
|
148
156
|
levels: DEFAULT_NUMBERINGS,
|
|
149
157
|
},
|
|
150
158
|
],
|
|
151
|
-
}
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
switch (output !== null && output !== void 0 ? output : "buffer") {
|
|
162
|
+
case "buffer":
|
|
163
|
+
return Packer.toBuffer(doc);
|
|
164
|
+
case "blob":
|
|
165
|
+
return Packer.toBlob(doc);
|
|
166
|
+
case "raw":
|
|
167
|
+
return doc;
|
|
168
|
+
}
|
|
152
169
|
}
|
|
153
|
-
function convertNodes(nodes, ctx
|
|
170
|
+
function convertNodes(nodes, ctx) {
|
|
154
171
|
var _a;
|
|
155
172
|
var results = [];
|
|
156
173
|
for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
|
|
157
174
|
var node = nodes_1[_i];
|
|
158
175
|
switch (node.type) {
|
|
159
176
|
case "paragraph":
|
|
160
|
-
results.push(buildParagraph(node, ctx
|
|
177
|
+
results.push(buildParagraph(node, ctx));
|
|
161
178
|
break;
|
|
162
179
|
case "heading":
|
|
163
|
-
results.push(buildHeading(node, ctx
|
|
180
|
+
results.push(buildHeading(node, ctx));
|
|
164
181
|
break;
|
|
165
182
|
case "thematicBreak":
|
|
166
|
-
results.push(buildThematicBreak());
|
|
183
|
+
results.push(buildThematicBreak(node));
|
|
167
184
|
break;
|
|
168
185
|
case "blockquote":
|
|
169
|
-
results.push.apply(results, buildBlockquote(node, ctx
|
|
186
|
+
results.push.apply(results, buildBlockquote(node, ctx));
|
|
170
187
|
break;
|
|
171
188
|
case "list":
|
|
172
|
-
results.push.apply(results, buildList(node, ctx
|
|
189
|
+
results.push.apply(results, buildList(node, ctx));
|
|
173
190
|
break;
|
|
174
191
|
case "listItem":
|
|
175
192
|
error("unreachable");
|
|
176
193
|
break;
|
|
177
194
|
case "table":
|
|
178
|
-
results.push(buildTable(node, ctx
|
|
195
|
+
results.push(buildTable(node, ctx));
|
|
179
196
|
break;
|
|
180
197
|
case "tableRow":
|
|
181
198
|
error("unreachable");
|
|
@@ -208,7 +225,7 @@ function convertNodes(nodes, ctx, opts) {
|
|
|
208
225
|
case "strong":
|
|
209
226
|
case "delete": {
|
|
210
227
|
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)) })
|
|
228
|
+
results.push.apply(results, convertNodes(children, __assign(__assign({}, ctx), { deco: __assign(__assign({}, ctx.deco), (_a = {}, _a[type] = true, _a)) })));
|
|
212
229
|
break;
|
|
213
230
|
}
|
|
214
231
|
case "inlineCode":
|
|
@@ -216,10 +233,10 @@ function convertNodes(nodes, ctx, opts) {
|
|
|
216
233
|
results.push(buildText(node.value, ctx.deco));
|
|
217
234
|
break;
|
|
218
235
|
case "break":
|
|
219
|
-
results.push(buildBreak());
|
|
236
|
+
results.push(buildBreak(node));
|
|
220
237
|
break;
|
|
221
238
|
case "link":
|
|
222
|
-
results.push(buildLink(node, ctx
|
|
239
|
+
results.push(buildLink(node, ctx));
|
|
223
240
|
break;
|
|
224
241
|
case "image":
|
|
225
242
|
results.push(buildImage(node, ctx.images));
|
|
@@ -231,7 +248,7 @@ function convertNodes(nodes, ctx, opts) {
|
|
|
231
248
|
// FIXME: unimplemented
|
|
232
249
|
break;
|
|
233
250
|
case "footnote":
|
|
234
|
-
results.push(buildFootnote(node, ctx
|
|
251
|
+
results.push(buildFootnote(node, ctx));
|
|
235
252
|
break;
|
|
236
253
|
case "footnoteReference":
|
|
237
254
|
// FIXME: unimplemented
|
|
@@ -246,10 +263,10 @@ function convertNodes(nodes, ctx, opts) {
|
|
|
246
263
|
}
|
|
247
264
|
return results;
|
|
248
265
|
}
|
|
249
|
-
function buildParagraph(
|
|
250
|
-
|
|
266
|
+
function buildParagraph(_a, ctx) {
|
|
267
|
+
_a.type; var children = _a.children;
|
|
251
268
|
var list = ctx.list;
|
|
252
|
-
return new docx.Paragraph(__assign({ children: convertNodes(children, ctx
|
|
269
|
+
return new docx.Paragraph(__assign({ children: convertNodes(children, ctx) }, (list &&
|
|
253
270
|
(list.ordered
|
|
254
271
|
? {
|
|
255
272
|
numbering: {
|
|
@@ -263,8 +280,8 @@ function buildParagraph(node, ctx, opts) {
|
|
|
263
280
|
},
|
|
264
281
|
}))));
|
|
265
282
|
}
|
|
266
|
-
function buildHeading(
|
|
267
|
-
|
|
283
|
+
function buildHeading(_a, ctx) {
|
|
284
|
+
_a.type; var children = _a.children, depth = _a.depth;
|
|
268
285
|
var heading;
|
|
269
286
|
switch (depth) {
|
|
270
287
|
case 1:
|
|
@@ -288,35 +305,37 @@ function buildHeading(node, ctx, opts) {
|
|
|
288
305
|
}
|
|
289
306
|
return new docx.Paragraph({
|
|
290
307
|
heading: heading,
|
|
291
|
-
children: convertNodes(children, ctx
|
|
308
|
+
children: convertNodes(children, ctx),
|
|
292
309
|
});
|
|
293
310
|
}
|
|
294
|
-
function buildThematicBreak(
|
|
311
|
+
function buildThematicBreak(_a) {
|
|
312
|
+
_a.type;
|
|
295
313
|
return new docx.Paragraph({
|
|
296
314
|
thematicBreak: true,
|
|
297
315
|
});
|
|
298
316
|
}
|
|
299
|
-
function buildBlockquote(
|
|
317
|
+
function buildBlockquote(_a, ctx) {
|
|
318
|
+
_a.type; var children = _a.children;
|
|
300
319
|
// FIXME: do nothing for now
|
|
301
|
-
return convertNodes(
|
|
320
|
+
return convertNodes(children, ctx);
|
|
302
321
|
}
|
|
303
|
-
function buildList(
|
|
304
|
-
|
|
322
|
+
function buildList(_a, ctx) {
|
|
323
|
+
_a.type; var children = _a.children, ordered = _a.ordered; _a.start; _a.spread;
|
|
305
324
|
var list = {
|
|
306
325
|
level: ctx.list ? ctx.list.level + 1 : 0,
|
|
307
326
|
ordered: !!ordered,
|
|
308
327
|
};
|
|
309
328
|
return children.reduce(function (acc, item) {
|
|
310
|
-
acc.push.apply(acc, buildListItem(item, __assign(__assign({}, ctx), { list: list })
|
|
329
|
+
acc.push.apply(acc, buildListItem(item, __assign(__assign({}, ctx), { list: list })));
|
|
311
330
|
return acc;
|
|
312
331
|
}, []);
|
|
313
332
|
}
|
|
314
|
-
function buildListItem(
|
|
315
|
-
|
|
316
|
-
return convertNodes(children, ctx
|
|
333
|
+
function buildListItem(_a, ctx) {
|
|
334
|
+
_a.type; var children = _a.children; _a.checked; _a.spread;
|
|
335
|
+
return convertNodes(children, ctx);
|
|
317
336
|
}
|
|
318
|
-
function buildTable(
|
|
319
|
-
|
|
337
|
+
function buildTable(_a, ctx) {
|
|
338
|
+
_a.type; var children = _a.children, align = _a.align;
|
|
320
339
|
var cellAligns = align === null || align === void 0 ? void 0 : align.map(function (a) {
|
|
321
340
|
switch (a) {
|
|
322
341
|
case "left":
|
|
@@ -331,52 +350,52 @@ function buildTable(node, ctx, opts) {
|
|
|
331
350
|
});
|
|
332
351
|
return new docx.Table({
|
|
333
352
|
rows: children.map(function (r) {
|
|
334
|
-
return buildTableRow(r, ctx,
|
|
353
|
+
return buildTableRow(r, ctx, cellAligns);
|
|
335
354
|
}),
|
|
336
355
|
});
|
|
337
356
|
}
|
|
338
|
-
function buildTableRow(
|
|
339
|
-
|
|
357
|
+
function buildTableRow(_a, ctx, cellAligns) {
|
|
358
|
+
_a.type; var children = _a.children;
|
|
340
359
|
return new docx.TableRow({
|
|
341
360
|
children: children.map(function (c, i) {
|
|
342
|
-
return buildTableCell(c, ctx,
|
|
361
|
+
return buildTableCell(c, ctx, cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i]);
|
|
343
362
|
}),
|
|
344
363
|
});
|
|
345
364
|
}
|
|
346
|
-
function buildTableCell(
|
|
347
|
-
|
|
365
|
+
function buildTableCell(_a, ctx, align) {
|
|
366
|
+
_a.type; var children = _a.children;
|
|
348
367
|
return new docx.TableCell({
|
|
349
368
|
children: [
|
|
350
369
|
new docx.Paragraph({
|
|
351
370
|
alignment: align,
|
|
352
|
-
children: convertNodes(children, ctx
|
|
371
|
+
children: convertNodes(children, ctx),
|
|
353
372
|
}),
|
|
354
373
|
],
|
|
355
374
|
});
|
|
356
375
|
}
|
|
357
|
-
function buildHtml(
|
|
358
|
-
|
|
376
|
+
function buildHtml(_a) {
|
|
377
|
+
_a.type; var value = _a.value;
|
|
359
378
|
// FIXME: transform to text for now
|
|
360
379
|
return new docx.Paragraph({
|
|
361
380
|
children: [buildText(value, {})],
|
|
362
381
|
});
|
|
363
382
|
}
|
|
364
|
-
function buildCode(
|
|
365
|
-
|
|
383
|
+
function buildCode(_a) {
|
|
384
|
+
_a.type; var value = _a.value; _a.lang; _a.meta;
|
|
366
385
|
// FIXME: transform to text for now
|
|
367
386
|
return new docx.Paragraph({
|
|
368
387
|
children: [buildText(value, {})],
|
|
369
388
|
});
|
|
370
389
|
}
|
|
371
|
-
function buildMath(
|
|
372
|
-
|
|
390
|
+
function buildMath(_a) {
|
|
391
|
+
_a.type; var value = _a.value;
|
|
373
392
|
// FIXME: transform to text for now
|
|
374
393
|
return new docx.Paragraph({
|
|
375
394
|
children: [new docx.TextRun(value)],
|
|
376
395
|
});
|
|
377
396
|
}
|
|
378
|
-
function buildInlineMath(
|
|
379
|
-
|
|
397
|
+
function buildInlineMath(_a) {
|
|
398
|
+
_a.type; var value = _a.value;
|
|
380
399
|
// FIXME: transform to text for now
|
|
381
400
|
return new docx.TextRun(value);
|
|
382
401
|
}
|
|
@@ -388,22 +407,23 @@ function buildText(text, deco) {
|
|
|
388
407
|
strike: deco.delete,
|
|
389
408
|
});
|
|
390
409
|
}
|
|
391
|
-
function buildBreak(
|
|
410
|
+
function buildBreak(_a) {
|
|
411
|
+
_a.type;
|
|
392
412
|
return new docx.TextRun({ text: "", break: 1 });
|
|
393
413
|
}
|
|
394
|
-
function buildLink(
|
|
395
|
-
|
|
414
|
+
function buildLink(_a, ctx) {
|
|
415
|
+
_a.type; var children = _a.children, url = _a.url; _a.title;
|
|
396
416
|
return new docx.ExternalHyperlink({
|
|
397
417
|
link: url,
|
|
398
|
-
children: convertNodes(children, ctx
|
|
418
|
+
children: convertNodes(children, ctx),
|
|
399
419
|
});
|
|
400
420
|
}
|
|
401
|
-
function buildImage(
|
|
402
|
-
|
|
421
|
+
function buildImage(_a, images) {
|
|
422
|
+
_a.type; var url = _a.url; _a.title; _a.alt;
|
|
403
423
|
if (!images[url]) {
|
|
404
424
|
return error("Fetch image was failed: ".concat(url));
|
|
405
425
|
}
|
|
406
|
-
var
|
|
426
|
+
var _b = images[url], image = _b.image, width = _b.width, height = _b.height;
|
|
407
427
|
return new docx.ImageRun({
|
|
408
428
|
data: image,
|
|
409
429
|
transformation: {
|
|
@@ -412,11 +432,11 @@ function buildImage(node, images) {
|
|
|
412
432
|
},
|
|
413
433
|
});
|
|
414
434
|
}
|
|
415
|
-
function buildFootnote(
|
|
416
|
-
|
|
435
|
+
function buildFootnote(_a, ctx) {
|
|
436
|
+
_a.type; var children = _a.children;
|
|
417
437
|
// FIXME: transform to paragraph for now
|
|
418
438
|
return new docx.Paragraph({
|
|
419
|
-
children: convertNodes(children, ctx
|
|
439
|
+
children: convertNodes(children, ctx),
|
|
420
440
|
});
|
|
421
441
|
}
|
|
422
442
|
|
|
@@ -428,18 +448,21 @@ var plugin = function (opts) {
|
|
|
428
448
|
return mdastToDocx(node, opts, images);
|
|
429
449
|
};
|
|
430
450
|
return function (node) { return __awaiter(_this, void 0, void 0, function () {
|
|
431
|
-
var
|
|
451
|
+
var imageList, imageResolver, imageDatas;
|
|
432
452
|
return __generator(this, function (_a) {
|
|
433
453
|
switch (_a.label) {
|
|
434
454
|
case 0:
|
|
435
|
-
imageResolver = opts.imageResolver;
|
|
436
|
-
if (!imageResolver) {
|
|
437
|
-
throw new Error("options.imageResolver is not defined.");
|
|
438
|
-
}
|
|
439
455
|
imageList = [];
|
|
440
456
|
visit(node, "image", function (node) {
|
|
441
457
|
imageList.push(node);
|
|
442
458
|
});
|
|
459
|
+
if (imageList.length === 0) {
|
|
460
|
+
return [2 /*return*/, node];
|
|
461
|
+
}
|
|
462
|
+
imageResolver = opts.imageResolver;
|
|
463
|
+
if (!imageResolver) {
|
|
464
|
+
throw new Error("options.imageResolver is not defined.");
|
|
465
|
+
}
|
|
443
466
|
return [4 /*yield*/, Promise.all(imageList.map(function (_a) {
|
|
444
467
|
var url = _a.url;
|
|
445
468
|
return imageResolver(url);
|
package/lib/transformer.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export declare type ImageData = {
|
|
|
11
11
|
};
|
|
12
12
|
export declare type ImageResolver = (url: string) => Promise<ImageData> | ImageData;
|
|
13
13
|
export declare type Opts = {
|
|
14
|
-
|
|
14
|
+
output?: "buffer" | "blob" | "raw";
|
|
15
15
|
imageResolver?: ImageResolver;
|
|
16
|
-
}
|
|
17
|
-
export declare function mdastToDocx(node: mdast.Root,
|
|
16
|
+
} & Pick<IPropertiesOptions, "title" | "subject" | "creator" | "keywords" | "description" | "lastModifiedBy" | "revision" | "styles" | "background">;
|
|
17
|
+
export declare function mdastToDocx(node: mdast.Root, { output, title, subject, creator, keywords, description, lastModifiedBy, revision, styles, background, }: Opts, images: ImageDataMap): Promise<any> | docx.File;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "remark-docx",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "remark plugin to
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "remark plugin to compile markdown to docx.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "lib/index.mjs",
|
|
7
7
|
"types": "lib/index.d.ts",
|