remark-docx 0.1.9 → 0.2.0

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.js CHANGED
@@ -1,8 +1,6 @@
1
- 'use strict';
2
-
3
- var unistUtilVisit = require('unist-util-visit');
4
- var docx = require('docx');
5
- var unifiedLatexUtilParse = require('@unified-latex/unified-latex-util-parse');
1
+ import { visit } from 'unist-util-visit';
2
+ import { AlignmentType, LevelFormat, convertInchesToTwip, Document, Packer, Math, Paragraph, FootnoteReferenceRun, ImageRun, ExternalHyperlink, TextRun, Table, HeadingLevel, CheckBox, TableRow, TableCell, MathRun, MathRadical, MathFraction, MathSum, MathSubScript, MathSuperScript } from 'docx';
3
+ import { parseMath } from '@unified-latex/unified-latex-util-parse';
6
4
 
7
5
  /**
8
6
  * @internal
@@ -23,62 +21,62 @@ const INDENT = 0.5;
23
21
  const DEFAULT_NUMBERINGS = [
24
22
  {
25
23
  level: 0,
26
- format: docx.LevelFormat.DECIMAL,
24
+ format: LevelFormat.DECIMAL,
27
25
  text: "%1.",
28
- alignment: docx.AlignmentType.START,
26
+ alignment: AlignmentType.START,
29
27
  },
30
28
  {
31
29
  level: 1,
32
- format: docx.LevelFormat.DECIMAL,
30
+ format: LevelFormat.DECIMAL,
33
31
  text: "%2.",
34
- alignment: docx.AlignmentType.START,
32
+ alignment: AlignmentType.START,
35
33
  style: {
36
34
  paragraph: {
37
- indent: { start: docx.convertInchesToTwip(INDENT * 1) },
35
+ indent: { start: convertInchesToTwip(INDENT * 1) },
38
36
  },
39
37
  },
40
38
  },
41
39
  {
42
40
  level: 2,
43
- format: docx.LevelFormat.DECIMAL,
41
+ format: LevelFormat.DECIMAL,
44
42
  text: "%3.",
45
- alignment: docx.AlignmentType.START,
43
+ alignment: AlignmentType.START,
46
44
  style: {
47
45
  paragraph: {
48
- indent: { start: docx.convertInchesToTwip(INDENT * 2) },
46
+ indent: { start: convertInchesToTwip(INDENT * 2) },
49
47
  },
50
48
  },
51
49
  },
52
50
  {
53
51
  level: 3,
54
- format: docx.LevelFormat.DECIMAL,
52
+ format: LevelFormat.DECIMAL,
55
53
  text: "%4.",
56
- alignment: docx.AlignmentType.START,
54
+ alignment: AlignmentType.START,
57
55
  style: {
58
56
  paragraph: {
59
- indent: { start: docx.convertInchesToTwip(INDENT * 3) },
57
+ indent: { start: convertInchesToTwip(INDENT * 3) },
60
58
  },
61
59
  },
62
60
  },
63
61
  {
64
62
  level: 4,
65
- format: docx.LevelFormat.DECIMAL,
63
+ format: LevelFormat.DECIMAL,
66
64
  text: "%5.",
67
- alignment: docx.AlignmentType.START,
65
+ alignment: AlignmentType.START,
68
66
  style: {
69
67
  paragraph: {
70
- indent: { start: docx.convertInchesToTwip(INDENT * 4) },
68
+ indent: { start: convertInchesToTwip(INDENT * 4) },
71
69
  },
72
70
  },
73
71
  },
74
72
  {
75
73
  level: 5,
76
- format: docx.LevelFormat.DECIMAL,
74
+ format: LevelFormat.DECIMAL,
77
75
  text: "%6.",
78
- alignment: docx.AlignmentType.START,
76
+ alignment: AlignmentType.START,
79
77
  style: {
80
78
  paragraph: {
81
- indent: { start: docx.convertInchesToTwip(INDENT * 5) },
79
+ indent: { start: convertInchesToTwip(INDENT * 5) },
82
80
  },
83
81
  },
84
82
  },
@@ -125,7 +123,7 @@ const createNumberingRegistry = () => {
125
123
  };
126
124
  const mdastToDocx = async (node, { output = "buffer", title, subject, creator, keywords, description, lastModifiedBy, revision, styles, background, }, images, latex) => {
127
125
  const definition = {};
128
- unistUtilVisit.visit(node, "definition", (node) => {
126
+ visit(node, "definition", (node) => {
129
127
  definition[node.identifier] = node.url;
130
128
  });
131
129
  const footnote = createFootnoteRegistry();
@@ -139,7 +137,7 @@ const mdastToDocx = async (node, { output = "buffer", title, subject, creator, k
139
137
  numbering,
140
138
  latex,
141
139
  });
142
- const doc = new docx.Document({
140
+ const doc = new Document({
143
141
  title,
144
142
  subject,
145
143
  creator,
@@ -157,13 +155,11 @@ const mdastToDocx = async (node, { output = "buffer", title, subject, creator, k
157
155
  });
158
156
  switch (output) {
159
157
  case "buffer":
160
- const bufOut = await docx.Packer.toBuffer(doc);
161
- // feature detection instead of environment detection, but if Buffer exists
162
- // it's probably Node. If not, return the Uint8Array that JSZip returns
163
- // when it doesn't detect a Node environment.
164
- return typeof Buffer === "function" ? Buffer.from(bufOut) : bufOut;
158
+ return Packer.toBuffer(doc);
159
+ case "arrayBuffer":
160
+ return Packer.toArrayBuffer(doc);
165
161
  case "blob":
166
- return docx.Packer.toBlob(doc);
162
+ return Packer.toBlob(doc);
167
163
  }
168
164
  };
169
165
  const convertNodes = (nodes, ctx) => {
@@ -279,17 +275,17 @@ const buildParagraph = ({ children }, ctx) => {
279
275
  const list = ctx.list;
280
276
  const nodes = convertNodes(children, ctx);
281
277
  if (list && list.checked != null) {
282
- nodes.unshift(new docx.CheckBox({
278
+ nodes.unshift(new CheckBox({
283
279
  checked: list.checked,
284
280
  checkedState: { value: "2611" },
285
281
  uncheckedState: { value: "2610" },
286
282
  }));
287
283
  }
288
- return new docx.Paragraph({
284
+ return new Paragraph({
289
285
  children: nodes,
290
286
  indent: ctx.indent > 0
291
287
  ? {
292
- start: docx.convertInchesToTwip(INDENT * ctx.indent),
288
+ start: convertInchesToTwip(INDENT * ctx.indent),
293
289
  }
294
290
  : undefined,
295
291
  ...(list &&
@@ -311,32 +307,32 @@ const buildHeading = ({ children, depth }, ctx) => {
311
307
  let headingLevel;
312
308
  switch (depth) {
313
309
  case 1:
314
- headingLevel = docx.HeadingLevel.TITLE;
310
+ headingLevel = HeadingLevel.TITLE;
315
311
  break;
316
312
  case 2:
317
- headingLevel = docx.HeadingLevel.HEADING_1;
313
+ headingLevel = HeadingLevel.HEADING_1;
318
314
  break;
319
315
  case 3:
320
- headingLevel = docx.HeadingLevel.HEADING_2;
316
+ headingLevel = HeadingLevel.HEADING_2;
321
317
  break;
322
318
  case 4:
323
- headingLevel = docx.HeadingLevel.HEADING_3;
319
+ headingLevel = HeadingLevel.HEADING_3;
324
320
  break;
325
321
  case 5:
326
- headingLevel = docx.HeadingLevel.HEADING_4;
322
+ headingLevel = HeadingLevel.HEADING_4;
327
323
  break;
328
324
  case 6:
329
- headingLevel = docx.HeadingLevel.HEADING_5;
325
+ headingLevel = HeadingLevel.HEADING_5;
330
326
  break;
331
327
  }
332
328
  const nodes = convertNodes(children, ctx);
333
- return new docx.Paragraph({
329
+ return new Paragraph({
334
330
  heading: headingLevel,
335
331
  children: nodes,
336
332
  });
337
333
  };
338
334
  const buildThematicBreak = (_) => {
339
- return new docx.Paragraph({
335
+ return new Paragraph({
340
336
  thematicBreak: true,
341
337
  });
342
338
  };
@@ -373,23 +369,23 @@ const buildTable = ({ children, align }, ctx) => {
373
369
  const cellAligns = align === null || align === void 0 ? void 0 : align.map((a) => {
374
370
  switch (a) {
375
371
  case "left":
376
- return docx.AlignmentType.LEFT;
372
+ return AlignmentType.LEFT;
377
373
  case "right":
378
- return docx.AlignmentType.RIGHT;
374
+ return AlignmentType.RIGHT;
379
375
  case "center":
380
- return docx.AlignmentType.CENTER;
376
+ return AlignmentType.CENTER;
381
377
  default:
382
- return docx.AlignmentType.LEFT;
378
+ return AlignmentType.LEFT;
383
379
  }
384
380
  });
385
- return new docx.Table({
381
+ return new Table({
386
382
  rows: children.map((r) => {
387
383
  return buildTableRow(r, ctx, cellAligns);
388
384
  }),
389
385
  });
390
386
  };
391
387
  const buildTableRow = ({ children }, ctx, cellAligns) => {
392
- return new docx.TableRow({
388
+ return new TableRow({
393
389
  children: children.map((c, i) => {
394
390
  return buildTableCell(c, ctx, cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i]);
395
391
  }),
@@ -397,9 +393,9 @@ const buildTableRow = ({ children }, ctx, cellAligns) => {
397
393
  };
398
394
  const buildTableCell = ({ children }, ctx, align) => {
399
395
  const nodes = convertNodes(children, ctx);
400
- return new docx.TableCell({
396
+ return new TableCell({
401
397
  children: [
402
- new docx.Paragraph({
398
+ new Paragraph({
403
399
  alignment: align,
404
400
  children: nodes,
405
401
  }),
@@ -408,32 +404,32 @@ const buildTableCell = ({ children }, ctx, align) => {
408
404
  };
409
405
  const buildHtml = ({ value }) => {
410
406
  // FIXME: transform to text for now
411
- return new docx.Paragraph({
407
+ return new Paragraph({
412
408
  children: [buildText(value, {})],
413
409
  });
414
410
  };
415
411
  const buildCode = ({ value, lang: _lang, meta: _meta, }) => {
416
412
  // FIXME: transform to text for now
417
- return new docx.Paragraph({
413
+ return new Paragraph({
418
414
  children: [buildText(value, {})],
419
415
  });
420
416
  };
421
417
  const buildMath = ({ value }, ctx) => {
422
- return ctx.latex(value).map((runs) => new docx.Paragraph({
418
+ return ctx.latex(value).map((runs) => new Paragraph({
423
419
  children: [
424
- new docx.Math({
420
+ new Math({
425
421
  children: runs,
426
422
  }),
427
423
  ],
428
424
  }));
429
425
  };
430
426
  const buildInlineMath = ({ value }, ctx) => {
431
- return new docx.Math({
427
+ return new Math({
432
428
  children: ctx.latex(value).flatMap((runs) => runs),
433
429
  });
434
430
  };
435
431
  const buildText = (text, deco) => {
436
- return new docx.TextRun({
432
+ return new TextRun({
437
433
  text,
438
434
  bold: deco.strong,
439
435
  italics: deco.emphasis,
@@ -441,11 +437,11 @@ const buildText = (text, deco) => {
441
437
  });
442
438
  };
443
439
  const buildBreak = (_) => {
444
- return new docx.TextRun({ text: "", break: 1 });
440
+ return new TextRun({ text: "", break: 1 });
445
441
  };
446
442
  const buildLink = ({ children, url }, ctx) => {
447
443
  const nodes = convertNodes(children, ctx);
448
- return new docx.ExternalHyperlink({
444
+ return new ExternalHyperlink({
449
445
  link: url,
450
446
  children: nodes,
451
447
  });
@@ -454,7 +450,8 @@ const buildImage = ({ url }, images) => {
454
450
  const img = images[url];
455
451
  invariant(img, `Fetch image was failed: ${url}`);
456
452
  const { image, width, height } = img;
457
- return new docx.ImageRun({
453
+ return new ImageRun({
454
+ type: img.type,
458
455
  data: image,
459
456
  transformation: {
460
457
  width,
@@ -481,17 +478,17 @@ const registerFootnoteDefinition = ({ children, identifier }, ctx) => {
481
478
  children: children.map((node) => {
482
479
  // Convert each node and extract the first result as a paragraph
483
480
  const nodes = convertNodes([node], ctx);
484
- if (nodes[0] instanceof docx.Paragraph) {
481
+ if (nodes[0] instanceof Paragraph) {
485
482
  return nodes[0];
486
483
  }
487
484
  // For non-paragraph content, wrap in a paragraph
488
- return new docx.Paragraph({ children: nodes });
485
+ return new Paragraph({ children: nodes });
489
486
  }),
490
487
  };
491
488
  ctx.footnote.def(identifier, definition);
492
489
  };
493
490
  const buildFootnoteReference = ({ identifier }, ctx) => {
494
- return new docx.FootnoteReferenceRun(ctx.footnote.ref(identifier));
491
+ return new FootnoteReferenceRun(ctx.footnote.ref(identifier));
495
492
  };
496
493
 
497
494
  const hasSquareBrackets = (arg) => {
@@ -500,7 +497,7 @@ const hasSquareBrackets = (arg) => {
500
497
  const hasCurlyBrackets = (arg) => {
501
498
  return !!arg && arg.openMark === "{" && arg.closeMark === "}";
502
499
  };
503
- const mapString = (s) => new docx.MathRun(s);
500
+ const mapString = (s) => new MathRun(s);
504
501
  const mapMacro = (n, runs) => {
505
502
  var _a, _b, _c, _d, _e, _f, _g, _h;
506
503
  switch (n.content) {
@@ -710,7 +707,7 @@ const mapMacro = (n, runs) => {
710
707
  const prev = runs.pop();
711
708
  if (!prev)
712
709
  break;
713
- return new docx.MathSuperScript({
710
+ return new MathSuperScript({
714
711
  children: [prev],
715
712
  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 : []),
716
713
  });
@@ -719,7 +716,7 @@ const mapMacro = (n, runs) => {
719
716
  const prev = runs.pop();
720
717
  if (!prev)
721
718
  break;
722
- return new docx.MathSubScript({
719
+ return new MathSubScript({
723
720
  children: [prev],
724
721
  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 : []),
725
722
  });
@@ -732,7 +729,7 @@ const mapMacro = (n, runs) => {
732
729
  break;
733
730
  case "sum": {
734
731
  // TODO: support superscript and subscript
735
- return new docx.MathSum({
732
+ return new MathSum({
736
733
  children: [],
737
734
  });
738
735
  }
@@ -745,7 +742,7 @@ const mapMacro = (n, runs) => {
745
742
  if (args.length === 2 &&
746
743
  hasCurlyBrackets(args[0]) &&
747
744
  hasCurlyBrackets(args[1])) {
748
- return new docx.MathFraction({
745
+ return new MathFraction({
749
746
  numerator: mapGroup(args[0].content),
750
747
  denominator: mapGroup(args[1].content),
751
748
  });
@@ -755,14 +752,14 @@ const mapMacro = (n, runs) => {
755
752
  case "sqrt": {
756
753
  const args = (_h = n.args) !== null && _h !== void 0 ? _h : [];
757
754
  if (args.length === 1 && hasCurlyBrackets(args[0])) {
758
- return new docx.MathRadical({
755
+ return new MathRadical({
759
756
  children: mapGroup(args[0].content),
760
757
  });
761
758
  }
762
759
  if (args.length === 2 &&
763
760
  hasSquareBrackets(args[0]) &&
764
761
  hasCurlyBrackets(args[1])) {
765
- return new docx.MathRadical({
762
+ return new MathRadical({
766
763
  children: mapGroup(args[1].content),
767
764
  degree: mapGroup(args[0].content),
768
765
  });
@@ -822,7 +819,7 @@ const mapNode = (n, runs) => {
822
819
  * @internal
823
820
  */
824
821
  const parseLatex = (value) => {
825
- const parsed = unifiedLatexUtilParse.parseMath(value);
822
+ const parsed = parseMath(value);
826
823
  const paragraphs = [[]];
827
824
  let runs = paragraphs[0];
828
825
  for (const n of parsed) {
@@ -845,14 +842,14 @@ const plugin = function (opts = {}) {
845
842
  };
846
843
  return async (node) => {
847
844
  const imageList = [];
848
- unistUtilVisit.visit(node, "image", (node) => {
845
+ visit(node, "image", (node) => {
849
846
  imageList.push(node);
850
847
  });
851
848
  const defs = new Map();
852
- unistUtilVisit.visit(node, "definition", (node) => {
849
+ visit(node, "definition", (node) => {
853
850
  defs.set(node.identifier, node);
854
851
  });
855
- unistUtilVisit.visit(node, "imageReference", (node) => {
852
+ visit(node, "imageReference", (node) => {
856
853
  const maybeImage = defs.get(node.identifier);
857
854
  if (maybeImage) {
858
855
  imageList.push(maybeImage);
@@ -882,5 +879,5 @@ const plugin = function (opts = {}) {
882
879
  };
883
880
  };
884
881
 
885
- module.exports = plugin;
882
+ export { plugin as default };
886
883
  //# sourceMappingURL=index.js.map