remark-docx 0.0.1 → 0.0.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/README.md CHANGED
@@ -65,4 +65,4 @@ const text = "# hello world";
65
65
  | Key | Default | Type | Description |
66
66
  | ------------- | --------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
67
67
  | docProperties | undefined | object | Override properties of document. |
68
- | imageResolver | undefined | ImageResolver | **You must set** if your markdown includes images. See example for [Node.js](https://github.com/inokawa/remark-docx/blob/main/src/index.spec.ts) and [browser](https://github.com/inokawa/remark-docx/blob/main/stories/playground.stories.tsx). |
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). |
package/lib/index.js CHANGED
@@ -185,7 +185,7 @@ function convertNodes(nodes, ctx, opts) {
185
185
  results.push(buildHeading(node, ctx, opts));
186
186
  break;
187
187
  case "thematicBreak":
188
- results.push(buildThematicBreak());
188
+ results.push(buildThematicBreak(node));
189
189
  break;
190
190
  case "blockquote":
191
191
  results.push.apply(results, buildBlockquote(node, ctx, opts));
@@ -238,7 +238,7 @@ function convertNodes(nodes, ctx, opts) {
238
238
  results.push(buildText(node.value, ctx.deco));
239
239
  break;
240
240
  case "break":
241
- results.push(buildBreak());
241
+ results.push(buildBreak(node));
242
242
  break;
243
243
  case "link":
244
244
  results.push(buildLink(node, ctx, opts));
@@ -268,8 +268,8 @@ function convertNodes(nodes, ctx, opts) {
268
268
  }
269
269
  return results;
270
270
  }
271
- function buildParagraph(node, ctx, opts) {
272
- node.type; var children = node.children;
271
+ function buildParagraph(_a, ctx, opts) {
272
+ _a.type; var children = _a.children;
273
273
  var list = ctx.list;
274
274
  return new docx__namespace.Paragraph(__assign({ children: convertNodes(children, ctx, opts) }, (list &&
275
275
  (list.ordered
@@ -285,8 +285,8 @@ function buildParagraph(node, ctx, opts) {
285
285
  },
286
286
  }))));
287
287
  }
288
- function buildHeading(node, ctx, opts) {
289
- node.type; var children = node.children, depth = node.depth;
288
+ function buildHeading(_a, ctx, opts) {
289
+ _a.type; var children = _a.children, depth = _a.depth;
290
290
  var heading;
291
291
  switch (depth) {
292
292
  case 1:
@@ -313,17 +313,19 @@ function buildHeading(node, ctx, opts) {
313
313
  children: convertNodes(children, ctx, opts),
314
314
  });
315
315
  }
316
- function buildThematicBreak(node) {
316
+ function buildThematicBreak(_a) {
317
+ _a.type;
317
318
  return new docx__namespace.Paragraph({
318
319
  thematicBreak: true,
319
320
  });
320
321
  }
321
- function buildBlockquote(node, ctx, opts) {
322
+ function buildBlockquote(_a, ctx, opts) {
323
+ _a.type; var children = _a.children;
322
324
  // FIXME: do nothing for now
323
- return convertNodes(node.children, ctx, opts);
325
+ return convertNodes(children, ctx, opts);
324
326
  }
325
- function buildList(node, ctx, opts) {
326
- node.type; var children = node.children, ordered = node.ordered; node.start; node.spread;
327
+ function buildList(_a, ctx, opts) {
328
+ _a.type; var children = _a.children, ordered = _a.ordered; _a.start; _a.spread;
327
329
  var list = {
328
330
  level: ctx.list ? ctx.list.level + 1 : 0,
329
331
  ordered: !!ordered,
@@ -333,12 +335,12 @@ function buildList(node, ctx, opts) {
333
335
  return acc;
334
336
  }, []);
335
337
  }
336
- function buildListItem(node, ctx, opts) {
337
- node.type; var children = node.children; node.checked; node.spread;
338
+ function buildListItem(_a, ctx, opts) {
339
+ _a.type; var children = _a.children; _a.checked; _a.spread;
338
340
  return convertNodes(children, ctx, opts);
339
341
  }
340
- function buildTable(node, ctx, opts) {
341
- node.type; var children = node.children, align = node.align;
342
+ function buildTable(_a, ctx, opts) {
343
+ _a.type; var children = _a.children, align = _a.align;
342
344
  var cellAligns = align === null || align === void 0 ? void 0 : align.map(function (a) {
343
345
  switch (a) {
344
346
  case "left":
@@ -357,16 +359,16 @@ function buildTable(node, ctx, opts) {
357
359
  }),
358
360
  });
359
361
  }
360
- function buildTableRow(node, ctx, opts, cellAligns) {
361
- node.type; var children = node.children;
362
+ function buildTableRow(_a, ctx, opts, cellAligns) {
363
+ _a.type; var children = _a.children;
362
364
  return new docx__namespace.TableRow({
363
365
  children: children.map(function (c, i) {
364
366
  return buildTableCell(c, ctx, opts, cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i]);
365
367
  }),
366
368
  });
367
369
  }
368
- function buildTableCell(node, ctx, opts, align) {
369
- node.type; var children = node.children;
370
+ function buildTableCell(_a, ctx, opts, align) {
371
+ _a.type; var children = _a.children;
370
372
  return new docx__namespace.TableCell({
371
373
  children: [
372
374
  new docx__namespace.Paragraph({
@@ -376,29 +378,29 @@ function buildTableCell(node, ctx, opts, align) {
376
378
  ],
377
379
  });
378
380
  }
379
- function buildHtml(node) {
380
- node.type; var value = node.value;
381
+ function buildHtml(_a) {
382
+ _a.type; var value = _a.value;
381
383
  // FIXME: transform to text for now
382
384
  return new docx__namespace.Paragraph({
383
385
  children: [buildText(value, {})],
384
386
  });
385
387
  }
386
- function buildCode(node) {
387
- node.type; var value = node.value; node.lang; node.meta;
388
+ function buildCode(_a) {
389
+ _a.type; var value = _a.value; _a.lang; _a.meta;
388
390
  // FIXME: transform to text for now
389
391
  return new docx__namespace.Paragraph({
390
392
  children: [buildText(value, {})],
391
393
  });
392
394
  }
393
- function buildMath(node) {
394
- node.type; var value = node.value;
395
+ function buildMath(_a) {
396
+ _a.type; var value = _a.value;
395
397
  // FIXME: transform to text for now
396
398
  return new docx__namespace.Paragraph({
397
399
  children: [new docx__namespace.TextRun(value)],
398
400
  });
399
401
  }
400
- function buildInlineMath(node) {
401
- node.type; var value = node.value;
402
+ function buildInlineMath(_a) {
403
+ _a.type; var value = _a.value;
402
404
  // FIXME: transform to text for now
403
405
  return new docx__namespace.TextRun(value);
404
406
  }
@@ -410,22 +412,23 @@ function buildText(text, deco) {
410
412
  strike: deco.delete,
411
413
  });
412
414
  }
413
- function buildBreak(node) {
415
+ function buildBreak(_a) {
416
+ _a.type;
414
417
  return new docx__namespace.TextRun({ text: "", break: 1 });
415
418
  }
416
- function buildLink(node, ctx, opts) {
417
- node.type; var children = node.children, url = node.url; node.title;
419
+ function buildLink(_a, ctx, opts) {
420
+ _a.type; var children = _a.children, url = _a.url; _a.title;
418
421
  return new docx__namespace.ExternalHyperlink({
419
422
  link: url,
420
423
  children: convertNodes(children, ctx, opts),
421
424
  });
422
425
  }
423
- function buildImage(node, images) {
424
- node.type; var url = node.url; node.title; node.alt;
426
+ function buildImage(_a, images) {
427
+ _a.type; var url = _a.url; _a.title; _a.alt;
425
428
  if (!images[url]) {
426
429
  return error("Fetch image was failed: ".concat(url));
427
430
  }
428
- var _a = images[url], image = _a.image, width = _a.width, height = _a.height;
431
+ var _b = images[url], image = _b.image, width = _b.width, height = _b.height;
429
432
  return new docx__namespace.ImageRun({
430
433
  data: image,
431
434
  transformation: {
@@ -434,8 +437,8 @@ function buildImage(node, images) {
434
437
  },
435
438
  });
436
439
  }
437
- function buildFootnote(node, ctx, opts) {
438
- node.type; var children = node.children;
440
+ function buildFootnote(_a, ctx, opts) {
441
+ _a.type; var children = _a.children;
439
442
  // FIXME: transform to paragraph for now
440
443
  return new docx__namespace.Paragraph({
441
444
  children: convertNodes(children, ctx, opts),
@@ -450,18 +453,21 @@ var plugin = function (opts) {
450
453
  return mdastToDocx(node, opts, images);
451
454
  };
452
455
  return function (node) { return __awaiter(_this, void 0, void 0, function () {
453
- var imageResolver, imageList, imageDatas;
456
+ var imageList, imageResolver, imageDatas;
454
457
  return __generator(this, function (_a) {
455
458
  switch (_a.label) {
456
459
  case 0:
457
- imageResolver = opts.imageResolver;
458
- if (!imageResolver) {
459
- throw new Error("options.imageResolver is not defined.");
460
- }
461
460
  imageList = [];
462
461
  unistUtilVisit.visit(node, "image", function (node) {
463
462
  imageList.push(node);
464
463
  });
464
+ if (imageList.length === 0) {
465
+ return [2 /*return*/, node];
466
+ }
467
+ imageResolver = opts.imageResolver;
468
+ if (!imageResolver) {
469
+ throw new Error("options.imageResolver is not defined.");
470
+ }
465
471
  return [4 /*yield*/, Promise.all(imageList.map(function (_a) {
466
472
  var url = _a.url;
467
473
  return imageResolver(url);
package/lib/index.mjs CHANGED
@@ -163,7 +163,7 @@ function convertNodes(nodes, ctx, opts) {
163
163
  results.push(buildHeading(node, ctx, opts));
164
164
  break;
165
165
  case "thematicBreak":
166
- results.push(buildThematicBreak());
166
+ results.push(buildThematicBreak(node));
167
167
  break;
168
168
  case "blockquote":
169
169
  results.push.apply(results, buildBlockquote(node, ctx, opts));
@@ -216,7 +216,7 @@ function convertNodes(nodes, ctx, opts) {
216
216
  results.push(buildText(node.value, ctx.deco));
217
217
  break;
218
218
  case "break":
219
- results.push(buildBreak());
219
+ results.push(buildBreak(node));
220
220
  break;
221
221
  case "link":
222
222
  results.push(buildLink(node, ctx, opts));
@@ -246,8 +246,8 @@ function convertNodes(nodes, ctx, opts) {
246
246
  }
247
247
  return results;
248
248
  }
249
- function buildParagraph(node, ctx, opts) {
250
- node.type; var children = node.children;
249
+ function buildParagraph(_a, ctx, opts) {
250
+ _a.type; var children = _a.children;
251
251
  var list = ctx.list;
252
252
  return new docx.Paragraph(__assign({ children: convertNodes(children, ctx, opts) }, (list &&
253
253
  (list.ordered
@@ -263,8 +263,8 @@ function buildParagraph(node, ctx, opts) {
263
263
  },
264
264
  }))));
265
265
  }
266
- function buildHeading(node, ctx, opts) {
267
- node.type; var children = node.children, depth = node.depth;
266
+ function buildHeading(_a, ctx, opts) {
267
+ _a.type; var children = _a.children, depth = _a.depth;
268
268
  var heading;
269
269
  switch (depth) {
270
270
  case 1:
@@ -291,17 +291,19 @@ function buildHeading(node, ctx, opts) {
291
291
  children: convertNodes(children, ctx, opts),
292
292
  });
293
293
  }
294
- function buildThematicBreak(node) {
294
+ function buildThematicBreak(_a) {
295
+ _a.type;
295
296
  return new docx.Paragraph({
296
297
  thematicBreak: true,
297
298
  });
298
299
  }
299
- function buildBlockquote(node, ctx, opts) {
300
+ function buildBlockquote(_a, ctx, opts) {
301
+ _a.type; var children = _a.children;
300
302
  // FIXME: do nothing for now
301
- return convertNodes(node.children, ctx, opts);
303
+ return convertNodes(children, ctx, opts);
302
304
  }
303
- function buildList(node, ctx, opts) {
304
- node.type; var children = node.children, ordered = node.ordered; node.start; node.spread;
305
+ function buildList(_a, ctx, opts) {
306
+ _a.type; var children = _a.children, ordered = _a.ordered; _a.start; _a.spread;
305
307
  var list = {
306
308
  level: ctx.list ? ctx.list.level + 1 : 0,
307
309
  ordered: !!ordered,
@@ -311,12 +313,12 @@ function buildList(node, ctx, opts) {
311
313
  return acc;
312
314
  }, []);
313
315
  }
314
- function buildListItem(node, ctx, opts) {
315
- node.type; var children = node.children; node.checked; node.spread;
316
+ function buildListItem(_a, ctx, opts) {
317
+ _a.type; var children = _a.children; _a.checked; _a.spread;
316
318
  return convertNodes(children, ctx, opts);
317
319
  }
318
- function buildTable(node, ctx, opts) {
319
- node.type; var children = node.children, align = node.align;
320
+ function buildTable(_a, ctx, opts) {
321
+ _a.type; var children = _a.children, align = _a.align;
320
322
  var cellAligns = align === null || align === void 0 ? void 0 : align.map(function (a) {
321
323
  switch (a) {
322
324
  case "left":
@@ -335,16 +337,16 @@ function buildTable(node, ctx, opts) {
335
337
  }),
336
338
  });
337
339
  }
338
- function buildTableRow(node, ctx, opts, cellAligns) {
339
- node.type; var children = node.children;
340
+ function buildTableRow(_a, ctx, opts, cellAligns) {
341
+ _a.type; var children = _a.children;
340
342
  return new docx.TableRow({
341
343
  children: children.map(function (c, i) {
342
344
  return buildTableCell(c, ctx, opts, cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i]);
343
345
  }),
344
346
  });
345
347
  }
346
- function buildTableCell(node, ctx, opts, align) {
347
- node.type; var children = node.children;
348
+ function buildTableCell(_a, ctx, opts, align) {
349
+ _a.type; var children = _a.children;
348
350
  return new docx.TableCell({
349
351
  children: [
350
352
  new docx.Paragraph({
@@ -354,29 +356,29 @@ function buildTableCell(node, ctx, opts, align) {
354
356
  ],
355
357
  });
356
358
  }
357
- function buildHtml(node) {
358
- node.type; var value = node.value;
359
+ function buildHtml(_a) {
360
+ _a.type; var value = _a.value;
359
361
  // FIXME: transform to text for now
360
362
  return new docx.Paragraph({
361
363
  children: [buildText(value, {})],
362
364
  });
363
365
  }
364
- function buildCode(node) {
365
- node.type; var value = node.value; node.lang; node.meta;
366
+ function buildCode(_a) {
367
+ _a.type; var value = _a.value; _a.lang; _a.meta;
366
368
  // FIXME: transform to text for now
367
369
  return new docx.Paragraph({
368
370
  children: [buildText(value, {})],
369
371
  });
370
372
  }
371
- function buildMath(node) {
372
- node.type; var value = node.value;
373
+ function buildMath(_a) {
374
+ _a.type; var value = _a.value;
373
375
  // FIXME: transform to text for now
374
376
  return new docx.Paragraph({
375
377
  children: [new docx.TextRun(value)],
376
378
  });
377
379
  }
378
- function buildInlineMath(node) {
379
- node.type; var value = node.value;
380
+ function buildInlineMath(_a) {
381
+ _a.type; var value = _a.value;
380
382
  // FIXME: transform to text for now
381
383
  return new docx.TextRun(value);
382
384
  }
@@ -388,22 +390,23 @@ function buildText(text, deco) {
388
390
  strike: deco.delete,
389
391
  });
390
392
  }
391
- function buildBreak(node) {
393
+ function buildBreak(_a) {
394
+ _a.type;
392
395
  return new docx.TextRun({ text: "", break: 1 });
393
396
  }
394
- function buildLink(node, ctx, opts) {
395
- node.type; var children = node.children, url = node.url; node.title;
397
+ function buildLink(_a, ctx, opts) {
398
+ _a.type; var children = _a.children, url = _a.url; _a.title;
396
399
  return new docx.ExternalHyperlink({
397
400
  link: url,
398
401
  children: convertNodes(children, ctx, opts),
399
402
  });
400
403
  }
401
- function buildImage(node, images) {
402
- node.type; var url = node.url; node.title; node.alt;
404
+ function buildImage(_a, images) {
405
+ _a.type; var url = _a.url; _a.title; _a.alt;
403
406
  if (!images[url]) {
404
407
  return error("Fetch image was failed: ".concat(url));
405
408
  }
406
- var _a = images[url], image = _a.image, width = _a.width, height = _a.height;
409
+ var _b = images[url], image = _b.image, width = _b.width, height = _b.height;
407
410
  return new docx.ImageRun({
408
411
  data: image,
409
412
  transformation: {
@@ -412,8 +415,8 @@ function buildImage(node, images) {
412
415
  },
413
416
  });
414
417
  }
415
- function buildFootnote(node, ctx, opts) {
416
- node.type; var children = node.children;
418
+ function buildFootnote(_a, ctx, opts) {
419
+ _a.type; var children = _a.children;
417
420
  // FIXME: transform to paragraph for now
418
421
  return new docx.Paragraph({
419
422
  children: convertNodes(children, ctx, opts),
@@ -428,18 +431,21 @@ var plugin = function (opts) {
428
431
  return mdastToDocx(node, opts, images);
429
432
  };
430
433
  return function (node) { return __awaiter(_this, void 0, void 0, function () {
431
- var imageResolver, imageList, imageDatas;
434
+ var imageList, imageResolver, imageDatas;
432
435
  return __generator(this, function (_a) {
433
436
  switch (_a.label) {
434
437
  case 0:
435
- imageResolver = opts.imageResolver;
436
- if (!imageResolver) {
437
- throw new Error("options.imageResolver is not defined.");
438
- }
439
438
  imageList = [];
440
439
  visit(node, "image", function (node) {
441
440
  imageList.push(node);
442
441
  });
442
+ if (imageList.length === 0) {
443
+ return [2 /*return*/, node];
444
+ }
445
+ imageResolver = opts.imageResolver;
446
+ if (!imageResolver) {
447
+ throw new Error("options.imageResolver is not defined.");
448
+ }
443
449
  return [4 /*yield*/, Promise.all(imageList.map(function (_a) {
444
450
  var url = _a.url;
445
451
  return imageResolver(url);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remark-docx",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "remark plugin to transform markdown to docx.",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.mjs",