remark-docx 0.3.8 → 0.3.10
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 +1 -1
- package/lib/index.cjs +106 -162
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +106 -162
- package/lib/index.js.map +1 -1
- package/lib/mdast-to-docx.d.ts +1 -1
- package/lib/plugins/code/index.cjs +33 -3
- package/lib/plugins/code/index.cjs.map +1 -1
- package/lib/plugins/code/index.js +33 -3
- package/lib/plugins/code/index.js.map +1 -1
- package/lib/plugins/html/index.cjs +2 -2
- package/lib/plugins/html/index.cjs.map +1 -1
- package/lib/plugins/html/index.js +2 -2
- package/lib/plugins/html/index.js.map +1 -1
- package/lib/plugins/image/index.cjs +17 -7
- package/lib/plugins/image/index.cjs.map +1 -1
- package/lib/plugins/image/index.js +14 -4
- package/lib/plugins/image/index.js.map +1 -1
- package/lib/plugins/math/index.cjs +4 -2
- package/lib/plugins/math/index.cjs.map +1 -1
- package/lib/plugins/math/index.js +3 -1
- package/lib/plugins/math/index.js.map +1 -1
- package/lib/types.d.ts +42 -5
- package/package.json +1 -1
- package/lib/mdast.d.ts +0 -2
- package/lib/utils-40yKzkXT.js +0 -19
- package/lib/utils-40yKzkXT.js.map +0 -1
- package/lib/utils-EYEfXxbh.js +0 -22
- package/lib/utils-EYEfXxbh.js.map +0 -1
package/lib/index.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { sectionPageSizeDefaults, sectionMarginDefaults, Document, Packer, AlignmentType, LevelFormat, convertInchesToTwip, FootnoteReferenceRun, ExternalHyperlink, TextRun, Paragraph, Table, TableRow, TableCell, HeadingLevel, CheckBox } from 'docx';
|
|
2
2
|
import { definitions } from 'mdast-util-definitions';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* @internal
|
|
6
|
-
*/
|
|
7
|
-
function invariant(cond, message) {
|
|
8
|
-
throw new Error(message);
|
|
9
|
-
}
|
|
10
4
|
const alreadyWarned = {};
|
|
11
5
|
/**
|
|
12
6
|
* @internal
|
|
@@ -43,7 +37,7 @@ const createFootnoteRegistry = () => {
|
|
|
43
37
|
},
|
|
44
38
|
toConfig: () => {
|
|
45
39
|
return defs.entries().reduce((acc, [key, def]) => {
|
|
46
|
-
acc[key] = def;
|
|
40
|
+
acc[key] = { children: def };
|
|
47
41
|
return acc;
|
|
48
42
|
}, {});
|
|
49
43
|
},
|
|
@@ -131,14 +125,63 @@ const mdastToDocx = async (node, { plugins = [], title, subject, creator, keywor
|
|
|
131
125
|
const footnote = createFootnoteRegistry();
|
|
132
126
|
const numbering = createNumberingRegistry();
|
|
133
127
|
const pluginCtx = { root: node, definition };
|
|
134
|
-
const
|
|
135
|
-
|
|
128
|
+
const defaultBuilders = {
|
|
129
|
+
paragraph: buildParagraph,
|
|
130
|
+
heading: buildHeading,
|
|
131
|
+
thematicBreak: buildThematicBreak,
|
|
132
|
+
blockquote: buildBlockquote,
|
|
133
|
+
list: buildList,
|
|
134
|
+
listItem: buildListItem,
|
|
135
|
+
table: buildTable,
|
|
136
|
+
tableRow: noop,
|
|
137
|
+
tableCell: noop,
|
|
138
|
+
html: fallbackText,
|
|
139
|
+
code: fallbackText,
|
|
140
|
+
definition: noop,
|
|
141
|
+
footnoteDefinition: buildFootnoteDefinition,
|
|
142
|
+
text: buildText,
|
|
143
|
+
emphasis: buildEmphasis,
|
|
144
|
+
strong: buildStrong,
|
|
145
|
+
delete: buildDelete,
|
|
146
|
+
inlineCode: buildInlineCode,
|
|
147
|
+
break: buildBreak,
|
|
148
|
+
link: buildLink,
|
|
149
|
+
linkReference: buildLinkReference,
|
|
150
|
+
// image: warnImage,
|
|
151
|
+
// imageReference: warnImage,
|
|
152
|
+
footnoteReference: buildFootnoteReference,
|
|
153
|
+
math: fallbackText,
|
|
154
|
+
inlineMath: fallbackText,
|
|
155
|
+
};
|
|
156
|
+
const builders = (await Promise.all(plugins.map((p) => p(pluginCtx)))).reduceRight((acc, p) => ({ ...acc, ...p }), defaultBuilders);
|
|
157
|
+
const ctx = {
|
|
158
|
+
next(nodes, c) {
|
|
159
|
+
const results = [];
|
|
160
|
+
for (const node of nodes) {
|
|
161
|
+
const builder = builders[node.type];
|
|
162
|
+
if (!builder) {
|
|
163
|
+
warnOnce(`${node.type} node is not supported without plugins.`);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const r = builder(node, c !== null && c !== void 0 ? c : this);
|
|
167
|
+
if (r) {
|
|
168
|
+
if (Array.isArray(r)) {
|
|
169
|
+
results.push(...r);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
results.push(r);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return results;
|
|
177
|
+
},
|
|
136
178
|
deco: {},
|
|
137
179
|
indent: 0,
|
|
138
180
|
definition: definition,
|
|
139
181
|
footnote,
|
|
140
182
|
numbering,
|
|
141
|
-
}
|
|
183
|
+
};
|
|
184
|
+
const nodes = ctx.next(node.children);
|
|
142
185
|
const doc = new Document({
|
|
143
186
|
title,
|
|
144
187
|
subject,
|
|
@@ -157,121 +200,9 @@ const mdastToDocx = async (node, { plugins = [], title, subject, creator, keywor
|
|
|
157
200
|
});
|
|
158
201
|
return Packer.toArrayBuffer(doc);
|
|
159
202
|
};
|
|
160
|
-
const convertNodes = (nodes, ctx) => {
|
|
161
|
-
var _a, _b;
|
|
162
|
-
const results = [];
|
|
163
|
-
for (const node of nodes) {
|
|
164
|
-
const customNodes = (_b = (_a = ctx.overrides)[node.type]) === null || _b === void 0 ? void 0 : _b.call(_a, node, (children) => convertNodes(children, ctx));
|
|
165
|
-
if (customNodes != null) {
|
|
166
|
-
if (Array.isArray(customNodes)) {
|
|
167
|
-
results.push(...customNodes);
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
results.push(customNodes);
|
|
171
|
-
}
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
switch (node.type) {
|
|
175
|
-
case "paragraph": {
|
|
176
|
-
results.push(buildParagraph(node, ctx));
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
case "heading": {
|
|
180
|
-
results.push(buildHeading(node, ctx));
|
|
181
|
-
break;
|
|
182
|
-
}
|
|
183
|
-
case "thematicBreak":
|
|
184
|
-
results.push(buildThematicBreak());
|
|
185
|
-
break;
|
|
186
|
-
case "blockquote": {
|
|
187
|
-
results.push(...buildBlockquote(node, ctx));
|
|
188
|
-
break;
|
|
189
|
-
}
|
|
190
|
-
case "list": {
|
|
191
|
-
results.push(...buildList(node, ctx));
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
case "listItem":
|
|
195
|
-
invariant(false, "unreachable");
|
|
196
|
-
case "table":
|
|
197
|
-
results.push(buildTable(node, ctx));
|
|
198
|
-
break;
|
|
199
|
-
case "tableRow":
|
|
200
|
-
invariant(false, "unreachable");
|
|
201
|
-
case "tableCell":
|
|
202
|
-
invariant(false, "unreachable");
|
|
203
|
-
case "html":
|
|
204
|
-
warnOnce(`${node.type} node is not rendered since remark-docx/plugins/html is not provided.`);
|
|
205
|
-
break;
|
|
206
|
-
case "code":
|
|
207
|
-
warnOnce(`${node.type} node is not rendered since remark-docx/plugins/code is not provided.`);
|
|
208
|
-
break;
|
|
209
|
-
// case "yaml":
|
|
210
|
-
// // unimplemented
|
|
211
|
-
// break;
|
|
212
|
-
// case "toml":
|
|
213
|
-
// // unimplemented
|
|
214
|
-
// break;
|
|
215
|
-
case "definition":
|
|
216
|
-
// noop
|
|
217
|
-
break;
|
|
218
|
-
case "footnoteDefinition": {
|
|
219
|
-
registerFootnoteDefinition(node, ctx);
|
|
220
|
-
break;
|
|
221
|
-
}
|
|
222
|
-
case "text":
|
|
223
|
-
results.push(buildText(node.value, ctx.deco));
|
|
224
|
-
break;
|
|
225
|
-
case "emphasis":
|
|
226
|
-
case "strong":
|
|
227
|
-
case "delete": {
|
|
228
|
-
const { type, children } = node;
|
|
229
|
-
const nodes = convertNodes(children, {
|
|
230
|
-
...ctx,
|
|
231
|
-
deco: { ...ctx.deco, [type]: true },
|
|
232
|
-
});
|
|
233
|
-
results.push(...nodes);
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
case "inlineCode":
|
|
237
|
-
results.push(buildInlineCode(node));
|
|
238
|
-
break;
|
|
239
|
-
case "break":
|
|
240
|
-
results.push(buildBreak());
|
|
241
|
-
break;
|
|
242
|
-
case "link": {
|
|
243
|
-
results.push(buildLink(node, ctx));
|
|
244
|
-
break;
|
|
245
|
-
}
|
|
246
|
-
case "linkReference":
|
|
247
|
-
results.push(...buildLinkReference(node, ctx));
|
|
248
|
-
break;
|
|
249
|
-
case "image":
|
|
250
|
-
case "imageReference": {
|
|
251
|
-
warnOnce(`${node.type} node is not rendered since remark-docx/plugins/image is not provided.`);
|
|
252
|
-
break;
|
|
253
|
-
}
|
|
254
|
-
// case "footnote": {
|
|
255
|
-
// // inline footnote was removed in mdast v5
|
|
256
|
-
// break;
|
|
257
|
-
// }
|
|
258
|
-
case "footnoteReference":
|
|
259
|
-
results.push(buildFootnoteReference(node, ctx));
|
|
260
|
-
break;
|
|
261
|
-
case "math":
|
|
262
|
-
case "inlineMath":
|
|
263
|
-
warnOnce(`${node.type} node is not rendered since remark-docx/plugins/math is not provided.`);
|
|
264
|
-
break;
|
|
265
|
-
default:
|
|
266
|
-
warnOnce(`${node.type} node is not officially supported.`);
|
|
267
|
-
break;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
return results;
|
|
271
|
-
};
|
|
272
203
|
const buildParagraph = ({ children }, ctx) => {
|
|
273
204
|
const list = ctx.list;
|
|
274
|
-
const nodes =
|
|
205
|
+
const nodes = ctx.next(children);
|
|
275
206
|
if (list && list.checked != null) {
|
|
276
207
|
nodes.unshift(new CheckBox({
|
|
277
208
|
checked: list.checked,
|
|
@@ -323,19 +254,19 @@ const buildHeading = ({ children, depth }, ctx) => {
|
|
|
323
254
|
headingLevel = HeadingLevel.HEADING_5;
|
|
324
255
|
break;
|
|
325
256
|
}
|
|
326
|
-
const nodes =
|
|
257
|
+
const nodes = ctx.next(children);
|
|
327
258
|
return new Paragraph({
|
|
328
259
|
heading: headingLevel,
|
|
329
260
|
children: nodes,
|
|
330
261
|
});
|
|
331
262
|
};
|
|
332
|
-
const buildThematicBreak = (
|
|
263
|
+
const buildThematicBreak = () => {
|
|
333
264
|
return new Paragraph({
|
|
334
265
|
thematicBreak: true,
|
|
335
266
|
});
|
|
336
267
|
};
|
|
337
268
|
const buildBlockquote = ({ children }, ctx) => {
|
|
338
|
-
return
|
|
269
|
+
return ctx.next(children, {
|
|
339
270
|
...ctx,
|
|
340
271
|
indent: ctx.indent + 1,
|
|
341
272
|
});
|
|
@@ -343,22 +274,20 @@ const buildBlockquote = ({ children }, ctx) => {
|
|
|
343
274
|
const buildList = ({ children, ordered }, ctx) => {
|
|
344
275
|
var _a;
|
|
345
276
|
const isTopLevel = !ctx.list;
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
list,
|
|
357
|
-
});
|
|
277
|
+
const reference = isTopLevel && ordered
|
|
278
|
+
? ctx.numbering.create()
|
|
279
|
+
: ((_a = ctx.list) === null || _a === void 0 ? void 0 : _a.reference) || ORDERED_LIST_REF;
|
|
280
|
+
return ctx.next(children, {
|
|
281
|
+
...ctx,
|
|
282
|
+
list: {
|
|
283
|
+
level: isTopLevel ? 0 : ctx.list.level + 1,
|
|
284
|
+
ordered: !!ordered,
|
|
285
|
+
reference,
|
|
286
|
+
},
|
|
358
287
|
});
|
|
359
288
|
};
|
|
360
289
|
const buildListItem = ({ children, checked }, ctx) => {
|
|
361
|
-
return
|
|
290
|
+
return ctx.next(children, {
|
|
362
291
|
...ctx,
|
|
363
292
|
...(ctx.list && { list: { ...ctx.list, checked: checked !== null && checked !== void 0 ? checked : undefined } }),
|
|
364
293
|
});
|
|
@@ -388,7 +317,7 @@ const buildTable = ({ children, align }, ctx) => {
|
|
|
388
317
|
children: [
|
|
389
318
|
new Paragraph({
|
|
390
319
|
alignment: cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i],
|
|
391
|
-
children:
|
|
320
|
+
children: ctx.next(c.children),
|
|
392
321
|
}),
|
|
393
322
|
],
|
|
394
323
|
});
|
|
@@ -397,12 +326,30 @@ const buildTable = ({ children, align }, ctx) => {
|
|
|
397
326
|
}),
|
|
398
327
|
});
|
|
399
328
|
};
|
|
400
|
-
const buildText = (
|
|
329
|
+
const buildText = ({ value }, { deco }) => {
|
|
401
330
|
return new TextRun({
|
|
402
|
-
text,
|
|
403
|
-
bold: deco.
|
|
404
|
-
italics: deco.
|
|
405
|
-
strike: deco.
|
|
331
|
+
text: value,
|
|
332
|
+
bold: deco.bold,
|
|
333
|
+
italics: deco.italic,
|
|
334
|
+
strike: deco.strike,
|
|
335
|
+
});
|
|
336
|
+
};
|
|
337
|
+
const buildEmphasis = ({ children }, ctx) => {
|
|
338
|
+
return ctx.next(children, {
|
|
339
|
+
...ctx,
|
|
340
|
+
deco: { ...ctx.deco, italic: true },
|
|
341
|
+
});
|
|
342
|
+
};
|
|
343
|
+
const buildStrong = ({ children }, ctx) => {
|
|
344
|
+
return ctx.next(children, {
|
|
345
|
+
...ctx,
|
|
346
|
+
deco: { ...ctx.deco, bold: true },
|
|
347
|
+
});
|
|
348
|
+
};
|
|
349
|
+
const buildDelete = ({ children }, ctx) => {
|
|
350
|
+
return ctx.next(children, {
|
|
351
|
+
...ctx,
|
|
352
|
+
deco: { ...ctx.deco, strike: true },
|
|
406
353
|
});
|
|
407
354
|
};
|
|
408
355
|
const buildInlineCode = ({ value }) => {
|
|
@@ -411,11 +358,11 @@ const buildInlineCode = ({ value }) => {
|
|
|
411
358
|
highlight: "lightGray",
|
|
412
359
|
});
|
|
413
360
|
};
|
|
414
|
-
const buildBreak = (
|
|
361
|
+
const buildBreak = () => {
|
|
415
362
|
return new TextRun({ text: "", break: 1 });
|
|
416
363
|
};
|
|
417
364
|
const buildLink = ({ children, url }, ctx) => {
|
|
418
|
-
const nodes =
|
|
365
|
+
const nodes = ctx.next(children);
|
|
419
366
|
return new ExternalHyperlink({
|
|
420
367
|
link: url,
|
|
421
368
|
children: nodes,
|
|
@@ -424,27 +371,24 @@ const buildLink = ({ children, url }, ctx) => {
|
|
|
424
371
|
const buildLinkReference = ({ children, identifier }, ctx) => {
|
|
425
372
|
const def = ctx.definition(identifier);
|
|
426
373
|
if (def == null) {
|
|
427
|
-
return
|
|
374
|
+
return ctx.next(children);
|
|
428
375
|
}
|
|
429
|
-
return
|
|
376
|
+
return buildLink({ children, url: def.url }, ctx);
|
|
430
377
|
};
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
// Convert each node and extract the first result as a paragraph
|
|
435
|
-
const nodes = convertNodes([node], ctx);
|
|
436
|
-
if (nodes[0] instanceof Paragraph) {
|
|
437
|
-
return nodes[0];
|
|
438
|
-
}
|
|
439
|
-
// For non-paragraph content, wrap in a paragraph
|
|
440
|
-
return new Paragraph({ children: nodes });
|
|
441
|
-
}),
|
|
442
|
-
};
|
|
443
|
-
ctx.footnote.def(identifier, definition);
|
|
378
|
+
const buildFootnoteDefinition = ({ children, identifier }, ctx) => {
|
|
379
|
+
ctx.footnote.def(identifier, ctx.next(children).filter((c) => c instanceof Paragraph));
|
|
380
|
+
return null;
|
|
444
381
|
};
|
|
445
382
|
const buildFootnoteReference = ({ identifier }, ctx) => {
|
|
446
383
|
return new FootnoteReferenceRun(ctx.footnote.ref(identifier));
|
|
447
384
|
};
|
|
385
|
+
const noop = () => {
|
|
386
|
+
return null;
|
|
387
|
+
};
|
|
388
|
+
const fallbackText = (node, ctx) => {
|
|
389
|
+
warnOnce(`${node.type} node is not supported without plugins, falling back to text.`);
|
|
390
|
+
return buildText({ value: node.value }, ctx);
|
|
391
|
+
};
|
|
448
392
|
|
|
449
393
|
const plugin = function (opts = {}) {
|
|
450
394
|
this.compiler = (node) => {
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/src/utils.ts","../src/src/mdast-to-docx.ts","../src/src/plugin.ts"],"sourcesContent":["/**\n * @internal\n */\nexport function invariant(cond: any, message: string): asserts cond {\n if (!cond) throw new Error(message);\n}\n\nconst alreadyWarned: { [message: string]: boolean } = {};\n\n/**\n * @internal\n */\nexport function warnOnce(message: string, cond: boolean = false): void {\n if (!cond && !alreadyWarned[message]) {\n alreadyWarned[message] = true;\n console.warn(message);\n }\n}\n","import {\n convertInchesToTwip,\n Packer,\n Document,\n Paragraph,\n Table,\n TableRow,\n TableCell,\n TextRun,\n ExternalHyperlink,\n HeadingLevel,\n LevelFormat,\n AlignmentType,\n type ILevelsOptions,\n FootnoteReferenceRun,\n CheckBox,\n type IPropertiesOptions,\n sectionPageSizeDefaults,\n sectionMarginDefaults,\n} from \"docx\";\nimport type * as mdast from \"./mdast\";\nimport { invariant, warnOnce } from \"./utils\";\nimport { definitions, type GetDefinition } from \"mdast-util-definitions\";\nimport type {\n DocxChild,\n DocxContent,\n NodeOverrides,\n RemarkDocxPlugin,\n} from \"./types\";\n\nconst CONTENT_WIDTH =\n sectionPageSizeDefaults.WIDTH -\n sectionMarginDefaults.LEFT -\n sectionMarginDefaults.RIGHT;\nconst ORDERED_LIST_REF = \"ordered\";\nconst INDENT = 0.5;\n\ntype Decoration = Readonly<{\n [key in (mdast.Emphasis | mdast.Strong | mdast.Delete)[\"type\"]]?: true;\n}>;\n\ntype ListInfo = Readonly<{\n level: number;\n ordered: boolean;\n reference: string;\n checked?: boolean;\n}>;\n\ntype FootnoteDefinition = Readonly<{ children: Paragraph[] }>;\ntype FootnoteRegistry = {\n ref: (id: string) => number;\n def: (id: string, def: FootnoteDefinition) => void;\n toConfig: () => {\n [key: string]: FootnoteDefinition;\n };\n};\n\nconst createFootnoteRegistry = (): FootnoteRegistry => {\n const idToInternalId = new Map<string, number>();\n const defs = new Map<number, FootnoteDefinition>();\n\n const getId = (id: string): number => {\n let internalId = idToInternalId.get(id);\n if (internalId == null) {\n idToInternalId.set(id, (internalId = idToInternalId.size + 1));\n }\n return internalId;\n };\n\n return {\n ref: (id) => {\n return getId(id);\n },\n def: (id, def) => {\n const internalId = getId(id);\n defs.set(internalId, def);\n },\n toConfig: () => {\n return defs.entries().reduce(\n (acc, [key, def]) => {\n acc[key] = def;\n return acc;\n },\n {} as {\n [key: string]: FootnoteDefinition;\n },\n );\n },\n };\n};\n\ntype NumberingRegistry = {\n create: () => string;\n toConfig: () => Array<{ reference: string; levels: ILevelsOptions[] }>;\n};\n\nconst createNumberingRegistry = (): NumberingRegistry => {\n let counter = 1;\n\n const DEFAULT_NUMBERINGS: ILevelsOptions[] = [\n {\n level: 0,\n format: LevelFormat.DECIMAL,\n text: \"%1.\",\n alignment: AlignmentType.START,\n },\n {\n level: 1,\n format: LevelFormat.DECIMAL,\n text: \"%2.\",\n alignment: AlignmentType.START,\n style: {\n paragraph: {\n indent: { start: convertInchesToTwip(INDENT * 1) },\n },\n },\n },\n {\n level: 2,\n format: LevelFormat.DECIMAL,\n text: \"%3.\",\n alignment: AlignmentType.START,\n style: {\n paragraph: {\n indent: { start: convertInchesToTwip(INDENT * 2) },\n },\n },\n },\n {\n level: 3,\n format: LevelFormat.DECIMAL,\n text: \"%4.\",\n alignment: AlignmentType.START,\n style: {\n paragraph: {\n indent: { start: convertInchesToTwip(INDENT * 3) },\n },\n },\n },\n {\n level: 4,\n format: LevelFormat.DECIMAL,\n text: \"%5.\",\n alignment: AlignmentType.START,\n style: {\n paragraph: {\n indent: { start: convertInchesToTwip(INDENT * 4) },\n },\n },\n },\n {\n level: 5,\n format: LevelFormat.DECIMAL,\n text: \"%6.\",\n alignment: AlignmentType.START,\n style: {\n paragraph: {\n indent: { start: convertInchesToTwip(INDENT * 5) },\n },\n },\n },\n ];\n\n return {\n create: () => {\n return `${ORDERED_LIST_REF}-${counter++}`;\n },\n toConfig: () => {\n return Array.from({ length: counter }, (_, i) => ({\n reference: `${ORDERED_LIST_REF}-${i}`,\n levels: DEFAULT_NUMBERINGS,\n }));\n },\n };\n};\n\ntype Context = Readonly<{\n overrides: NodeOverrides;\n deco: Decoration;\n indent: number;\n list?: ListInfo;\n definition: GetDefinition;\n footnote: FootnoteRegistry;\n numbering: NumberingRegistry;\n}>;\n\nexport interface DocxOptions extends Pick<\n IPropertiesOptions,\n | \"title\"\n | \"subject\"\n | \"creator\"\n | \"keywords\"\n | \"description\"\n | \"lastModifiedBy\"\n | \"revision\"\n | \"styles\"\n | \"background\"\n> {\n /**\n * Plugins to customize how mdast nodes are transformed.\n */\n plugins?: RemarkDocxPlugin[];\n}\n\nexport const mdastToDocx = async (\n node: mdast.Root,\n {\n plugins = [],\n title,\n subject,\n creator,\n keywords,\n description,\n lastModifiedBy,\n revision,\n styles,\n background,\n }: DocxOptions,\n): Promise<ArrayBuffer> => {\n const definition = definitions(node);\n\n const footnote = createFootnoteRegistry();\n const numbering = createNumberingRegistry();\n\n const pluginCtx = { root: node, definition };\n const nodes = convertNodes(node.children, {\n overrides: (\n await Promise.all(plugins.map((p) => p(pluginCtx)))\n ).reduceRight((acc, p) => ({ ...acc, ...p }), {}),\n deco: {},\n indent: 0,\n definition: definition,\n footnote,\n numbering,\n });\n\n const doc = new Document({\n title,\n subject,\n creator,\n keywords,\n description,\n lastModifiedBy,\n revision,\n styles,\n background,\n footnotes: footnote.toConfig(),\n sections: [{ children: nodes as DocxChild[] }],\n numbering: {\n config: numbering.toConfig(),\n },\n });\n\n return Packer.toArrayBuffer(doc);\n};\n\nconst convertNodes = (\n nodes: mdast.RootContent[],\n ctx: Context,\n): DocxContent[] => {\n const results: DocxContent[] = [];\n for (const node of nodes) {\n const customNodes = ctx.overrides[node.type]?.(node as any, (children) =>\n convertNodes(children, ctx),\n );\n if (customNodes != null) {\n if (Array.isArray(customNodes)) {\n results.push(...customNodes);\n } else {\n results.push(customNodes);\n }\n continue;\n }\n\n switch (node.type) {\n case \"paragraph\": {\n results.push(buildParagraph(node, ctx));\n break;\n }\n case \"heading\": {\n results.push(buildHeading(node, ctx));\n break;\n }\n case \"thematicBreak\":\n results.push(buildThematicBreak(node));\n break;\n case \"blockquote\": {\n results.push(...buildBlockquote(node, ctx));\n break;\n }\n case \"list\": {\n results.push(...buildList(node, ctx));\n break;\n }\n case \"listItem\":\n invariant(false, \"unreachable\");\n case \"table\":\n results.push(buildTable(node, ctx));\n break;\n case \"tableRow\":\n invariant(false, \"unreachable\");\n case \"tableCell\":\n invariant(false, \"unreachable\");\n case \"html\":\n warnOnce(\n `${node.type} node is not rendered since remark-docx/plugins/html is not provided.`,\n );\n break;\n case \"code\":\n warnOnce(\n `${node.type} node is not rendered since remark-docx/plugins/code is not provided.`,\n );\n break;\n // case \"yaml\":\n // // unimplemented\n // break;\n // case \"toml\":\n // // unimplemented\n // break;\n case \"definition\":\n // noop\n break;\n case \"footnoteDefinition\": {\n registerFootnoteDefinition(node, ctx);\n break;\n }\n case \"text\":\n results.push(buildText(node.value, ctx.deco));\n break;\n case \"emphasis\":\n case \"strong\":\n case \"delete\": {\n const { type, children } = node;\n const nodes = convertNodes(children, {\n ...ctx,\n deco: { ...ctx.deco, [type]: true },\n });\n results.push(...nodes);\n break;\n }\n case \"inlineCode\":\n results.push(buildInlineCode(node));\n break;\n case \"break\":\n results.push(buildBreak(node));\n break;\n case \"link\": {\n results.push(buildLink(node, ctx));\n break;\n }\n case \"linkReference\":\n results.push(...buildLinkReference(node, ctx));\n break;\n case \"image\":\n case \"imageReference\": {\n warnOnce(\n `${node.type} node is not rendered since remark-docx/plugins/image is not provided.`,\n );\n break;\n }\n // case \"footnote\": {\n // // inline footnote was removed in mdast v5\n // break;\n // }\n case \"footnoteReference\":\n results.push(buildFootnoteReference(node, ctx));\n break;\n case \"math\":\n case \"inlineMath\":\n warnOnce(\n `${node.type} node is not rendered since remark-docx/plugins/math is not provided.`,\n );\n break;\n default:\n warnOnce(`${node.type} node is not officially supported.`);\n break;\n }\n }\n return results;\n};\n\nconst buildParagraph = (\n { children }: mdast.Paragraph,\n ctx: Context,\n): DocxContent => {\n const list = ctx.list;\n const nodes = convertNodes(children, ctx);\n\n if (list && list.checked != null) {\n nodes.unshift(\n new CheckBox({\n checked: list.checked,\n checkedState: { value: \"2611\" },\n uncheckedState: { value: \"2610\" },\n }),\n );\n }\n return new Paragraph({\n children: nodes,\n indent:\n ctx.indent > 0\n ? {\n start: convertInchesToTwip(INDENT * ctx.indent),\n }\n : undefined,\n ...(list &&\n (list.ordered\n ? {\n numbering: {\n reference: list.reference,\n level: list.level,\n },\n }\n : {\n bullet: {\n level: list.level,\n },\n })),\n });\n};\n\nconst buildHeading = (\n { children, depth }: mdast.Heading,\n ctx: Context,\n): DocxContent => {\n let headingLevel: (typeof HeadingLevel)[keyof typeof HeadingLevel];\n switch (depth) {\n case 1:\n headingLevel = HeadingLevel.TITLE;\n break;\n case 2:\n headingLevel = HeadingLevel.HEADING_1;\n break;\n case 3:\n headingLevel = HeadingLevel.HEADING_2;\n break;\n case 4:\n headingLevel = HeadingLevel.HEADING_3;\n break;\n case 5:\n headingLevel = HeadingLevel.HEADING_4;\n break;\n case 6:\n headingLevel = HeadingLevel.HEADING_5;\n break;\n }\n const nodes = convertNodes(children, ctx);\n return new Paragraph({\n heading: headingLevel,\n children: nodes,\n });\n};\n\nconst buildThematicBreak = (_: mdast.ThematicBreak): DocxContent => {\n return new Paragraph({\n thematicBreak: true,\n });\n};\n\nconst buildBlockquote = (\n { children }: mdast.Blockquote,\n ctx: Context,\n): DocxContent[] => {\n return convertNodes(children, {\n ...ctx,\n indent: ctx.indent + 1,\n });\n};\n\nconst buildList = (\n { children, ordered }: mdast.List,\n ctx: Context,\n): DocxContent[] => {\n const isTopLevel = !ctx.list;\n const list: ListInfo = {\n level: ctx.list ? ctx.list.level + 1 : 0,\n ordered: !!ordered,\n reference:\n isTopLevel && ordered\n ? ctx.numbering.create()\n : ctx.list?.reference || ORDERED_LIST_REF,\n };\n return children.flatMap((item) => {\n return buildListItem(item, {\n ...ctx,\n list,\n });\n });\n};\n\nconst buildListItem = (\n { children, checked }: mdast.ListItem,\n ctx: Context,\n): DocxContent[] => {\n return convertNodes(children, {\n ...ctx,\n ...(ctx.list && { list: { ...ctx.list, checked: checked ?? undefined } }),\n });\n};\n\nconst buildTable = (\n { children, align }: mdast.Table,\n ctx: Context,\n): DocxContent => {\n const cellAligns:\n | (typeof AlignmentType)[keyof typeof AlignmentType][]\n | undefined = align?.map((a) => {\n switch (a) {\n case \"left\":\n return AlignmentType.LEFT;\n case \"right\":\n return AlignmentType.RIGHT;\n case \"center\":\n return AlignmentType.CENTER;\n default:\n return AlignmentType.LEFT;\n }\n });\n\n const columnLength = children[0]!.children.length;\n const columnWidth = CONTENT_WIDTH / columnLength;\n\n return new Table({\n columnWidths: Array.from({ length: columnLength }).map(() => columnWidth),\n rows: children.map((r) => {\n return new TableRow({\n children: r.children.map((c, i) => {\n return new TableCell({\n width: { size: columnWidth, type: \"dxa\" },\n children: [\n new Paragraph({\n alignment: cellAligns?.[i],\n children: convertNodes(c.children, ctx),\n }),\n ],\n });\n }),\n });\n }),\n });\n};\n\nconst buildText = (text: string, deco: Decoration): DocxContent => {\n return new TextRun({\n text,\n bold: deco.strong,\n italics: deco.emphasis,\n strike: deco.delete,\n });\n};\n\nconst buildInlineCode = ({ value }: mdast.InlineCode): DocxContent => {\n return new TextRun({\n text: value,\n highlight: \"lightGray\",\n });\n};\n\nconst buildBreak = (_: mdast.Break): DocxContent => {\n return new TextRun({ text: \"\", break: 1 });\n};\n\nconst buildLink = (\n { children, url }: Pick<mdast.Link, \"children\" | \"url\">,\n ctx: Context,\n): DocxContent => {\n const nodes = convertNodes(children, ctx);\n return new ExternalHyperlink({\n link: url,\n children: nodes,\n });\n};\n\nconst buildLinkReference = (\n { children, identifier }: mdast.LinkReference,\n ctx: Context,\n): DocxContent[] => {\n const def = ctx.definition(identifier);\n if (def == null) {\n return convertNodes(children, ctx);\n }\n return [buildLink({ children, url: def.url }, ctx)];\n};\n\nconst registerFootnoteDefinition = (\n { children, identifier }: mdast.FootnoteDefinition,\n ctx: Context,\n): void => {\n const definition: FootnoteDefinition = {\n children: children.map((node) => {\n // Convert each node and extract the first result as a paragraph\n const nodes = convertNodes([node], ctx);\n if (nodes[0] instanceof Paragraph) {\n return nodes[0] as Paragraph;\n }\n // For non-paragraph content, wrap in a paragraph\n return new Paragraph({ children: nodes });\n }),\n };\n ctx.footnote.def(identifier, definition);\n};\n\nconst buildFootnoteReference = (\n { identifier }: mdast.FootnoteReference,\n ctx: Context,\n): DocxContent => {\n return new FootnoteReferenceRun(ctx.footnote.ref(identifier));\n};\n","import type { Plugin } from \"unified\";\nimport type { Root } from \"mdast\";\nimport { mdastToDocx, type DocxOptions } from \"./mdast-to-docx\";\n\nexport type { DocxOptions };\n\ndeclare module \"unified\" {\n interface CompileResultMap {\n docx: Promise<ArrayBuffer>;\n }\n}\n\nconst plugin: Plugin<[DocxOptions?], Root, Promise<ArrayBuffer>> = function (\n opts = {},\n) {\n this.compiler = (node) => {\n return mdastToDocx(node as Root, opts);\n };\n};\nexport default plugin;\n"],"names":[],"mappings":";;;AAAA;;AAEG;AACG,SAAU,SAAS,CAAC,IAAS,EAAE,OAAe,EAAA;AAClD,IAAW,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC;AACrC;AAEA,MAAM,aAAa,GAAmC,EAAE;AAExD;;AAEG;SACa,QAAQ,CAAC,OAAe,EAAE,OAAgB,KAAK,EAAA;IAC7D,IAAI,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE;AACpC,QAAA,aAAa,CAAC,OAAO,CAAC,GAAG,IAAI;AAC7B,QAAA,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC;IACvB;AACF;;ACaA,MAAM,aAAa,GACjB,uBAAuB,CAAC,KAAK;AAC7B,IAAA,qBAAqB,CAA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CAAC,CAAC;AACrD,CAAC;AAED,MAAM,0BAA0B,GAAG,CACjC,EAAE,QAAQ,EAAE,UAAU,EAA4B,EAClD,GAAY,KACJ;AACR,IAAA,MAAM,UAAU,GAAuB;QACrC,QAAQ,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,KAAI;;YAE9B,MAAM,KAAK,GAAG,YAAY,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC;AACvC,YAAA,IAAI,KAAK,CAAC,CAAC,CAAC,YAAY,SAAS,EAAE;AACjC,gBAAA,OAAO,KAAK,CAAC,CAAC,CAAc;YAC9B;;YAEA,OAAO,IAAI,SAAS,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAC3C,QAAA,CAAC,CAAC;KACH;IACD,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,UAAU,CAAC;AAC1C,CAAC;AAED,MAAM,sBAAsB,GAAG,CAC7B,EAAE,UAAU,EAA2B,EACvC,GAAY,KACG;AACf,IAAA,OAAO,IAAI,oBAAoB,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;AAC/D,CAAC;;ACnlBD,MAAM,MAAM,GAAuD,UACjE,IAAI,GAAG,EAAE,EAAA;AAET,IAAA,IAAI,CAAC,QAAQ,GAAG,CAAC,IAAI,KAAI;AACvB,QAAA,OAAO,WAAW,CAAC,IAAY,EAAE,IAAI,CAAC;AACxC,IAAA,CAAC;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/src/utils.ts","../src/src/mdast-to-docx.ts","../src/src/plugin.ts"],"sourcesContent":["const alreadyWarned: { [message: string]: boolean } = {};\n\n/**\n * @internal\n */\nexport function warnOnce(message: string, cond: boolean = false): void {\n if (!cond && !alreadyWarned[message]) {\n alreadyWarned[message] = true;\n console.warn(message);\n }\n}\n","import {\n convertInchesToTwip,\n Packer,\n Document,\n Paragraph,\n Table,\n TableRow,\n TableCell,\n TextRun,\n ExternalHyperlink,\n HeadingLevel,\n LevelFormat,\n AlignmentType,\n type ILevelsOptions,\n FootnoteReferenceRun,\n CheckBox,\n type IPropertiesOptions,\n sectionPageSizeDefaults,\n sectionMarginDefaults,\n} from \"docx\";\nimport type * as mdast from \"mdast\";\nimport { warnOnce } from \"./utils\";\nimport { definitions } from \"mdast-util-definitions\";\nimport type {\n Context,\n DocxChild,\n DocxContent,\n FootnoteRegistry,\n NodeBuilder,\n NodeBuilders,\n NumberingRegistry,\n RemarkDocxPlugin,\n} from \"./types\";\n\nconst CONTENT_WIDTH =\n sectionPageSizeDefaults.WIDTH -\n sectionMarginDefaults.LEFT -\n sectionMarginDefaults.RIGHT;\nconst ORDERED_LIST_REF = \"ordered\";\nconst INDENT = 0.5;\n\nconst createFootnoteRegistry = (): FootnoteRegistry => {\n const idToInternalId = new Map<string, number>();\n const defs = new Map<number, Paragraph[]>();\n\n const getId = (id: string): number => {\n let internalId = idToInternalId.get(id);\n if (internalId == null) {\n idToInternalId.set(id, (internalId = idToInternalId.size + 1));\n }\n return internalId;\n };\n\n return {\n ref: (id) => {\n return getId(id);\n },\n def: (id, def) => {\n const internalId = getId(id);\n defs.set(internalId, def);\n },\n toConfig: () => {\n return defs.entries().reduce(\n (acc, [key, def]) => {\n acc[key] = { children: def };\n return acc;\n },\n {} as {\n [key: string]: { children: Paragraph[] };\n },\n );\n },\n };\n};\n\nconst createNumberingRegistry = (): NumberingRegistry => {\n let counter = 1;\n\n const DEFAULT_NUMBERINGS: ILevelsOptions[] = [\n {\n level: 0,\n format: LevelFormat.DECIMAL,\n text: \"%1.\",\n alignment: AlignmentType.START,\n },\n {\n level: 1,\n format: LevelFormat.DECIMAL,\n text: \"%2.\",\n alignment: AlignmentType.START,\n style: {\n paragraph: {\n indent: { start: convertInchesToTwip(INDENT * 1) },\n },\n },\n },\n {\n level: 2,\n format: LevelFormat.DECIMAL,\n text: \"%3.\",\n alignment: AlignmentType.START,\n style: {\n paragraph: {\n indent: { start: convertInchesToTwip(INDENT * 2) },\n },\n },\n },\n {\n level: 3,\n format: LevelFormat.DECIMAL,\n text: \"%4.\",\n alignment: AlignmentType.START,\n style: {\n paragraph: {\n indent: { start: convertInchesToTwip(INDENT * 3) },\n },\n },\n },\n {\n level: 4,\n format: LevelFormat.DECIMAL,\n text: \"%5.\",\n alignment: AlignmentType.START,\n style: {\n paragraph: {\n indent: { start: convertInchesToTwip(INDENT * 4) },\n },\n },\n },\n {\n level: 5,\n format: LevelFormat.DECIMAL,\n text: \"%6.\",\n alignment: AlignmentType.START,\n style: {\n paragraph: {\n indent: { start: convertInchesToTwip(INDENT * 5) },\n },\n },\n },\n ];\n\n return {\n create: () => {\n return `${ORDERED_LIST_REF}-${counter++}`;\n },\n toConfig: () => {\n return Array.from({ length: counter }, (_, i) => ({\n reference: `${ORDERED_LIST_REF}-${i}`,\n levels: DEFAULT_NUMBERINGS,\n }));\n },\n };\n};\n\nexport interface DocxOptions extends Pick<\n IPropertiesOptions,\n | \"title\"\n | \"subject\"\n | \"creator\"\n | \"keywords\"\n | \"description\"\n | \"lastModifiedBy\"\n | \"revision\"\n | \"styles\"\n | \"background\"\n> {\n /**\n * Plugins to customize how mdast nodes are transformed.\n */\n plugins?: RemarkDocxPlugin[];\n}\n\nexport const mdastToDocx = async (\n node: mdast.Root,\n {\n plugins = [],\n title,\n subject,\n creator,\n keywords,\n description,\n lastModifiedBy,\n revision,\n styles,\n background,\n }: DocxOptions,\n): Promise<ArrayBuffer> => {\n const definition = definitions(node);\n\n const footnote = createFootnoteRegistry();\n const numbering = createNumberingRegistry();\n\n const pluginCtx = { root: node, definition };\n\n const defaultBuilders: NodeBuilders = {\n paragraph: buildParagraph,\n heading: buildHeading,\n thematicBreak: buildThematicBreak,\n blockquote: buildBlockquote,\n list: buildList,\n listItem: buildListItem,\n table: buildTable,\n tableRow: noop,\n tableCell: noop,\n html: fallbackText,\n code: fallbackText,\n definition: noop,\n footnoteDefinition: buildFootnoteDefinition,\n text: buildText,\n emphasis: buildEmphasis,\n strong: buildStrong,\n delete: buildDelete,\n inlineCode: buildInlineCode,\n break: buildBreak,\n link: buildLink,\n linkReference: buildLinkReference,\n // image: warnImage,\n // imageReference: warnImage,\n footnoteReference: buildFootnoteReference,\n math: fallbackText,\n inlineMath: fallbackText,\n };\n\n const builders = (\n await Promise.all(plugins.map((p) => p(pluginCtx)))\n ).reduceRight((acc, p) => ({ ...acc, ...p }), defaultBuilders);\n\n const ctx: Context = {\n next(nodes, c) {\n const results: DocxContent[] = [];\n for (const node of nodes) {\n const builder = builders[node.type];\n if (!builder) {\n warnOnce(`${node.type} node is not supported without plugins.`);\n continue;\n }\n const r = builder(node as any, c ?? this);\n if (r) {\n if (Array.isArray(r)) {\n results.push(...r);\n } else {\n results.push(r);\n }\n }\n }\n return results;\n },\n deco: {},\n indent: 0,\n definition: definition,\n footnote,\n numbering,\n };\n\n const nodes = ctx.next(node.children);\n\n const doc = new Document({\n title,\n subject,\n creator,\n keywords,\n description,\n lastModifiedBy,\n revision,\n styles,\n background,\n footnotes: footnote.toConfig(),\n sections: [{ children: nodes as DocxChild[] }],\n numbering: {\n config: numbering.toConfig(),\n },\n });\n\n return Packer.toArrayBuffer(doc);\n};\n\nconst buildParagraph: NodeBuilder<\"paragraph\"> = ({ children }, ctx) => {\n const list = ctx.list;\n const nodes = ctx.next(children);\n\n if (list && list.checked != null) {\n nodes.unshift(\n new CheckBox({\n checked: list.checked,\n checkedState: { value: \"2611\" },\n uncheckedState: { value: \"2610\" },\n }),\n );\n }\n return new Paragraph({\n children: nodes,\n indent:\n ctx.indent > 0\n ? {\n start: convertInchesToTwip(INDENT * ctx.indent),\n }\n : undefined,\n ...(list &&\n (list.ordered\n ? {\n numbering: {\n reference: list.reference,\n level: list.level,\n },\n }\n : {\n bullet: {\n level: list.level,\n },\n })),\n });\n};\n\nconst buildHeading: NodeBuilder<\"heading\"> = ({ children, depth }, ctx) => {\n let headingLevel: (typeof HeadingLevel)[keyof typeof HeadingLevel];\n switch (depth) {\n case 1:\n headingLevel = HeadingLevel.TITLE;\n break;\n case 2:\n headingLevel = HeadingLevel.HEADING_1;\n break;\n case 3:\n headingLevel = HeadingLevel.HEADING_2;\n break;\n case 4:\n headingLevel = HeadingLevel.HEADING_3;\n break;\n case 5:\n headingLevel = HeadingLevel.HEADING_4;\n break;\n case 6:\n headingLevel = HeadingLevel.HEADING_5;\n break;\n }\n const nodes = ctx.next(children);\n return new Paragraph({\n heading: headingLevel,\n children: nodes,\n });\n};\n\nconst buildThematicBreak: NodeBuilder<\"thematicBreak\"> = () => {\n return new Paragraph({\n thematicBreak: true,\n });\n};\n\nconst buildBlockquote: NodeBuilder<\"blockquote\"> = ({ children }, ctx) => {\n return ctx.next(children, {\n ...ctx,\n indent: ctx.indent + 1,\n });\n};\n\nconst buildList: NodeBuilder<\"list\"> = ({ children, ordered }, ctx) => {\n const isTopLevel = !ctx.list;\n const reference =\n isTopLevel && ordered\n ? ctx.numbering.create()\n : ctx.list?.reference || ORDERED_LIST_REF;\n\n return ctx.next(children, {\n ...ctx,\n list: {\n level: isTopLevel ? 0 : ctx.list.level + 1,\n ordered: !!ordered,\n reference,\n },\n });\n};\n\nconst buildListItem: NodeBuilder<\"listItem\"> = ({ children, checked }, ctx) => {\n return ctx.next(children, {\n ...ctx,\n ...(ctx.list && { list: { ...ctx.list, checked: checked ?? undefined } }),\n });\n};\n\nconst buildTable: NodeBuilder<\"table\"> = ({ children, align }, ctx) => {\n const cellAligns:\n | (typeof AlignmentType)[keyof typeof AlignmentType][]\n | undefined = align?.map((a) => {\n switch (a) {\n case \"left\":\n return AlignmentType.LEFT;\n case \"right\":\n return AlignmentType.RIGHT;\n case \"center\":\n return AlignmentType.CENTER;\n default:\n return AlignmentType.LEFT;\n }\n });\n\n const columnLength = children[0]!.children.length;\n const columnWidth = CONTENT_WIDTH / columnLength;\n\n return new Table({\n columnWidths: Array.from({ length: columnLength }).map(() => columnWidth),\n rows: children.map((r) => {\n return new TableRow({\n children: r.children.map((c, i) => {\n return new TableCell({\n width: { size: columnWidth, type: \"dxa\" },\n children: [\n new Paragraph({\n alignment: cellAligns?.[i],\n children: ctx.next(c.children),\n }),\n ],\n });\n }),\n });\n }),\n });\n};\n\nconst buildText: NodeBuilder<\"text\"> = ({ value }, { deco }) => {\n return new TextRun({\n text: value,\n bold: deco.bold,\n italics: deco.italic,\n strike: deco.strike,\n });\n};\n\nconst buildEmphasis: NodeBuilder<\"emphasis\"> = ({ children }, ctx) => {\n return ctx.next(children, {\n ...ctx,\n deco: { ...ctx.deco, italic: true },\n });\n};\n\nconst buildStrong: NodeBuilder<\"strong\"> = ({ children }, ctx) => {\n return ctx.next(children, {\n ...ctx,\n deco: { ...ctx.deco, bold: true },\n });\n};\n\nconst buildDelete: NodeBuilder<\"delete\"> = ({ children }, ctx) => {\n return ctx.next(children, {\n ...ctx,\n deco: { ...ctx.deco, strike: true },\n });\n};\n\nconst buildInlineCode: NodeBuilder<\"inlineCode\"> = ({ value }) => {\n return new TextRun({\n text: value,\n highlight: \"lightGray\",\n });\n};\n\nconst buildBreak: NodeBuilder<\"break\"> = () => {\n return new TextRun({ text: \"\", break: 1 });\n};\n\nconst buildLink: NodeBuilder<\"link\"> = ({ children, url }, ctx) => {\n const nodes = ctx.next(children);\n return new ExternalHyperlink({\n link: url,\n children: nodes,\n });\n};\n\nconst buildLinkReference: NodeBuilder<\"linkReference\"> = (\n { children, identifier },\n ctx,\n) => {\n const def = ctx.definition(identifier);\n if (def == null) {\n return ctx.next(children);\n }\n return buildLink({ type: \"link\", children, url: def.url }, ctx);\n};\n\nconst buildFootnoteDefinition: NodeBuilder<\"footnoteDefinition\"> = (\n { children, identifier },\n ctx,\n) => {\n ctx.footnote.def(\n identifier,\n ctx.next(children).filter((c) => c instanceof Paragraph),\n );\n return null;\n};\n\nconst buildFootnoteReference: NodeBuilder<\"footnoteReference\"> = (\n { identifier },\n ctx,\n) => {\n return new FootnoteReferenceRun(ctx.footnote.ref(identifier));\n};\n\nconst noop = () => {\n return null;\n};\n\nconst fallbackText = (node: { type: string; value: string }, ctx: Context) => {\n warnOnce(\n `${node.type} node is not supported without plugins, falling back to text.`,\n );\n return buildText({ type: \"text\", value: node.value }, ctx);\n};\n","import type { Plugin } from \"unified\";\nimport type { Root } from \"mdast\";\nimport { mdastToDocx, type DocxOptions } from \"./mdast-to-docx\";\n\nexport type { DocxOptions };\n\ndeclare module \"unified\" {\n interface CompileResultMap {\n docx: Promise<ArrayBuffer>;\n }\n}\n\nconst plugin: Plugin<[DocxOptions?], Root, Promise<ArrayBuffer>> = function (\n opts = {},\n) {\n this.compiler = (node) => {\n return mdastToDocx(node as Root, opts);\n };\n};\nexport default plugin;\n"],"names":[],"mappings":";;;AAAA,MAAM,aAAa,GAAmC,EAAE;AAExD;;AAEG;SACa,QAAQ,CAAC,OAAe,EAAE,OAAgB,KAAK,EAAA;IAC7D,IAAI,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE;AACpC,QAAA,aAAa,CAAC,OAAO,CAAC,GAAG,IAAI;AAC7B,QAAA,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC;IACvB;AACF;;ACwBA,MAAM,aAAa,GACjB,uBAAuB,CAAC,KAAK;AAC7B,IAAA,qBAAqB,CAAC,IAAI;IAC1B,qBAAqB,CAAC,KAAK;AAC7B,MAAM,gBAAgB,GAAG,SAAS;AAClC,MAAM,MAAM,GAAG,GAAG;AAElB,MAAM,sBAAsB,GAAG,MAAuB;AACpD,IAAA,MAAM,cAAc,GAAG,IAAI,GAAG,EAAkB;AAChD,IAAA,MAAM,IAAI,GAAG,IAAI,GAAG,EAAuB;AAE3C,IAAA,MAAM,KAAK,GAAG,CAAC,EAAU,KAAY;QACnC,IAAI,UAAU,GAAG,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC;AACvC,QAAA,IAAI,UAAU,IAAI,IAAI,EAAE;AACtB,YAAA,cAAc,CAAC,GAAG,CAAC,EAAE,GAAG,UAAU,GAAG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O,IAAI;AACb,CAAC;AAED,MAAM,sBAAsB,GAAqC,CAC/D,EAAE,UAAU,EAAE,EACd,GAAG,KACD;AACF,IAAA,OAAO,IAAI,oBAAoB,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;AAC/D,CAAC;AAED,MAAM,IAAI,GAAG,MAAK;AAChB,IAAA,OAAO,IAAI;AACb,CAAC;AAED,MAAM,YAAY,GAAG,CAAC,IAAqC,EAAE,GAAY,KAAI;AAC3E,IAAA,QAAQ,CACN,CAAA,EAAG,IAAI,CAAC,IAAI,CAAA,6DAAA,CAA+D,CAC5E;AACD,IAAA,OAAO,SAAS,CAAC,EAAgB,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,EAAE,GAAG,CAAC;AAC5D,CAAC;;AC9eD,MAAM,MAAM,GAAuD,UACjE,IAAI,GAAG,EAAE,EAAA;AAET,IAAA,IAAI,CAAC,QAAQ,GAAG,CAAC,IAAI,KAAI;AACvB,QAAA,OAAO,WAAW,CAAC,IAAY,EAAE,IAAI,CAAC;AACxC,IAAA,CAAC;AACH;;;;"}
|
package/lib/mdast-to-docx.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type IPropertiesOptions } from "docx";
|
|
2
|
-
import type * as mdast from "
|
|
2
|
+
import type * as mdast from "mdast";
|
|
3
3
|
import type { RemarkDocxPlugin } from "./types";
|
|
4
4
|
export interface DocxOptions extends Pick<IPropertiesOptions, "title" | "subject" | "creator" | "keywords" | "description" | "lastModifiedBy" | "revision" | "styles" | "background"> {
|
|
5
5
|
/**
|
|
@@ -4,6 +4,23 @@ var docx = require('docx');
|
|
|
4
4
|
var shiki = require('shiki');
|
|
5
5
|
var unistUtilVisit = require('unist-util-visit');
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Format to 6 disit hex
|
|
9
|
+
*/
|
|
10
|
+
const formatHex = (str) => {
|
|
11
|
+
// #RGB
|
|
12
|
+
if (str.length === 4) {
|
|
13
|
+
const r = str[1];
|
|
14
|
+
const g = str[2];
|
|
15
|
+
const b = str[3];
|
|
16
|
+
return str[0] + r + r + g + g + b + b;
|
|
17
|
+
}
|
|
18
|
+
// #RRGGBBAA
|
|
19
|
+
if (str.length === 9) {
|
|
20
|
+
return str.slice(0, 7);
|
|
21
|
+
}
|
|
22
|
+
return str;
|
|
23
|
+
};
|
|
7
24
|
/**
|
|
8
25
|
* A plugin to render "code" nodes, with syntax highlighting powered by shiki.
|
|
9
26
|
*/
|
|
@@ -38,17 +55,30 @@ const shikiPlugin = ({ theme, }) => {
|
|
|
38
55
|
lang: lang,
|
|
39
56
|
theme,
|
|
40
57
|
});
|
|
58
|
+
let { bg, fg } = res;
|
|
59
|
+
if (fg) {
|
|
60
|
+
fg = formatHex(fg);
|
|
61
|
+
}
|
|
62
|
+
if (bg) {
|
|
63
|
+
bg = formatHex(bg);
|
|
64
|
+
}
|
|
41
65
|
return res.tokens.map((r) => {
|
|
42
66
|
return new docx.Paragraph({
|
|
43
67
|
shading: {
|
|
44
68
|
type: "clear",
|
|
45
69
|
color: "auto",
|
|
46
|
-
fill:
|
|
70
|
+
fill: bg,
|
|
47
71
|
},
|
|
48
72
|
children: r.map(({ content, bgColor, color, fontStyle }) => {
|
|
73
|
+
if (color) {
|
|
74
|
+
color = formatHex(color);
|
|
75
|
+
}
|
|
76
|
+
if (bgColor) {
|
|
77
|
+
bgColor = formatHex(bgColor);
|
|
78
|
+
}
|
|
49
79
|
return new docx.TextRun({
|
|
50
80
|
text: content,
|
|
51
|
-
color: color !== null && color !== void 0 ? color :
|
|
81
|
+
color: color !== null && color !== void 0 ? color : fg,
|
|
52
82
|
shading: bgColor
|
|
53
83
|
? {
|
|
54
84
|
type: "clear",
|
|
@@ -59,7 +89,7 @@ const shikiPlugin = ({ theme, }) => {
|
|
|
59
89
|
bold: fontStyle === 2,
|
|
60
90
|
italics: fontStyle === 1,
|
|
61
91
|
underline: fontStyle === 4
|
|
62
|
-
? { type: "single", color:
|
|
92
|
+
? { type: "single", color: fg }
|
|
63
93
|
: undefined,
|
|
64
94
|
strike: fontStyle === 8,
|
|
65
95
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../../src/plugins/code/index.ts"],"sourcesContent":["import { Paragraph, TextRun } from \"docx\";\nimport type { RemarkDocxPlugin } from \"../../types\";\nimport {\n createHighlighter,\n type BundledLanguage,\n type BundledTheme,\n} from \"shiki\";\nimport { visit } from \"unist-util-visit\";\nimport type { FontStyle } from \"shiki/textmate\";\n\nexport interface ShikiPluginOptions {\n /**\n * https://shiki.style/themes\n */\n theme: BundledTheme;\n}\n\n/**\n * A plugin to render \"code\" nodes, with syntax highlighting powered by shiki.\n */\nexport const shikiPlugin = ({\n theme,\n}: ShikiPluginOptions): RemarkDocxPlugin => {\n let highlighter: Awaited<ReturnType<typeof createHighlighter>> | undefined;\n const langs = new Set<string>();\n\n return async ({ root }) => {\n const newLangs: string[] = [];\n\n visit(root, \"code\", ({ lang }) => {\n if (lang) {\n if (!langs.has(lang)) {\n langs.add(lang);\n newLangs.push(lang);\n }\n }\n });\n\n if (!highlighter) {\n highlighter = await createHighlighter({\n themes: [theme],\n langs: [...langs],\n });\n } else {\n await Promise.all(\n newLangs.map((l) => highlighter!.loadLanguage(l as BundledLanguage)),\n );\n }\n\n return {\n code: ({ value, lang }) => {\n if (!lang) {\n return null;\n }\n const res = highlighter!.codeToTokens(value, {\n lang: lang as BundledLanguage,\n theme,\n });\n\n return res.tokens.map((r) => {\n return new Paragraph({\n shading: {\n type: \"clear\",\n color: \"auto\",\n fill:
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../../src/plugins/code/index.ts"],"sourcesContent":["import { Paragraph, TextRun } from \"docx\";\nimport type { RemarkDocxPlugin } from \"../../types\";\nimport {\n createHighlighter,\n type BundledLanguage,\n type BundledTheme,\n} from \"shiki\";\nimport { visit } from \"unist-util-visit\";\nimport type { FontStyle } from \"shiki/textmate\";\n\n/**\n * Format to 6 disit hex\n */\nconst formatHex = (str: string): string => {\n // #RGB\n if (str.length === 4) {\n const r = str[1]!;\n const g = str[2]!;\n const b = str[3]!;\n return str[0]! + r + r + g + g + b + b;\n }\n // #RRGGBBAA\n if (str.length === 9) {\n return str.slice(0, 7);\n }\n return str;\n};\n\nexport interface ShikiPluginOptions {\n /**\n * https://shiki.style/themes\n */\n theme: BundledTheme;\n}\n\n/**\n * A plugin to render \"code\" nodes, with syntax highlighting powered by shiki.\n */\nexport const shikiPlugin = ({\n theme,\n}: ShikiPluginOptions): RemarkDocxPlugin => {\n let highlighter: Awaited<ReturnType<typeof createHighlighter>> | undefined;\n const langs = new Set<string>();\n\n return async ({ root }) => {\n const newLangs: string[] = [];\n\n visit(root, \"code\", ({ lang }) => {\n if (lang) {\n if (!langs.has(lang)) {\n langs.add(lang);\n newLangs.push(lang);\n }\n }\n });\n\n if (!highlighter) {\n highlighter = await createHighlighter({\n themes: [theme],\n langs: [...langs],\n });\n } else {\n await Promise.all(\n newLangs.map((l) => highlighter!.loadLanguage(l as BundledLanguage)),\n );\n }\n\n return {\n code: ({ value, lang }) => {\n if (!lang) {\n return null;\n }\n const res = highlighter!.codeToTokens(value, {\n lang: lang as BundledLanguage,\n theme,\n });\n let { bg, fg } = res;\n if (fg) {\n fg = formatHex(fg);\n }\n if (bg) {\n bg = formatHex(bg);\n }\n\n return res.tokens.map((r) => {\n return new Paragraph({\n shading: {\n type: \"clear\",\n color: \"auto\",\n fill: bg,\n },\n children: r.map(({ content, bgColor, color, fontStyle }) => {\n if (color) {\n color = formatHex(color);\n }\n if (bgColor) {\n bgColor = formatHex(bgColor);\n }\n return new TextRun({\n text: content,\n color: color ?? fg,\n shading: bgColor\n ? {\n type: \"clear\",\n color: \"auto\",\n fill: bgColor,\n }\n : undefined,\n bold: fontStyle === (2 satisfies FontStyle.Bold),\n italics: fontStyle === (1 satisfies FontStyle.Italic),\n underline:\n fontStyle === (4 satisfies FontStyle.Underline)\n ? { type: \"single\", color: fg }\n : undefined,\n strike: fontStyle === (8 satisfies FontStyle.Strikethrough),\n });\n }),\n });\n });\n },\n };\n };\n};\n"],"names":["visit","createHighlighter","Paragraph","TextRun"],"mappings":";;;;;;AAUA;;AAEG;AACH,MAAM,SAAS,GAAG,CAAC,GAAW,KAAY;;AAExC,IAAA,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE;AACpB,QAAA,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAE;AACjB,QAAA,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAE;AACjB,QAAA,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAE;AACjB,QAAA,OAAO,GAAG,CAAC,CAAC,CAAE,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC;IACxC;;AAEA,IAAA,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE;QACpB,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;IACxB;AACA,IAAA,OAAO,GAAG;AACZ,CAAC;AASD;;AAEG;MACU,WAAW,GAAG,CAAC,EAC1B,KAAK,GACc,KAAsB;AACzC,IAAA,IAAI,WAAsE;AAC1E,IAAA,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU;AAE/B,IAAA,OAAO,OAAO,EAAE,IAAI,EAAE,KAAI;QACxB,MAAM,QAAQ,GAAa,EAAE;QAE7BA,oBAAK,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,KAAI;YAC/B,IAAI,IAAI,EAAE;gBACR,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;AACpB,oBAAA,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC;AACf,oBAAA,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC;gBACrB;YACF;AACF,QAAA,CAAC,CAAC;QAEF,IAAI,CAAC,WAAW,EAAE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A,OAAO,GAAG,SAAS,CAAC,OAAO,CAAC;4BAC9B;4BACA,OAAO,IAAIC,YAAO,CAAC;AACjB,gCAAA,IAAI,EAAE,OAAO;AACb,gCAAA,KAAK,EAAE,KAAK,KAAA,IAAA,IAAL,KAAK,KAAA,MAAA,GAAL,KAAK,GAAI,EAAE;AAClB,gCAAA,OAAO,EAAE;AACP,sCAAE;AACE,wCAAA,IAAI,EAAE,OAAO;AACb,wCAAA,KAAK,EAAE,MAAM;AACb,wCAAA,IAAI,EAAE,OAAO;AACd;AACH,sCAAE,SAAS;gCACb,IAAI,EAAE,SAAS,KAAM,CAA2B;gCAChD,OAAO,EAAE,SAAS,KAAM,CAA6B;gCACrD,SAAS,EACP,SAAS,KAAM;sCACX,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE;AAC7B,sCAAE,SAAS;gCACf,MAAM,EAAE,SAAS,KAAM,CAAoC;AAC5D,6BAAA,CAAC;AACJ,wBAAA,CAAC,CAAC;AACH,qBAAA,CAAC;AACJ,gBAAA,CAAC,CAAC;YACJ,CAAC;SACF;AACH,IAAA,CAAC;AACH;;;;"}
|
|
@@ -2,6 +2,23 @@ import { Paragraph, TextRun } from 'docx';
|
|
|
2
2
|
import { createHighlighter } from 'shiki';
|
|
3
3
|
import { visit } from 'unist-util-visit';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Format to 6 disit hex
|
|
7
|
+
*/
|
|
8
|
+
const formatHex = (str) => {
|
|
9
|
+
// #RGB
|
|
10
|
+
if (str.length === 4) {
|
|
11
|
+
const r = str[1];
|
|
12
|
+
const g = str[2];
|
|
13
|
+
const b = str[3];
|
|
14
|
+
return str[0] + r + r + g + g + b + b;
|
|
15
|
+
}
|
|
16
|
+
// #RRGGBBAA
|
|
17
|
+
if (str.length === 9) {
|
|
18
|
+
return str.slice(0, 7);
|
|
19
|
+
}
|
|
20
|
+
return str;
|
|
21
|
+
};
|
|
5
22
|
/**
|
|
6
23
|
* A plugin to render "code" nodes, with syntax highlighting powered by shiki.
|
|
7
24
|
*/
|
|
@@ -36,17 +53,30 @@ const shikiPlugin = ({ theme, }) => {
|
|
|
36
53
|
lang: lang,
|
|
37
54
|
theme,
|
|
38
55
|
});
|
|
56
|
+
let { bg, fg } = res;
|
|
57
|
+
if (fg) {
|
|
58
|
+
fg = formatHex(fg);
|
|
59
|
+
}
|
|
60
|
+
if (bg) {
|
|
61
|
+
bg = formatHex(bg);
|
|
62
|
+
}
|
|
39
63
|
return res.tokens.map((r) => {
|
|
40
64
|
return new Paragraph({
|
|
41
65
|
shading: {
|
|
42
66
|
type: "clear",
|
|
43
67
|
color: "auto",
|
|
44
|
-
fill:
|
|
68
|
+
fill: bg,
|
|
45
69
|
},
|
|
46
70
|
children: r.map(({ content, bgColor, color, fontStyle }) => {
|
|
71
|
+
if (color) {
|
|
72
|
+
color = formatHex(color);
|
|
73
|
+
}
|
|
74
|
+
if (bgColor) {
|
|
75
|
+
bgColor = formatHex(bgColor);
|
|
76
|
+
}
|
|
47
77
|
return new TextRun({
|
|
48
78
|
text: content,
|
|
49
|
-
color: color !== null && color !== void 0 ? color :
|
|
79
|
+
color: color !== null && color !== void 0 ? color : fg,
|
|
50
80
|
shading: bgColor
|
|
51
81
|
? {
|
|
52
82
|
type: "clear",
|
|
@@ -57,7 +87,7 @@ const shikiPlugin = ({ theme, }) => {
|
|
|
57
87
|
bold: fontStyle === 2,
|
|
58
88
|
italics: fontStyle === 1,
|
|
59
89
|
underline: fontStyle === 4
|
|
60
|
-
? { type: "single", color:
|
|
90
|
+
? { type: "single", color: fg }
|
|
61
91
|
: undefined,
|
|
62
92
|
strike: fontStyle === 8,
|
|
63
93
|
});
|