remark-docx 0.0.7 → 0.1.2

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,7 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ var tslib = require('tslib');
3
4
  var unistUtilVisit = require('unist-util-visit');
4
5
  var docx = require('docx');
6
+ var unifiedLatexUtilParse = require('@unified-latex/unified-latex-util-parse');
5
7
 
6
8
  function _interopNamespace(e) {
7
9
  if (e && e.__esModule) return e;
@@ -23,73 +25,358 @@ function _interopNamespace(e) {
23
25
 
24
26
  var docx__namespace = /*#__PURE__*/_interopNamespace(docx);
25
27
 
26
- /*! *****************************************************************************
27
- Copyright (c) Microsoft Corporation.
28
-
29
- Permission to use, copy, modify, and/or distribute this software for any
30
- purpose with or without fee is hereby granted.
31
-
32
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
33
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
34
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
35
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
36
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
37
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
38
- PERFORMANCE OF THIS SOFTWARE.
39
- ***************************************************************************** */
40
-
41
- var __assign = function() {
42
- __assign = Object.assign || function __assign(t) {
43
- for (var s, i = 1, n = arguments.length; i < n; i++) {
44
- s = arguments[i];
45
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
46
- }
47
- return t;
48
- };
49
- return __assign.apply(this, arguments);
50
- };
51
-
52
- function __awaiter(thisArg, _arguments, P, generator) {
53
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
54
- return new (P || (P = Promise))(function (resolve, reject) {
55
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
56
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
57
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
58
- step((generator = generator.apply(thisArg, _arguments || [])).next());
59
- });
60
- }
61
-
62
- function __generator(thisArg, body) {
63
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
64
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
65
- function verb(n) { return function (v) { return step([n, v]); }; }
66
- function step(op) {
67
- if (f) throw new TypeError("Generator is already executing.");
68
- while (_) try {
69
- 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;
70
- if (y = 0, t) op = [op[0] & 2, t.value];
71
- switch (op[0]) {
72
- case 0: case 1: t = op; break;
73
- case 4: _.label++; return { value: op[1], done: false };
74
- case 5: _.label++; y = op[1]; op = [0]; continue;
75
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
76
- default:
77
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
78
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
79
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
80
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
81
- if (t[2]) _.ops.pop();
82
- _.trys.pop(); continue;
83
- }
84
- op = body.call(thisArg, _);
85
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
86
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
87
- }
28
+ const unreachable = (_) => {
29
+ throw new Error("unreachable");
30
+ };
31
+ function invariant(cond, message) {
32
+ if (!cond)
33
+ throw new Error(message);
88
34
  }
89
35
 
90
- var ORDERED_LIST_REF = "ordered";
91
- var INDENT = 0.5;
92
- var DEFAULT_NUMBERINGS = [
36
+ const hasSquareBrackets = (arg) => {
37
+ return !!arg && arg.openMark === "[" && arg.closeMark === "]";
38
+ };
39
+ const hasCurlyBrackets = (arg) => {
40
+ return !!arg && arg.openMark === "{" && arg.closeMark === "}";
41
+ };
42
+ const mapString = (s) => new docx__namespace.MathRun(s);
43
+ const mapMacro = (n, runs) => {
44
+ var _a, _b, _c, _d, _e, _f, _g, _h;
45
+ switch (n.content) {
46
+ case "#":
47
+ return mapString("#");
48
+ case "$":
49
+ return mapString("$");
50
+ case "%":
51
+ return mapString("%");
52
+ case "&":
53
+ return mapString("&");
54
+ case "textasciitilde":
55
+ return mapString("~");
56
+ case "textasciicircum":
57
+ return mapString("^");
58
+ case "textbackslash":
59
+ return mapString("∖");
60
+ case "{":
61
+ return mapString("{");
62
+ case "}":
63
+ return mapString("}");
64
+ case "textbar":
65
+ return mapString("|");
66
+ case "textless":
67
+ return mapString("<");
68
+ case "textgreater":
69
+ return mapString(">");
70
+ case "neq":
71
+ return mapString("≠");
72
+ case "sim":
73
+ return mapString("∼");
74
+ case "simeq":
75
+ return mapString("≃");
76
+ case "approx":
77
+ return mapString("≈");
78
+ case "fallingdotseq":
79
+ return mapString("≒");
80
+ case "risingdotseq":
81
+ return mapString("≓");
82
+ case "equiv":
83
+ return mapString("≡");
84
+ case "geq":
85
+ return mapString("≥");
86
+ case "geqq":
87
+ return mapString("≧");
88
+ case "leq":
89
+ return mapString("≤");
90
+ case "leqq":
91
+ return mapString("≦");
92
+ case "gg":
93
+ return mapString("≫");
94
+ case "ll":
95
+ return mapString("≪");
96
+ case "times":
97
+ return mapString("×");
98
+ case "div":
99
+ return mapString("÷");
100
+ case "pm":
101
+ return mapString("±");
102
+ case "mp":
103
+ return mapString("∓");
104
+ case "oplus":
105
+ return mapString("⊕");
106
+ case "ominus":
107
+ return mapString("⊖");
108
+ case "otimes":
109
+ return mapString("⊗");
110
+ case "oslash":
111
+ return mapString("⊘");
112
+ case "circ":
113
+ return mapString("∘");
114
+ case "cdot":
115
+ return mapString("⋅");
116
+ case "bullet":
117
+ return mapString("∙");
118
+ case "ltimes":
119
+ return mapString("⋉");
120
+ case "rtimes":
121
+ return mapString("⋊");
122
+ case "in":
123
+ return mapString("∈");
124
+ case "ni":
125
+ return mapString("∋");
126
+ case "notin":
127
+ return mapString("∉");
128
+ case "subset":
129
+ return mapString("⊂");
130
+ case "supset":
131
+ return mapString("⊃");
132
+ case "subseteq":
133
+ return mapString("⊆");
134
+ case "supseteq":
135
+ return mapString("⊇");
136
+ case "nsubseteq":
137
+ return mapString("⊈");
138
+ case "nsupseteq":
139
+ return mapString("⊉");
140
+ case "subsetneq":
141
+ return mapString("⊊");
142
+ case "supsetneq":
143
+ return mapString("⊋");
144
+ case "cap":
145
+ return mapString("∩");
146
+ case "cup":
147
+ return mapString("∪");
148
+ case "emptyset":
149
+ return mapString("∅");
150
+ case "infty":
151
+ return mapString("∞");
152
+ case "partial":
153
+ return mapString("∂");
154
+ case "aleph":
155
+ return mapString("ℵ");
156
+ case "hbar":
157
+ return mapString("ℏ");
158
+ case "wp":
159
+ return mapString("℘");
160
+ case "Re":
161
+ return mapString("ℜ");
162
+ case "Im":
163
+ return mapString("ℑ");
164
+ case "alpha":
165
+ return mapString("α");
166
+ case "beta":
167
+ return mapString("β");
168
+ case "gamma":
169
+ return mapString("γ");
170
+ case "delta":
171
+ return mapString("δ");
172
+ case "epsilon":
173
+ return mapString("ϵ");
174
+ case "zeta":
175
+ return mapString("ζ");
176
+ case "eta":
177
+ return mapString("η");
178
+ case "theta":
179
+ return mapString("θ");
180
+ case "iota":
181
+ return mapString("ι");
182
+ case "kappa":
183
+ return mapString("κ");
184
+ case "lambda":
185
+ return mapString("λ");
186
+ case "eta":
187
+ return mapString("η");
188
+ case "mu":
189
+ return mapString("μ");
190
+ case "nu":
191
+ return mapString("ν");
192
+ case "xi":
193
+ return mapString("ξ");
194
+ case "pi":
195
+ return mapString("π");
196
+ case "rho":
197
+ return mapString("ρ");
198
+ case "sigma":
199
+ return mapString("σ");
200
+ case "tau":
201
+ return mapString("τ");
202
+ case "upsilon":
203
+ return mapString("υ");
204
+ case "phi":
205
+ return mapString("ϕ");
206
+ case "chi":
207
+ return mapString("χ");
208
+ case "psi":
209
+ return mapString("ψ");
210
+ case "omega":
211
+ return mapString("ω");
212
+ case "varepsilon":
213
+ return mapString("ε");
214
+ case "vartheta":
215
+ return mapString("ϑ");
216
+ case "varrho":
217
+ return mapString("ϱ");
218
+ case "varsigma":
219
+ return mapString("ς");
220
+ case "varphi":
221
+ return mapString("φ");
222
+ case "Gamma":
223
+ return mapString("Γ");
224
+ case "Delta":
225
+ return mapString("Δ");
226
+ case "Theta":
227
+ return mapString("Θ");
228
+ case "Lambda":
229
+ return mapString("Λ");
230
+ case "Xi":
231
+ return mapString("Ξ");
232
+ case "Pi":
233
+ return mapString("Π");
234
+ case "Sigma":
235
+ return mapString("Σ");
236
+ case "Upsilon":
237
+ return mapString("Υ");
238
+ case "Phi":
239
+ return mapString("Φ");
240
+ case "Psi":
241
+ return mapString("Ψ");
242
+ case "Omega":
243
+ return mapString("Ω");
244
+ case "newline":
245
+ case "\\":
246
+ // line break
247
+ return false;
248
+ case "^": {
249
+ const prev = runs.pop();
250
+ if (!prev)
251
+ break;
252
+ return new docx__namespace.MathSuperScript({
253
+ children: [prev],
254
+ 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 : []),
255
+ });
256
+ }
257
+ case "_": {
258
+ const prev = runs.pop();
259
+ if (!prev)
260
+ break;
261
+ return new docx__namespace.MathSubScript({
262
+ children: [prev],
263
+ 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 : []),
264
+ });
265
+ }
266
+ case "hat":
267
+ // TODO: implement
268
+ break;
269
+ case "widehat":
270
+ // TODO: implement
271
+ break;
272
+ case "sum": {
273
+ // TODO: support superscript and subscript
274
+ return new docx__namespace.MathSum({
275
+ children: [],
276
+ });
277
+ }
278
+ case "int":
279
+ return mapString("∫");
280
+ case "frac":
281
+ case "tfrac":
282
+ case "dfrac": {
283
+ const args = (_g = n.args) !== null && _g !== void 0 ? _g : [];
284
+ if (args.length === 2 &&
285
+ hasCurlyBrackets(args[0]) &&
286
+ hasCurlyBrackets(args[1])) {
287
+ return new docx__namespace.MathFraction({
288
+ numerator: mapGroup(args[0].content),
289
+ denominator: mapGroup(args[1].content),
290
+ });
291
+ }
292
+ break;
293
+ }
294
+ case "sqrt": {
295
+ const args = (_h = n.args) !== null && _h !== void 0 ? _h : [];
296
+ if (args.length === 1 && hasCurlyBrackets(args[0])) {
297
+ return new docx__namespace.MathRadical({
298
+ children: mapGroup(args[0].content),
299
+ });
300
+ }
301
+ if (args.length === 2 &&
302
+ hasSquareBrackets(args[0]) &&
303
+ hasCurlyBrackets(args[1])) {
304
+ return new docx__namespace.MathRadical({
305
+ children: mapGroup(args[1].content),
306
+ degree: mapGroup(args[0].content),
307
+ });
308
+ }
309
+ break;
310
+ }
311
+ }
312
+ return mapString(n.content);
313
+ };
314
+ const mapGroup = (nodes) => {
315
+ const group = [];
316
+ for (const c of nodes) {
317
+ group.push(...(mapNode(c, group) || []));
318
+ }
319
+ return group;
320
+ };
321
+ const mapNode = (n, runs) => {
322
+ switch (n.type) {
323
+ case "root":
324
+ break;
325
+ case "string":
326
+ return [mapString(n.content)];
327
+ case "whitespace":
328
+ break;
329
+ case "parbreak":
330
+ break;
331
+ case "comment":
332
+ break;
333
+ case "macro":
334
+ const run = mapMacro(n, runs);
335
+ if (!run) {
336
+ // line break
337
+ return false;
338
+ }
339
+ else {
340
+ return [run];
341
+ }
342
+ case "environment":
343
+ case "mathenv":
344
+ break;
345
+ case "verbatim":
346
+ break;
347
+ case "inlinemath":
348
+ break;
349
+ case "displaymath":
350
+ break;
351
+ case "group":
352
+ return mapGroup(n.content);
353
+ case "verb":
354
+ break;
355
+ default:
356
+ unreachable();
357
+ }
358
+ return [];
359
+ };
360
+ const parseLatex = (value) => {
361
+ const parsed = unifiedLatexUtilParse.parseMath(value);
362
+ const paragraphs = [[]];
363
+ let runs = paragraphs[0];
364
+ for (const n of parsed) {
365
+ const res = mapNode(n, runs);
366
+ if (!res) {
367
+ // line break
368
+ paragraphs.push((runs = []));
369
+ }
370
+ else {
371
+ runs.push(...res);
372
+ }
373
+ }
374
+ return paragraphs;
375
+ };
376
+
377
+ const ORDERED_LIST_REF = "ordered";
378
+ const INDENT = 0.5;
379
+ const DEFAULT_NUMBERINGS = [
93
380
  {
94
381
  level: 0,
95
382
  format: docx__namespace.LevelFormat.DECIMAL,
@@ -152,25 +439,22 @@ var DEFAULT_NUMBERINGS = [
152
439
  },
153
440
  },
154
441
  ];
155
- var error = function (message) {
156
- throw new Error(message);
157
- };
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;
160
- var nodes = convertNodes(node.children, {
442
+ const mdastToDocx = (node, { output = "buffer", title, subject, creator, keywords, description, lastModifiedBy, revision, styles, background, }, images) => {
443
+ const nodes = convertNodes(node.children, {
161
444
  deco: {},
162
- images: images,
445
+ images,
446
+ indent: 0,
163
447
  });
164
- var doc = new docx__namespace.Document({
165
- title: title,
166
- subject: subject,
167
- creator: creator,
168
- keywords: keywords,
169
- description: description,
170
- lastModifiedBy: lastModifiedBy,
171
- revision: revision,
172
- styles: styles,
173
- background: background,
448
+ const doc = new docx__namespace.Document({
449
+ title,
450
+ subject,
451
+ creator,
452
+ keywords,
453
+ description,
454
+ lastModifiedBy,
455
+ revision,
456
+ styles,
457
+ background,
174
458
  sections: [{ children: nodes }],
175
459
  numbering: {
176
460
  config: [
@@ -181,18 +465,16 @@ function mdastToDocx(node, _a, images) {
181
465
  ],
182
466
  },
183
467
  });
184
- switch (output !== null && output !== void 0 ? output : "buffer") {
468
+ switch (output) {
185
469
  case "buffer":
186
470
  return docx.Packer.toBuffer(doc);
187
471
  case "blob":
188
472
  return docx.Packer.toBlob(doc);
189
473
  }
190
- }
191
- function convertNodes(nodes, ctx) {
192
- var _a;
193
- var results = [];
194
- for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
195
- var node = nodes_1[_i];
474
+ };
475
+ const convertNodes = (nodes, ctx) => {
476
+ const results = [];
477
+ for (const node of nodes) {
196
478
  switch (node.type) {
197
479
  case "paragraph":
198
480
  results.push(buildParagraph(node, ctx));
@@ -201,26 +483,23 @@ function convertNodes(nodes, ctx) {
201
483
  results.push(buildHeading(node, ctx));
202
484
  break;
203
485
  case "thematicBreak":
204
- results.push(buildThematicBreak(node));
486
+ results.push(buildThematicBreak());
205
487
  break;
206
488
  case "blockquote":
207
- results.push.apply(results, buildBlockquote(node, ctx));
489
+ results.push(...buildBlockquote(node, ctx));
208
490
  break;
209
491
  case "list":
210
- results.push.apply(results, buildList(node, ctx));
492
+ results.push(...buildList(node, ctx));
211
493
  break;
212
494
  case "listItem":
213
- error("unreachable");
214
- break;
495
+ invariant(false, "unreachable");
215
496
  case "table":
216
497
  results.push(buildTable(node, ctx));
217
498
  break;
218
499
  case "tableRow":
219
- error("unreachable");
220
- break;
500
+ invariant(false, "unreachable");
221
501
  case "tableCell":
222
- error("unreachable");
223
- break;
502
+ invariant(false, "unreachable");
224
503
  case "html":
225
504
  results.push(buildHtml(node));
226
505
  break;
@@ -245,8 +524,8 @@ function convertNodes(nodes, ctx) {
245
524
  case "emphasis":
246
525
  case "strong":
247
526
  case "delete": {
248
- var type = node.type, children = node.children;
249
- results.push.apply(results, convertNodes(children, __assign(__assign({}, ctx), { deco: __assign(__assign({}, ctx.deco), (_a = {}, _a[type] = true, _a)) })));
527
+ const { type, children } = node;
528
+ results.push(...convertNodes(children, Object.assign(Object.assign({}, ctx), { deco: Object.assign(Object.assign({}, ctx.deco), { [type]: true }) })));
250
529
  break;
251
530
  }
252
531
  case "inlineCode":
@@ -254,7 +533,7 @@ function convertNodes(nodes, ctx) {
254
533
  results.push(buildText(node.value, ctx.deco));
255
534
  break;
256
535
  case "break":
257
- results.push(buildBreak(node));
536
+ results.push(buildBreak());
258
537
  break;
259
538
  case "link":
260
539
  results.push(buildLink(node, ctx));
@@ -275,19 +554,25 @@ function convertNodes(nodes, ctx) {
275
554
  // FIXME: unimplemented
276
555
  break;
277
556
  case "math":
278
- results.push(buildMath(node));
557
+ results.push(...buildMath(node));
279
558
  break;
280
559
  case "inlineMath":
281
560
  results.push(buildInlineMath(node));
282
561
  break;
562
+ default:
563
+ unreachable();
564
+ break;
283
565
  }
284
566
  }
285
567
  return results;
286
- }
287
- function buildParagraph(_a, ctx) {
288
- _a.type; var children = _a.children;
289
- var list = ctx.list;
290
- return new docx__namespace.Paragraph(__assign({ children: convertNodes(children, ctx) }, (list &&
568
+ };
569
+ const buildParagraph = ({ children }, ctx) => {
570
+ const list = ctx.list;
571
+ return new docx__namespace.Paragraph(Object.assign({ children: convertNodes(children, ctx), indent: ctx.indent > 0
572
+ ? {
573
+ start: docx.convertInchesToTwip(INDENT * ctx.indent),
574
+ }
575
+ : undefined }, (list &&
291
576
  (list.ordered
292
577
  ? {
293
578
  numbering: {
@@ -300,10 +585,9 @@ function buildParagraph(_a, ctx) {
300
585
  level: list.level,
301
586
  },
302
587
  }))));
303
- }
304
- function buildHeading(_a, ctx) {
305
- _a.type; var children = _a.children, depth = _a.depth;
306
- var heading;
588
+ };
589
+ const buildHeading = ({ children, depth }, ctx) => {
590
+ let heading;
307
591
  switch (depth) {
308
592
  case 1:
309
593
  heading = docx__namespace.HeadingLevel.TITLE;
@@ -325,39 +609,33 @@ function buildHeading(_a, ctx) {
325
609
  break;
326
610
  }
327
611
  return new docx__namespace.Paragraph({
328
- heading: heading,
612
+ heading,
329
613
  children: convertNodes(children, ctx),
330
614
  });
331
- }
332
- function buildThematicBreak(_a) {
333
- _a.type;
615
+ };
616
+ const buildThematicBreak = (_) => {
334
617
  return new docx__namespace.Paragraph({
335
618
  thematicBreak: true,
336
619
  });
337
- }
338
- function buildBlockquote(_a, ctx) {
339
- _a.type; var children = _a.children;
340
- // FIXME: do nothing for now
341
- return convertNodes(children, ctx);
342
- }
343
- function buildList(_a, ctx) {
344
- _a.type; var children = _a.children, ordered = _a.ordered; _a.start; _a.spread;
345
- var list = {
620
+ };
621
+ const buildBlockquote = ({ children }, ctx) => {
622
+ return convertNodes(children, Object.assign(Object.assign({}, ctx), { indent: ctx.indent + 1 }));
623
+ };
624
+ const buildList = ({ children, ordered, start: _start, spread: _spread }, ctx) => {
625
+ const list = {
346
626
  level: ctx.list ? ctx.list.level + 1 : 0,
347
627
  ordered: !!ordered,
348
628
  };
349
- return children.reduce(function (acc, item) {
350
- acc.push.apply(acc, buildListItem(item, __assign(__assign({}, ctx), { list: list })));
629
+ return children.reduce((acc, item) => {
630
+ acc.push(...buildListItem(item, Object.assign(Object.assign({}, ctx), { list })));
351
631
  return acc;
352
632
  }, []);
353
- }
354
- function buildListItem(_a, ctx) {
355
- _a.type; var children = _a.children; _a.checked; _a.spread;
633
+ };
634
+ const buildListItem = ({ children, checked: _checked, spread: _spread }, ctx) => {
356
635
  return convertNodes(children, ctx);
357
- }
358
- function buildTable(_a, ctx) {
359
- _a.type; var children = _a.children, align = _a.align;
360
- var cellAligns = align === null || align === void 0 ? void 0 : align.map(function (a) {
636
+ };
637
+ const buildTable = ({ children, align }, ctx) => {
638
+ const cellAligns = align === null || align === void 0 ? void 0 : align.map((a) => {
361
639
  switch (a) {
362
640
  case "left":
363
641
  return docx__namespace.AlignmentType.LEFT;
@@ -370,21 +648,19 @@ function buildTable(_a, ctx) {
370
648
  }
371
649
  });
372
650
  return new docx__namespace.Table({
373
- rows: children.map(function (r) {
651
+ rows: children.map((r) => {
374
652
  return buildTableRow(r, ctx, cellAligns);
375
653
  }),
376
654
  });
377
- }
378
- function buildTableRow(_a, ctx, cellAligns) {
379
- _a.type; var children = _a.children;
655
+ };
656
+ const buildTableRow = ({ children }, ctx, cellAligns) => {
380
657
  return new docx__namespace.TableRow({
381
- children: children.map(function (c, i) {
658
+ children: children.map((c, i) => {
382
659
  return buildTableCell(c, ctx, cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i]);
383
660
  }),
384
661
  });
385
- }
386
- function buildTableCell(_a, ctx, align) {
387
- _a.type; var children = _a.children;
662
+ };
663
+ const buildTableCell = ({ children }, ctx, align) => {
388
664
  return new docx__namespace.TableCell({
389
665
  children: [
390
666
  new docx__namespace.Paragraph({
@@ -393,111 +669,92 @@ function buildTableCell(_a, ctx, align) {
393
669
  }),
394
670
  ],
395
671
  });
396
- }
397
- function buildHtml(_a) {
398
- _a.type; var value = _a.value;
672
+ };
673
+ const buildHtml = ({ value }) => {
399
674
  // FIXME: transform to text for now
400
675
  return new docx__namespace.Paragraph({
401
676
  children: [buildText(value, {})],
402
677
  });
403
- }
404
- function buildCode(_a) {
405
- _a.type; var value = _a.value; _a.lang; _a.meta;
678
+ };
679
+ const buildCode = ({ value, lang: _lang, meta: _meta }) => {
406
680
  // FIXME: transform to text for now
407
681
  return new docx__namespace.Paragraph({
408
682
  children: [buildText(value, {})],
409
683
  });
410
- }
411
- function buildMath(_a) {
412
- _a.type; var value = _a.value;
413
- // FIXME: transform to text for now
414
- return new docx__namespace.Paragraph({
415
- children: [new docx__namespace.TextRun(value)],
684
+ };
685
+ const buildMath = ({ value }) => {
686
+ return parseLatex(value).map((runs) => new docx__namespace.Paragraph({
687
+ children: [
688
+ new docx__namespace.Math({
689
+ children: runs,
690
+ }),
691
+ ],
692
+ }));
693
+ };
694
+ const buildInlineMath = ({ value }) => {
695
+ return new docx__namespace.Math({
696
+ children: parseLatex(value).flatMap((runs) => runs),
416
697
  });
417
- }
418
- function buildInlineMath(_a) {
419
- _a.type; var value = _a.value;
420
- // FIXME: transform to text for now
421
- return new docx__namespace.TextRun(value);
422
- }
423
- function buildText(text, deco) {
698
+ };
699
+ const buildText = (text, deco) => {
424
700
  return new docx__namespace.TextRun({
425
- text: text,
701
+ text,
426
702
  bold: deco.strong,
427
703
  italics: deco.emphasis,
428
704
  strike: deco.delete,
429
705
  });
430
- }
431
- function buildBreak(_a) {
432
- _a.type;
706
+ };
707
+ const buildBreak = (_) => {
433
708
  return new docx__namespace.TextRun({ text: "", break: 1 });
434
- }
435
- function buildLink(_a, ctx) {
436
- _a.type; var children = _a.children, url = _a.url; _a.title;
709
+ };
710
+ const buildLink = ({ children, url, title: _title }, ctx) => {
437
711
  return new docx__namespace.ExternalHyperlink({
438
712
  link: url,
439
713
  children: convertNodes(children, ctx),
440
714
  });
441
- }
442
- function buildImage(_a, images) {
443
- _a.type; var url = _a.url; _a.title; _a.alt;
444
- if (!images[url]) {
445
- return error("Fetch image was failed: ".concat(url));
446
- }
447
- var _b = images[url], image = _b.image, width = _b.width, height = _b.height;
715
+ };
716
+ const buildImage = ({ url, title: _title, alt: _alt }, images) => {
717
+ const img = images[url];
718
+ invariant(img, `Fetch image was failed: ${url}`);
719
+ const { image, width, height } = img;
448
720
  return new docx__namespace.ImageRun({
449
721
  data: image,
450
722
  transformation: {
451
- width: width,
452
- height: height,
723
+ width,
724
+ height,
453
725
  },
454
726
  });
455
- }
456
- function buildFootnote(_a, ctx) {
457
- _a.type; var children = _a.children;
727
+ };
728
+ const buildFootnote = ({ children }, ctx) => {
458
729
  // FIXME: transform to paragraph for now
459
730
  return new docx__namespace.Paragraph({
460
731
  children: convertNodes(children, ctx),
461
732
  });
462
- }
733
+ };
463
734
 
464
- var plugin = function (opts) {
465
- var _this = this;
466
- if (opts === void 0) { opts = {}; }
467
- var images = {};
468
- this.Compiler = function (node) {
735
+ const plugin = function (opts = {}) {
736
+ let images = {};
737
+ this.Compiler = (node) => {
469
738
  return mdastToDocx(node, opts, images);
470
739
  };
471
- return function (node) { return __awaiter(_this, void 0, void 0, function () {
472
- var imageList, imageResolver, imageDatas;
473
- return __generator(this, function (_a) {
474
- switch (_a.label) {
475
- case 0:
476
- imageList = [];
477
- unistUtilVisit.visit(node, "image", function (node) {
478
- imageList.push(node);
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
- }
487
- return [4 /*yield*/, Promise.all(imageList.map(function (_a) {
488
- var url = _a.url;
489
- return imageResolver(url);
490
- }))];
491
- case 1:
492
- imageDatas = _a.sent();
493
- images = imageList.reduce(function (acc, img, i) {
494
- acc[img.url] = imageDatas[i];
495
- return acc;
496
- }, {});
497
- return [2 /*return*/, node];
498
- }
740
+ return (node) => tslib.__awaiter(this, void 0, void 0, function* () {
741
+ const imageList = [];
742
+ unistUtilVisit.visit(node, "image", (node) => {
743
+ imageList.push(node);
499
744
  });
500
- }); };
745
+ if (imageList.length === 0) {
746
+ return node;
747
+ }
748
+ const imageResolver = opts.imageResolver;
749
+ invariant(imageResolver, "options.imageResolver is not defined.");
750
+ const imageDatas = yield Promise.all(imageList.map(({ url }) => imageResolver(url)));
751
+ images = imageList.reduce((acc, img, i) => {
752
+ acc[img.url] = imageDatas[i];
753
+ return acc;
754
+ }, {});
755
+ return node;
756
+ });
501
757
  };
502
758
 
503
759
  module.exports = plugin;
760
+ //# sourceMappingURL=index.js.map