remark-docx 0.2.1 → 0.3.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/lib/index.cjs CHANGED
@@ -1,88 +1,27 @@
1
1
  'use strict';
2
2
 
3
- var unistUtilVisit = require('unist-util-visit');
4
3
  var docx = require('docx');
5
- var unifiedLatexUtilParse = require('@unified-latex/unified-latex-util-parse');
4
+ var mdastUtilDefinitions = require('mdast-util-definitions');
6
5
 
7
6
  /**
8
7
  * @internal
9
8
  */
10
- const unreachable = (_) => {
11
- throw new Error("unreachable");
12
- };
9
+ function invariant(cond, message) {
10
+ throw new Error(message);
11
+ }
12
+ const alreadyWarned = {};
13
13
  /**
14
14
  * @internal
15
15
  */
16
- function invariant(cond, message) {
17
- if (!cond)
18
- throw new Error(message);
16
+ function warnOnce(message, cond = false) {
17
+ if (!cond && !alreadyWarned[message]) {
18
+ alreadyWarned[message] = true;
19
+ console.warn(message);
20
+ }
19
21
  }
20
22
 
21
23
  const ORDERED_LIST_REF = "ordered";
22
24
  const INDENT = 0.5;
23
- const DEFAULT_NUMBERINGS = [
24
- {
25
- level: 0,
26
- format: docx.LevelFormat.DECIMAL,
27
- text: "%1.",
28
- alignment: docx.AlignmentType.START,
29
- },
30
- {
31
- level: 1,
32
- format: docx.LevelFormat.DECIMAL,
33
- text: "%2.",
34
- alignment: docx.AlignmentType.START,
35
- style: {
36
- paragraph: {
37
- indent: { start: docx.convertInchesToTwip(INDENT * 1) },
38
- },
39
- },
40
- },
41
- {
42
- level: 2,
43
- format: docx.LevelFormat.DECIMAL,
44
- text: "%3.",
45
- alignment: docx.AlignmentType.START,
46
- style: {
47
- paragraph: {
48
- indent: { start: docx.convertInchesToTwip(INDENT * 2) },
49
- },
50
- },
51
- },
52
- {
53
- level: 3,
54
- format: docx.LevelFormat.DECIMAL,
55
- text: "%4.",
56
- alignment: docx.AlignmentType.START,
57
- style: {
58
- paragraph: {
59
- indent: { start: docx.convertInchesToTwip(INDENT * 3) },
60
- },
61
- },
62
- },
63
- {
64
- level: 4,
65
- format: docx.LevelFormat.DECIMAL,
66
- text: "%5.",
67
- alignment: docx.AlignmentType.START,
68
- style: {
69
- paragraph: {
70
- indent: { start: docx.convertInchesToTwip(INDENT * 4) },
71
- },
72
- },
73
- },
74
- {
75
- level: 5,
76
- format: docx.LevelFormat.DECIMAL,
77
- text: "%6.",
78
- alignment: docx.AlignmentType.START,
79
- style: {
80
- paragraph: {
81
- indent: { start: docx.convertInchesToTwip(INDENT * 5) },
82
- },
83
- },
84
- },
85
- ];
86
25
  const createFootnoteRegistry = () => {
87
26
  const idToInternalId = new Map();
88
27
  const defs = new Map();
@@ -111,11 +50,74 @@ const createFootnoteRegistry = () => {
111
50
  };
112
51
  const createNumberingRegistry = () => {
113
52
  let counter = 1;
53
+ const DEFAULT_NUMBERINGS = [
54
+ {
55
+ level: 0,
56
+ format: docx.LevelFormat.DECIMAL,
57
+ text: "%1.",
58
+ alignment: docx.AlignmentType.START,
59
+ },
60
+ {
61
+ level: 1,
62
+ format: docx.LevelFormat.DECIMAL,
63
+ text: "%2.",
64
+ alignment: docx.AlignmentType.START,
65
+ style: {
66
+ paragraph: {
67
+ indent: { start: docx.convertInchesToTwip(INDENT * 1) },
68
+ },
69
+ },
70
+ },
71
+ {
72
+ level: 2,
73
+ format: docx.LevelFormat.DECIMAL,
74
+ text: "%3.",
75
+ alignment: docx.AlignmentType.START,
76
+ style: {
77
+ paragraph: {
78
+ indent: { start: docx.convertInchesToTwip(INDENT * 2) },
79
+ },
80
+ },
81
+ },
82
+ {
83
+ level: 3,
84
+ format: docx.LevelFormat.DECIMAL,
85
+ text: "%4.",
86
+ alignment: docx.AlignmentType.START,
87
+ style: {
88
+ paragraph: {
89
+ indent: { start: docx.convertInchesToTwip(INDENT * 3) },
90
+ },
91
+ },
92
+ },
93
+ {
94
+ level: 4,
95
+ format: docx.LevelFormat.DECIMAL,
96
+ text: "%5.",
97
+ alignment: docx.AlignmentType.START,
98
+ style: {
99
+ paragraph: {
100
+ indent: { start: docx.convertInchesToTwip(INDENT * 4) },
101
+ },
102
+ },
103
+ },
104
+ {
105
+ level: 5,
106
+ format: docx.LevelFormat.DECIMAL,
107
+ text: "%6.",
108
+ alignment: docx.AlignmentType.START,
109
+ style: {
110
+ paragraph: {
111
+ indent: { start: docx.convertInchesToTwip(INDENT * 5) },
112
+ },
113
+ },
114
+ },
115
+ ];
114
116
  return {
115
117
  create: () => {
116
118
  return `${ORDERED_LIST_REF}-${counter++}`;
117
119
  },
118
- getAll: () => {
120
+ numberings: () => {
119
121
  return Array.from({ length: counter }, (_, i) => ({
120
122
  reference: `${ORDERED_LIST_REF}-${i}`,
121
123
  levels: DEFAULT_NUMBERINGS,
@@ -123,21 +125,18 @@ const createNumberingRegistry = () => {
123
125
  },
124
126
  };
125
127
  };
126
- const mdastToDocx = async (node, { output = "buffer", title, subject, creator, keywords, description, lastModifiedBy, revision, styles, background, }, images, latex) => {
127
- const definition = {};
128
- unistUtilVisit.visit(node, "definition", (node) => {
129
- definition[node.identifier] = node.url;
130
- });
128
+ const mdastToDocx = async (node, { plugins = [], title, subject, creator, keywords, description, lastModifiedBy, revision, styles, background, }) => {
129
+ const definition = mdastUtilDefinitions.definitions(node);
131
130
  const footnote = createFootnoteRegistry();
132
131
  const numbering = createNumberingRegistry();
132
+ const pluginCtx = { root: node, definition };
133
133
  const nodes = convertNodes(node.children, {
134
+ overrides: (await Promise.all(plugins.map((p) => p(pluginCtx)))).reduceRight((acc, p) => ({ acc, ...p }), {}),
134
135
  deco: {},
135
- images,
136
136
  indent: 0,
137
- def: definition,
137
+ definition: definition,
138
138
  footnote,
139
139
  numbering,
140
- latex,
141
140
  });
142
141
  const doc = new docx.Document({
143
142
  title,
@@ -152,21 +151,25 @@ const mdastToDocx = async (node, { output = "buffer", title, subject, creator, k
152
151
  footnotes: footnote.footnotes(),
153
152
  sections: [{ children: nodes }],
154
153
  numbering: {
155
- config: numbering.getAll(),
154
+ config: numbering.numberings(),
156
155
  },
157
156
  });
158
- switch (output) {
159
- case "buffer":
160
- return docx.Packer.toBuffer(doc);
161
- case "arrayBuffer":
162
- return docx.Packer.toArrayBuffer(doc);
163
- case "blob":
164
- return docx.Packer.toBlob(doc);
165
- }
157
+ return docx.Packer.toArrayBuffer(doc);
166
158
  };
167
159
  const convertNodes = (nodes, ctx) => {
160
+ var _a, _b;
168
161
  const results = [];
169
162
  for (const node of nodes) {
163
+ const customNodes = (_b = (_a = ctx.overrides)[node.type]) === null || _b === void 0 ? void 0 : _b.call(_a, node, (children) => convertNodes(children, ctx));
164
+ if (customNodes != null) {
165
+ if (Array.isArray(customNodes)) {
166
+ results.push(...customNodes);
167
+ }
168
+ else {
169
+ results.push(customNodes);
170
+ }
171
+ continue;
172
+ }
170
173
  switch (node.type) {
171
174
  case "paragraph": {
172
175
  results.push(buildParagraph(node, ctx));
@@ -202,12 +205,12 @@ const convertNodes = (nodes, ctx) => {
202
205
  case "code":
203
206
  results.push(buildCode(node));
204
207
  break;
205
- case "yaml":
206
- // FIXME: unimplemented
207
- break;
208
- case "toml":
209
- // FIXME: unimplemented
210
- break;
208
+ // case "yaml":
209
+ // // unimplemented
210
+ // break;
211
+ // case "toml":
212
+ // // unimplemented
213
+ // break;
211
214
  case "definition":
212
215
  // noop
213
216
  break;
@@ -240,34 +243,27 @@ const convertNodes = (nodes, ctx) => {
240
243
  results.push(buildLink(node, ctx));
241
244
  break;
242
245
  }
243
- case "image":
244
- results.push(buildImage(node, ctx.images));
245
- break;
246
246
  case "linkReference":
247
247
  results.push(...buildLinkReference(node, ctx));
248
248
  break;
249
+ case "image":
249
250
  case "imageReference": {
250
- const image = buildImageReference(node, ctx);
251
- if (image) {
252
- results.push(image);
253
- }
254
- break;
255
- }
256
- case "footnote": {
257
- // inline footnote was removed in mdast v5
251
+ warnOnce(`${node.type} node is not rendered since remark-docx/plugins/image is not provided.`);
258
252
  break;
259
253
  }
254
+ // case "footnote": {
255
+ // // inline footnote was removed in mdast v5
256
+ // break;
257
+ // }
260
258
  case "footnoteReference":
261
259
  results.push(buildFootnoteReference(node, ctx));
262
260
  break;
263
261
  case "math":
264
- results.push(...buildMath(node, ctx));
265
- break;
266
262
  case "inlineMath":
267
- results.push(buildInlineMath(node, ctx));
263
+ warnOnce(`${node.type} node is not rendered since remark-docx/plugins/math is not provided.`);
268
264
  break;
269
265
  default:
270
- unreachable();
266
+ warnOnce(`${node.type} node is not officially supported.`);
271
267
  break;
272
268
  }
273
269
  }
@@ -344,7 +340,7 @@ const buildBlockquote = ({ children }, ctx) => {
344
340
  indent: ctx.indent + 1,
345
341
  });
346
342
  };
347
- const buildList = ({ children, ordered, start: _start, spread: _spread }, ctx) => {
343
+ const buildList = ({ children, ordered }, ctx) => {
348
344
  var _a;
349
345
  const isTopLevel = !ctx.list;
350
346
  const list = {
@@ -361,7 +357,7 @@ const buildList = ({ children, ordered, start: _start, spread: _spread }, ctx) =
361
357
  });
362
358
  });
363
359
  };
364
- const buildListItem = ({ children, checked, spread: _spread }, ctx) => {
360
+ const buildListItem = ({ children, checked }, ctx) => {
365
361
  return convertNodes(children, {
366
362
  ...ctx,
367
363
  ...(ctx.list && { list: { ...ctx.list, checked: checked !== null && checked !== void 0 ? checked : undefined } }),
@@ -382,28 +378,21 @@ const buildTable = ({ children, align }, ctx) => {
382
378
  });
383
379
  return new docx.Table({
384
380
  rows: children.map((r) => {
385
- return buildTableRow(r, ctx, cellAligns);
386
- }),
387
- });
388
- };
389
- const buildTableRow = ({ children }, ctx, cellAligns) => {
390
- return new docx.TableRow({
391
- children: children.map((c, i) => {
392
- return buildTableCell(c, ctx, cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i]);
381
+ return new docx.TableRow({
382
+ children: r.children.map((c, i) => {
383
+ return new docx.TableCell({
384
+ children: [
385
+ new docx.Paragraph({
386
+ alignment: cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i],
387
+ children: convertNodes(c.children, ctx),
388
+ }),
389
+ ],
390
+ });
391
+ }),
392
+ });
393
393
  }),
394
394
  });
395
395
  };
396
- const buildTableCell = ({ children }, ctx, align) => {
397
- const nodes = convertNodes(children, ctx);
398
- return new docx.TableCell({
399
- children: [
400
- new docx.Paragraph({
401
- alignment: align,
402
- children: nodes,
403
- }),
404
- ],
405
- });
406
- };
407
396
  const buildHtml = ({ value }) => {
408
397
  // FIXME: transform to text for now
409
398
  return new docx.Paragraph({
@@ -416,20 +405,6 @@ const buildCode = ({ value, lang: _lang, meta: _meta, }) => {
416
405
  children: [buildText(value, {})],
417
406
  });
418
407
  };
419
- const buildMath = ({ value }, ctx) => {
420
- return ctx.latex(value).map((runs) => new docx.Paragraph({
421
- children: [
422
- new docx.Math({
423
- children: runs,
424
- }),
425
- ],
426
- }));
427
- };
428
- const buildInlineMath = ({ value }, ctx) => {
429
- return new docx.Math({
430
- children: ctx.latex(value).flatMap((runs) => runs),
431
- });
432
- };
433
408
  const buildText = (text, deco) => {
434
409
  return new docx.TextRun({
435
410
  text,
@@ -448,32 +423,12 @@ const buildLink = ({ children, url }, ctx) => {
448
423
  children: nodes,
449
424
  });
450
425
  };
451
- const buildImage = ({ url }, images) => {
452
- const img = images[url];
453
- invariant(img, `Fetch image was failed: ${url}`);
454
- const { image, width, height } = img;
455
- return new docx.ImageRun({
456
- type: img.type,
457
- data: image,
458
- transformation: {
459
- width,
460
- height,
461
- },
462
- });
463
- };
464
426
  const buildLinkReference = ({ children, identifier }, ctx) => {
465
- const def = ctx.def[identifier];
427
+ const def = ctx.definition(identifier);
466
428
  if (def == null) {
467
429
  return convertNodes(children, ctx);
468
430
  }
469
- return [buildLink({ children, url: def }, ctx)];
470
- };
471
- const buildImageReference = ({ identifier }, ctx) => {
472
- const def = ctx.def[identifier];
473
- if (def == null) {
474
- return;
475
- }
476
- return buildImage({ url: def }, ctx.images);
431
+ return [buildLink({ children, url: def.url }, ctx)];
477
432
  };
478
433
  const registerFootnoteDefinition = ({ children, identifier }, ctx) => {
479
434
  const definition = {
@@ -493,391 +448,9 @@ const buildFootnoteReference = ({ identifier }, ctx) => {
493
448
  return new docx.FootnoteReferenceRun(ctx.footnote.ref(identifier));
494
449
  };
495
450
 
496
- const hasSquareBrackets = (arg) => {
497
- return !!arg && arg.openMark === "[" && arg.closeMark === "]";
498
- };
499
- const hasCurlyBrackets = (arg) => {
500
- return !!arg && arg.openMark === "{" && arg.closeMark === "}";
501
- };
502
- const mapString = (s) => new docx.MathRun(s);
503
- const mapMacro = (n, runs) => {
504
- var _a, _b, _c, _d, _e, _f, _g, _h;
505
- switch (n.content) {
506
- case "#":
507
- return mapString("#");
508
- case "$":
509
- return mapString("$");
510
- case "%":
511
- return mapString("%");
512
- case "&":
513
- return mapString("&");
514
- case "textasciitilde":
515
- return mapString("~");
516
- case "textasciicircum":
517
- return mapString("^");
518
- case "textbackslash":
519
- return mapString("∖");
520
- case "{":
521
- return mapString("{");
522
- case "}":
523
- return mapString("}");
524
- case "textbar":
525
- return mapString("|");
526
- case "textless":
527
- return mapString("<");
528
- case "textgreater":
529
- return mapString(">");
530
- case "neq":
531
- return mapString("≠");
532
- case "sim":
533
- return mapString("∼");
534
- case "simeq":
535
- return mapString("≃");
536
- case "approx":
537
- return mapString("≈");
538
- case "fallingdotseq":
539
- return mapString("≒");
540
- case "risingdotseq":
541
- return mapString("≓");
542
- case "equiv":
543
- return mapString("≡");
544
- case "geq":
545
- return mapString("≥");
546
- case "geqq":
547
- return mapString("≧");
548
- case "leq":
549
- return mapString("≤");
550
- case "leqq":
551
- return mapString("≦");
552
- case "gg":
553
- return mapString("≫");
554
- case "ll":
555
- return mapString("≪");
556
- case "times":
557
- return mapString("×");
558
- case "div":
559
- return mapString("÷");
560
- case "pm":
561
- return mapString("±");
562
- case "mp":
563
- return mapString("∓");
564
- case "oplus":
565
- return mapString("⊕");
566
- case "ominus":
567
- return mapString("⊖");
568
- case "otimes":
569
- return mapString("⊗");
570
- case "oslash":
571
- return mapString("⊘");
572
- case "circ":
573
- return mapString("∘");
574
- case "cdot":
575
- return mapString("⋅");
576
- case "bullet":
577
- return mapString("∙");
578
- case "ltimes":
579
- return mapString("⋉");
580
- case "rtimes":
581
- return mapString("⋊");
582
- case "in":
583
- return mapString("∈");
584
- case "ni":
585
- return mapString("∋");
586
- case "notin":
587
- return mapString("∉");
588
- case "subset":
589
- return mapString("⊂");
590
- case "supset":
591
- return mapString("⊃");
592
- case "subseteq":
593
- return mapString("⊆");
594
- case "supseteq":
595
- return mapString("⊇");
596
- case "nsubseteq":
597
- return mapString("⊈");
598
- case "nsupseteq":
599
- return mapString("⊉");
600
- case "subsetneq":
601
- return mapString("⊊");
602
- case "supsetneq":
603
- return mapString("⊋");
604
- case "cap":
605
- return mapString("∩");
606
- case "cup":
607
- return mapString("∪");
608
- case "emptyset":
609
- return mapString("∅");
610
- case "infty":
611
- return mapString("∞");
612
- case "partial":
613
- return mapString("∂");
614
- case "aleph":
615
- return mapString("ℵ");
616
- case "hbar":
617
- return mapString("ℏ");
618
- case "wp":
619
- return mapString("℘");
620
- case "Re":
621
- return mapString("ℜ");
622
- case "Im":
623
- return mapString("ℑ");
624
- case "alpha":
625
- return mapString("α");
626
- case "beta":
627
- return mapString("β");
628
- case "gamma":
629
- return mapString("γ");
630
- case "delta":
631
- return mapString("δ");
632
- case "epsilon":
633
- return mapString("ϵ");
634
- case "zeta":
635
- return mapString("ζ");
636
- case "eta":
637
- return mapString("η");
638
- case "theta":
639
- return mapString("θ");
640
- case "iota":
641
- return mapString("ι");
642
- case "kappa":
643
- return mapString("κ");
644
- case "lambda":
645
- return mapString("λ");
646
- case "eta":
647
- return mapString("η");
648
- case "mu":
649
- return mapString("μ");
650
- case "nu":
651
- return mapString("ν");
652
- case "xi":
653
- return mapString("ξ");
654
- case "pi":
655
- return mapString("π");
656
- case "rho":
657
- return mapString("ρ");
658
- case "sigma":
659
- return mapString("σ");
660
- case "tau":
661
- return mapString("τ");
662
- case "upsilon":
663
- return mapString("υ");
664
- case "phi":
665
- return mapString("ϕ");
666
- case "chi":
667
- return mapString("χ");
668
- case "psi":
669
- return mapString("ψ");
670
- case "omega":
671
- return mapString("ω");
672
- case "varepsilon":
673
- return mapString("ε");
674
- case "vartheta":
675
- return mapString("ϑ");
676
- case "varrho":
677
- return mapString("ϱ");
678
- case "varsigma":
679
- return mapString("ς");
680
- case "varphi":
681
- return mapString("φ");
682
- case "Gamma":
683
- return mapString("Γ");
684
- case "Delta":
685
- return mapString("Δ");
686
- case "Theta":
687
- return mapString("Θ");
688
- case "Lambda":
689
- return mapString("Λ");
690
- case "Xi":
691
- return mapString("Ξ");
692
- case "Pi":
693
- return mapString("Π");
694
- case "Sigma":
695
- return mapString("Σ");
696
- case "Upsilon":
697
- return mapString("Υ");
698
- case "Phi":
699
- return mapString("Φ");
700
- case "Psi":
701
- return mapString("Ψ");
702
- case "Omega":
703
- return mapString("Ω");
704
- case "newline":
705
- case "\\":
706
- // line break
707
- return false;
708
- case "^": {
709
- const prev = runs.pop();
710
- if (!prev)
711
- break;
712
- return new docx.MathSuperScript({
713
- children: [prev],
714
- superScript: mapGroup((_c = (_b = (_a = n.args) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.content) !== null && _c !== void 0 ? _c : []),
715
- });
716
- }
717
- case "_": {
718
- const prev = runs.pop();
719
- if (!prev)
720
- break;
721
- return new docx.MathSubScript({
722
- children: [prev],
723
- subScript: mapGroup((_f = (_e = (_d = n.args) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.content) !== null && _f !== void 0 ? _f : []),
724
- });
725
- }
726
- case "hat":
727
- // TODO: implement
728
- break;
729
- case "widehat":
730
- // TODO: implement
731
- break;
732
- case "sum": {
733
- // TODO: support superscript and subscript
734
- return new docx.MathSum({
735
- children: [],
736
- });
737
- }
738
- case "int":
739
- return mapString("∫");
740
- case "frac":
741
- case "tfrac":
742
- case "dfrac": {
743
- const args = (_g = n.args) !== null && _g !== void 0 ? _g : [];
744
- if (args.length === 2 &&
745
- hasCurlyBrackets(args[0]) &&
746
- hasCurlyBrackets(args[1])) {
747
- return new docx.MathFraction({
748
- numerator: mapGroup(args[0].content),
749
- denominator: mapGroup(args[1].content),
750
- });
751
- }
752
- break;
753
- }
754
- case "sqrt": {
755
- const args = (_h = n.args) !== null && _h !== void 0 ? _h : [];
756
- if (args.length === 1 && hasCurlyBrackets(args[0])) {
757
- return new docx.MathRadical({
758
- children: mapGroup(args[0].content),
759
- });
760
- }
761
- if (args.length === 2 &&
762
- hasSquareBrackets(args[0]) &&
763
- hasCurlyBrackets(args[1])) {
764
- return new docx.MathRadical({
765
- children: mapGroup(args[1].content),
766
- degree: mapGroup(args[0].content),
767
- });
768
- }
769
- break;
770
- }
771
- }
772
- return mapString(n.content);
773
- };
774
- const mapGroup = (nodes) => {
775
- const group = [];
776
- for (const c of nodes) {
777
- group.push(...(mapNode(c, group) || []));
778
- }
779
- return group;
780
- };
781
- const mapNode = (n, runs) => {
782
- switch (n.type) {
783
- case "root":
784
- break;
785
- case "string":
786
- return [mapString(n.content)];
787
- case "whitespace":
788
- break;
789
- case "parbreak":
790
- break;
791
- case "comment":
792
- break;
793
- case "macro":
794
- const run = mapMacro(n, runs);
795
- if (!run) {
796
- // line break
797
- return false;
798
- }
799
- else {
800
- return [run];
801
- }
802
- case "environment":
803
- case "mathenv":
804
- break;
805
- case "verbatim":
806
- break;
807
- case "inlinemath":
808
- break;
809
- case "displaymath":
810
- break;
811
- case "group":
812
- return mapGroup(n.content);
813
- case "verb":
814
- break;
815
- default:
816
- unreachable();
817
- }
818
- return [];
819
- };
820
- /**
821
- * @internal
822
- */
823
- const parseLatex = (value) => {
824
- const parsed = unifiedLatexUtilParse.parseMath(value);
825
- const paragraphs = [[]];
826
- let runs = paragraphs[0];
827
- for (const n of parsed) {
828
- const res = mapNode(n, runs);
829
- if (!res) {
830
- // line break
831
- paragraphs.push((runs = []));
832
- }
833
- else {
834
- runs.push(...res);
835
- }
836
- }
837
- return paragraphs;
838
- };
839
-
840
451
  const plugin = function (opts = {}) {
841
- let images = {};
842
- this.Compiler = (node) => {
843
- return mdastToDocx(node, opts, images, parseLatex);
844
- };
845
- return async (node) => {
846
- const imageList = [];
847
- unistUtilVisit.visit(node, "image", (node) => {
848
- imageList.push(node);
849
- });
850
- const defs = new Map();
851
- unistUtilVisit.visit(node, "definition", (node) => {
852
- defs.set(node.identifier, node);
853
- });
854
- unistUtilVisit.visit(node, "imageReference", (node) => {
855
- const maybeImage = defs.get(node.identifier);
856
- if (maybeImage) {
857
- imageList.push(maybeImage);
858
- }
859
- });
860
- if (imageList.length === 0) {
861
- return node;
862
- }
863
- const imageResolver = opts.imageResolver;
864
- invariant(imageResolver, "options.imageResolver is not defined.");
865
- const resolved = new Set();
866
- const promises = [];
867
- imageList.forEach(({ url }) => {
868
- if (!resolved.has(url)) {
869
- resolved.add(url);
870
- promises.push((async () => {
871
- const img = await imageResolver(url);
872
- return { img, url };
873
- })());
874
- }
875
- });
876
- images = (await Promise.all(promises)).reduce((acc, { img, url }) => {
877
- acc[url] = img;
878
- return acc;
879
- }, {});
880
- return node;
452
+ this.compiler = (node) => {
453
+ return mdastToDocx(node, opts);
881
454
  };
882
455
  };
883
456