remark-docx 0.3.0 → 0.3.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 +6 -21
- package/lib/index.cjs +82 -89
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +83 -90
- package/lib/index.js.map +1 -1
- package/lib/mdast-to-docx.d.ts +1 -1
- package/lib/plugins/image/index.cjs +46 -68
- package/lib/plugins/image/index.cjs.map +1 -1
- package/lib/plugins/image/index.d.ts +5 -9
- package/lib/plugins/image/index.js +46 -67
- package/lib/plugins/image/index.js.map +1 -1
- package/lib/plugins/math/index.cjs.map +1 -1
- package/lib/plugins/math/index.d.ts +1 -1
- package/lib/plugins/math/index.js.map +1 -1
- package/lib/types.d.ts +14 -0
- package/package.json +1 -1
- package/lib/plugins/types.d.ts +0 -15
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { 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
4
|
/**
|
|
@@ -20,69 +20,6 @@ function warnOnce(message, cond = false) {
|
|
|
20
20
|
|
|
21
21
|
const ORDERED_LIST_REF = "ordered";
|
|
22
22
|
const INDENT = 0.5;
|
|
23
|
-
const DEFAULT_NUMBERINGS = [
|
|
24
|
-
{
|
|
25
|
-
level: 0,
|
|
26
|
-
format: LevelFormat.DECIMAL,
|
|
27
|
-
text: "%1.",
|
|
28
|
-
alignment: AlignmentType.START,
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
level: 1,
|
|
32
|
-
format: LevelFormat.DECIMAL,
|
|
33
|
-
text: "%2.",
|
|
34
|
-
alignment: AlignmentType.START,
|
|
35
|
-
style: {
|
|
36
|
-
paragraph: {
|
|
37
|
-
indent: { start: convertInchesToTwip(INDENT * 1) },
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
level: 2,
|
|
43
|
-
format: LevelFormat.DECIMAL,
|
|
44
|
-
text: "%3.",
|
|
45
|
-
alignment: AlignmentType.START,
|
|
46
|
-
style: {
|
|
47
|
-
paragraph: {
|
|
48
|
-
indent: { start: convertInchesToTwip(INDENT * 2) },
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
level: 3,
|
|
54
|
-
format: LevelFormat.DECIMAL,
|
|
55
|
-
text: "%4.",
|
|
56
|
-
alignment: AlignmentType.START,
|
|
57
|
-
style: {
|
|
58
|
-
paragraph: {
|
|
59
|
-
indent: { start: convertInchesToTwip(INDENT * 3) },
|
|
60
|
-
},
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
level: 4,
|
|
65
|
-
format: LevelFormat.DECIMAL,
|
|
66
|
-
text: "%5.",
|
|
67
|
-
alignment: AlignmentType.START,
|
|
68
|
-
style: {
|
|
69
|
-
paragraph: {
|
|
70
|
-
indent: { start: convertInchesToTwip(INDENT * 4) },
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
level: 5,
|
|
76
|
-
format: LevelFormat.DECIMAL,
|
|
77
|
-
text: "%6.",
|
|
78
|
-
alignment: AlignmentType.START,
|
|
79
|
-
style: {
|
|
80
|
-
paragraph: {
|
|
81
|
-
indent: { start: convertInchesToTwip(INDENT * 5) },
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
];
|
|
86
23
|
const createFootnoteRegistry = () => {
|
|
87
24
|
const idToInternalId = new Map();
|
|
88
25
|
const defs = new Map();
|
|
@@ -101,7 +38,7 @@ const createFootnoteRegistry = () => {
|
|
|
101
38
|
const internalId = getId(id);
|
|
102
39
|
defs.set(internalId, def);
|
|
103
40
|
},
|
|
104
|
-
|
|
41
|
+
toConfig: () => {
|
|
105
42
|
return defs.entries().reduce((acc, [key, def]) => {
|
|
106
43
|
acc[key] = def;
|
|
107
44
|
return acc;
|
|
@@ -111,11 +48,74 @@ const createFootnoteRegistry = () => {
|
|
|
111
48
|
};
|
|
112
49
|
const createNumberingRegistry = () => {
|
|
113
50
|
let counter = 1;
|
|
51
|
+
const DEFAULT_NUMBERINGS = [
|
|
52
|
+
{
|
|
53
|
+
level: 0,
|
|
54
|
+
format: LevelFormat.DECIMAL,
|
|
55
|
+
text: "%1.",
|
|
56
|
+
alignment: AlignmentType.START,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
level: 1,
|
|
60
|
+
format: LevelFormat.DECIMAL,
|
|
61
|
+
text: "%2.",
|
|
62
|
+
alignment: AlignmentType.START,
|
|
63
|
+
style: {
|
|
64
|
+
paragraph: {
|
|
65
|
+
indent: { start: convertInchesToTwip(INDENT * 1) },
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
level: 2,
|
|
71
|
+
format: LevelFormat.DECIMAL,
|
|
72
|
+
text: "%3.",
|
|
73
|
+
alignment: AlignmentType.START,
|
|
74
|
+
style: {
|
|
75
|
+
paragraph: {
|
|
76
|
+
indent: { start: convertInchesToTwip(INDENT * 2) },
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
level: 3,
|
|
82
|
+
format: LevelFormat.DECIMAL,
|
|
83
|
+
text: "%4.",
|
|
84
|
+
alignment: AlignmentType.START,
|
|
85
|
+
style: {
|
|
86
|
+
paragraph: {
|
|
87
|
+
indent: { start: convertInchesToTwip(INDENT * 3) },
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
level: 4,
|
|
93
|
+
format: LevelFormat.DECIMAL,
|
|
94
|
+
text: "%5.",
|
|
95
|
+
alignment: AlignmentType.START,
|
|
96
|
+
style: {
|
|
97
|
+
paragraph: {
|
|
98
|
+
indent: { start: convertInchesToTwip(INDENT * 4) },
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
level: 5,
|
|
104
|
+
format: LevelFormat.DECIMAL,
|
|
105
|
+
text: "%6.",
|
|
106
|
+
alignment: AlignmentType.START,
|
|
107
|
+
style: {
|
|
108
|
+
paragraph: {
|
|
109
|
+
indent: { start: convertInchesToTwip(INDENT * 5) },
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
];
|
|
114
114
|
return {
|
|
115
115
|
create: () => {
|
|
116
116
|
return `${ORDERED_LIST_REF}-${counter++}`;
|
|
117
117
|
},
|
|
118
|
-
|
|
118
|
+
toConfig: () => {
|
|
119
119
|
return Array.from({ length: counter }, (_, i) => ({
|
|
120
120
|
reference: `${ORDERED_LIST_REF}-${i}`,
|
|
121
121
|
levels: DEFAULT_NUMBERINGS,
|
|
@@ -125,9 +125,9 @@ const createNumberingRegistry = () => {
|
|
|
125
125
|
};
|
|
126
126
|
const mdastToDocx = async (node, { plugins = [], title, subject, creator, keywords, description, lastModifiedBy, revision, styles, background, }) => {
|
|
127
127
|
const definition = definitions(node);
|
|
128
|
-
const pluginCtx = { root: node, definition };
|
|
129
128
|
const footnote = createFootnoteRegistry();
|
|
130
129
|
const numbering = createNumberingRegistry();
|
|
130
|
+
const pluginCtx = { root: node, definition };
|
|
131
131
|
const nodes = convertNodes(node.children, {
|
|
132
132
|
overrides: (await Promise.all(plugins.map((p) => p(pluginCtx)))).reduceRight((acc, p) => ({ acc, ...p }), {}),
|
|
133
133
|
deco: {},
|
|
@@ -146,10 +146,10 @@ const mdastToDocx = async (node, { plugins = [], title, subject, creator, keywor
|
|
|
146
146
|
revision,
|
|
147
147
|
styles,
|
|
148
148
|
background,
|
|
149
|
-
footnotes: footnote.
|
|
149
|
+
footnotes: footnote.toConfig(),
|
|
150
150
|
sections: [{ children: nodes }],
|
|
151
151
|
numbering: {
|
|
152
|
-
config: numbering.
|
|
152
|
+
config: numbering.toConfig(),
|
|
153
153
|
},
|
|
154
154
|
});
|
|
155
155
|
return Packer.toArrayBuffer(doc);
|
|
@@ -338,7 +338,7 @@ const buildBlockquote = ({ children }, ctx) => {
|
|
|
338
338
|
indent: ctx.indent + 1,
|
|
339
339
|
});
|
|
340
340
|
};
|
|
341
|
-
const buildList = ({ children, ordered
|
|
341
|
+
const buildList = ({ children, ordered }, ctx) => {
|
|
342
342
|
var _a;
|
|
343
343
|
const isTopLevel = !ctx.list;
|
|
344
344
|
const list = {
|
|
@@ -355,7 +355,7 @@ const buildList = ({ children, ordered, start: _start, spread: _spread }, ctx) =
|
|
|
355
355
|
});
|
|
356
356
|
});
|
|
357
357
|
};
|
|
358
|
-
const buildListItem = ({ children, checked
|
|
358
|
+
const buildListItem = ({ children, checked }, ctx) => {
|
|
359
359
|
return convertNodes(children, {
|
|
360
360
|
...ctx,
|
|
361
361
|
...(ctx.list && { list: { ...ctx.list, checked: checked !== null && checked !== void 0 ? checked : undefined } }),
|
|
@@ -376,28 +376,21 @@ const buildTable = ({ children, align }, ctx) => {
|
|
|
376
376
|
});
|
|
377
377
|
return new Table({
|
|
378
378
|
rows: children.map((r) => {
|
|
379
|
-
return
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
379
|
+
return new TableRow({
|
|
380
|
+
children: r.children.map((c, i) => {
|
|
381
|
+
return new TableCell({
|
|
382
|
+
children: [
|
|
383
|
+
new Paragraph({
|
|
384
|
+
alignment: cellAligns === null || cellAligns === void 0 ? void 0 : cellAligns[i],
|
|
385
|
+
children: convertNodes(c.children, ctx),
|
|
386
|
+
}),
|
|
387
|
+
],
|
|
388
|
+
});
|
|
389
|
+
}),
|
|
390
|
+
});
|
|
387
391
|
}),
|
|
388
392
|
});
|
|
389
393
|
};
|
|
390
|
-
const buildTableCell = ({ children }, ctx, align) => {
|
|
391
|
-
const nodes = convertNodes(children, ctx);
|
|
392
|
-
return new TableCell({
|
|
393
|
-
children: [
|
|
394
|
-
new Paragraph({
|
|
395
|
-
alignment: align,
|
|
396
|
-
children: nodes,
|
|
397
|
-
}),
|
|
398
|
-
],
|
|
399
|
-
});
|
|
400
|
-
};
|
|
401
394
|
const buildHtml = ({ value }) => {
|
|
402
395
|
// FIXME: transform to text for now
|
|
403
396
|
return new Paragraph({
|
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} from \"docx\";\nimport type * as mdast from \"./mdast\";\nimport { invariant, warnOnce } from \"./utils\";\nimport { definitions, type GetDefinition } from \"mdast-util-definitions\";\nimport type { DocxChild, DocxContent } from \"./types\";\nimport type { NodeOverrides, RemarkDocxPlugin } from \"./plugins/types\";\n\nconst ORDERED_LIST_REF = \"ordered\";\nconst INDENT = 0.5;\nconst 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\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 footnotes: () => {\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 footnotes: () => {\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 getAll: () => Array<{ reference: string; levels: ILevelsOptions[] }>;\n};\n\nconst createNumberingRegistry = (): NumberingRegistry => {\n let counter = 1;\n\n return {\n create: () => {\n return `${ORDERED_LIST_REF}-${counter++}`;\n },\n getAll: () => {\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 pluginCtx = { root: node, definition };\n const footnote = createFootnoteRegistry();\n const numbering = createNumberingRegistry();\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 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.footnotes(),\n sections: [{ children: nodes as DocxChild[] }],\n numbering: {\n config: numbering.getAll(),\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 results.push(buildHtml(node));\n break;\n case \"code\":\n results.push(buildCode(node));\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 // FIXME: transform to text for now\n results.push(buildText(node.value, ctx.deco));\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, start: _start, spread: _spread }: 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, spread: _spread }: 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 return new Table({\n rows: children.map((r) => {\n return buildTableRow(r, ctx, cellAligns);\n }),\n });\n};\n\nconst buildTableRow = (\n { children }: mdast.TableRow,\n ctx: Context,\n cellAligns: (typeof AlignmentType)[keyof typeof AlignmentType][] | undefined,\n): TableRow => {\n return new TableRow({\n children: children.map((c, i) => {\n return buildTableCell(c, ctx, cellAligns?.[i]);\n }),\n });\n};\n\nconst buildTableCell = (\n { children }: mdast.TableCell,\n ctx: Context,\n align: (typeof AlignmentType)[keyof typeof AlignmentType] | undefined,\n): TableCell => {\n const nodes = convertNodes(children, ctx);\n return new TableCell({\n children: [\n new Paragraph({\n alignment: align,\n children: nodes,\n }),\n ],\n });\n};\n\nconst buildHtml = ({ value }: mdast.HTML): DocxContent => {\n // FIXME: transform to text for now\n return new Paragraph({\n children: [buildText(value, {})],\n });\n};\n\nconst buildCode = ({\n value,\n lang: _lang,\n meta: _meta,\n}: mdast.Code): DocxContent => {\n // FIXME: transform to text for now\n return new Paragraph({\n children: [buildText(value, {})],\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 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;AA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I,oBAAoB,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;AAC/D,CAAC;;AC1lBD,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":["/**\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} 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 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 results.push(buildHtml(node));\n break;\n case \"code\":\n results.push(buildCode(node));\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 // FIXME: transform to text for now\n results.push(buildText(node.value, ctx.deco));\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 return new Table({\n rows: children.map((r) => {\n return new TableRow({\n children: r.children.map((c, i) => {\n return new TableCell({\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 buildHtml = ({ value }: mdast.HTML): DocxContent => {\n // FIXME: transform to text for now\n return new Paragraph({\n children: [buildText(value, {})],\n });\n};\n\nconst buildCode = ({\n value,\n lang: _lang,\n meta: _meta,\n}: mdast.Code): DocxContent => {\n // FIXME: transform to text for now\n return new Paragraph({\n children: [buildText(value, {})],\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 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;;ACWA,MAAM,gBAAgB,GAAG,SAAS;AAClC,MAAM,MAAM,GAA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,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;;AChlBD,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,6 +1,6 @@
|
|
|
1
1
|
import { type IPropertiesOptions } from "docx";
|
|
2
2
|
import type * as mdast from "./mdast";
|
|
3
|
-
import type { RemarkDocxPlugin } from "./
|
|
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
|
/**
|
|
6
6
|
* Plugins to customize how mdast nodes are transformed.
|
|
@@ -3,25 +3,44 @@
|
|
|
3
3
|
var utils = require('../../utils-EYEfXxbh.js');
|
|
4
4
|
var docx = require('docx');
|
|
5
5
|
var unistUtilVisit = require('unist-util-visit');
|
|
6
|
+
var imageSize = require('image-size');
|
|
6
7
|
|
|
7
|
-
const buildImage = ({
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const buildImage = ({ image, width, height, type }) => {
|
|
9
|
+
return new docx.ImageRun({
|
|
10
|
+
type: type,
|
|
11
|
+
data: image,
|
|
12
|
+
transformation: {
|
|
13
|
+
width,
|
|
14
|
+
height,
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
const supportedTypes = ["png", "jpg", "gif", "bmp", "svg"];
|
|
19
|
+
const isSupportedType = (type) => {
|
|
20
|
+
if (!type)
|
|
21
|
+
return false;
|
|
22
|
+
if (supportedTypes.includes(type)) {
|
|
23
|
+
return true;
|
|
11
24
|
}
|
|
12
|
-
|
|
13
|
-
return [
|
|
14
|
-
new docx.ImageRun({
|
|
15
|
-
type: data.type,
|
|
16
|
-
data: image,
|
|
17
|
-
transformation: {
|
|
18
|
-
width,
|
|
19
|
-
height,
|
|
20
|
-
},
|
|
21
|
-
}),
|
|
22
|
-
];
|
|
25
|
+
return false;
|
|
23
26
|
};
|
|
24
|
-
|
|
27
|
+
/**
|
|
28
|
+
* A plugin to render "image" nodes
|
|
29
|
+
*/
|
|
30
|
+
const imagePlugin = ({ load = async (url) => {
|
|
31
|
+
const res = await fetch(url);
|
|
32
|
+
return res.arrayBuffer();
|
|
33
|
+
}, } = {}) => {
|
|
34
|
+
const resolver = async (url) => {
|
|
35
|
+
const buf = await load(url);
|
|
36
|
+
const { width, height, type } = imageSize.imageSize(new Uint8Array(buf));
|
|
37
|
+
if (!isSupportedType(type)) {
|
|
38
|
+
const err = `Not supported image type: ${type}`;
|
|
39
|
+
utils.warnOnce(err);
|
|
40
|
+
throw new Error(err);
|
|
41
|
+
}
|
|
42
|
+
return { image: buf, width, height, type };
|
|
43
|
+
};
|
|
25
44
|
const images = new Map();
|
|
26
45
|
return async ({ root, definition }) => {
|
|
27
46
|
const imageList = [];
|
|
@@ -53,67 +72,26 @@ const imagePlugin = (resolver) => {
|
|
|
53
72
|
}
|
|
54
73
|
return {
|
|
55
74
|
image: (node) => {
|
|
56
|
-
|
|
75
|
+
const data = images.get(node.url);
|
|
76
|
+
if (!data) {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
return buildImage(data);
|
|
57
80
|
},
|
|
58
81
|
imageReference: ({ identifier }) => {
|
|
59
82
|
const def = definition(identifier);
|
|
60
83
|
if (def == null) {
|
|
61
84
|
return [];
|
|
62
85
|
}
|
|
63
|
-
|
|
86
|
+
const data = images.get(def.url);
|
|
87
|
+
if (!data) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
return buildImage(data);
|
|
64
91
|
},
|
|
65
92
|
};
|
|
66
93
|
};
|
|
67
94
|
};
|
|
68
|
-
/**
|
|
69
|
-
* A plugin to render "image" nodes on browser.
|
|
70
|
-
*/
|
|
71
|
-
const browserImagePlugin = () => imagePlugin(async (url) => {
|
|
72
|
-
const image = new Image();
|
|
73
|
-
const res = await fetch(url);
|
|
74
|
-
const buf = await res.arrayBuffer();
|
|
75
|
-
return new Promise((resolve, reject) => {
|
|
76
|
-
image.onload = () => {
|
|
77
|
-
resolve({
|
|
78
|
-
image: buf,
|
|
79
|
-
width: image.naturalWidth,
|
|
80
|
-
height: image.naturalHeight,
|
|
81
|
-
type: "png",
|
|
82
|
-
});
|
|
83
|
-
};
|
|
84
|
-
image.onerror = reject;
|
|
85
|
-
image.src = URL.createObjectURL(new Blob([buf], { type: "image/png" }));
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
/**
|
|
89
|
-
* A plugin to render "image" nodes on Node.js.
|
|
90
|
-
*/
|
|
91
|
-
const nodeImagePlugin = ({ load = async (url) => {
|
|
92
|
-
const res = await fetch(url);
|
|
93
|
-
return res.arrayBuffer();
|
|
94
|
-
}, }) => {
|
|
95
|
-
const is = import('image-size');
|
|
96
|
-
const isSupportedType = (type) => {
|
|
97
|
-
if (!type)
|
|
98
|
-
return false;
|
|
99
|
-
if (type === "png" || type === "jpg" || type === "gif" || type === "bmp") {
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
return false;
|
|
103
|
-
};
|
|
104
|
-
return imagePlugin(async (url) => {
|
|
105
|
-
const { imageSize } = await is;
|
|
106
|
-
const buf = await load(url);
|
|
107
|
-
const { width, height, type } = imageSize(new Uint8Array(buf));
|
|
108
|
-
if (!isSupportedType(type)) {
|
|
109
|
-
const err = `Not supported image type: ${type}`;
|
|
110
|
-
utils.warnOnce(err);
|
|
111
|
-
throw new Error(err);
|
|
112
|
-
}
|
|
113
|
-
return { image: buf, width, height, type };
|
|
114
|
-
});
|
|
115
|
-
};
|
|
116
95
|
|
|
117
|
-
exports.
|
|
118
|
-
exports.nodeImagePlugin = nodeImagePlugin;
|
|
96
|
+
exports.imagePlugin = imagePlugin;
|
|
119
97
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../../src/plugins/image/index.ts"],"sourcesContent":["import { warnOnce } from \"../../utils\";\nimport type { RemarkDocxPlugin } from \"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../../src/plugins/image/index.ts"],"sourcesContent":["import { warnOnce } from \"../../utils\";\nimport type { RemarkDocxPlugin } from \"../../types\";\nimport type * as mdast from \"../../mdast\";\nimport { ImageRun, type IImageOptions } from \"docx\";\nimport { visit } from \"unist-util-visit\";\nimport { imageSize } from \"image-size\";\n\ntype ImageData = {\n image: IImageOptions[\"data\"];\n width: number;\n height: number;\n type: IImageOptions[\"type\"];\n};\n\nconst buildImage = ({ image, width, height, type }: ImageData) => {\n return new ImageRun({\n type: type,\n data: image,\n transformation: {\n width,\n height,\n },\n } as IImageOptions);\n};\n\nconst supportedTypes = [\"png\", \"jpg\", \"gif\", \"bmp\", \"svg\"] as const;\n\nconst isSupportedType = (\n type: string | undefined,\n): type is (typeof supportedTypes)[number] => {\n if (!type) return false;\n if ((supportedTypes as readonly string[]).includes(type)) {\n return true;\n }\n return false;\n};\n\ninterface ImagePluginOptions {\n /**\n * A function to resolve image data from url.\n * @default fetch\n */\n load?: (url: string) => Promise<ArrayBuffer>;\n}\n\n/**\n * A plugin to render \"image\" nodes\n */\nexport const imagePlugin = ({\n load = async (url) => {\n const res = await fetch(url);\n return res.arrayBuffer();\n },\n}: ImagePluginOptions = {}): RemarkDocxPlugin => {\n const resolver = async (url: string): Promise<ImageData> => {\n const buf = await load(url);\n\n const { width, height, type } = imageSize(new Uint8Array(buf));\n if (!isSupportedType(type)) {\n const err = `Not supported image type: ${type}`;\n warnOnce(err);\n throw new Error(err);\n }\n return { image: buf, width, height, type };\n };\n\n const images = new Map<string, ImageData>();\n\n return async ({ root, definition }) => {\n const imageList: (mdast.Image | mdast.Definition)[] = [];\n visit(root, \"image\", (node) => {\n imageList.push(node);\n });\n visit(root, \"imageReference\", (node) => {\n const maybeImage = definition(node.identifier)!;\n if (maybeImage) {\n imageList.push(maybeImage);\n }\n });\n\n if (imageList.length !== 0) {\n const promises = new Map<string, Promise<void>>();\n imageList.forEach(({ url }) => {\n if (!images.has(url) && !promises.has(url)) {\n promises.set(\n url,\n (async () => {\n try {\n const img = await resolver(url);\n images.set(url, img);\n } catch (e) {\n warnOnce(`Failed to fetch image: ${url}`);\n }\n })(),\n );\n }\n });\n\n await Promise.all(promises.values());\n }\n\n return {\n image: (node) => {\n const data = images.get(node.url);\n if (!data) {\n return [];\n }\n return buildImage(data);\n },\n imageReference: ({ identifier }) => {\n const def = definition(identifier);\n if (def == null) {\n return [];\n }\n const data = images.get(def.url);\n if (!data) {\n return [];\n }\n return buildImage(data);\n },\n };\n };\n};\n"],"names":["ImageRun","imageSize","warnOnce","visit"],"mappings":";;;;;;;AAcA,MAAM,UAAU,GAAG,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAa,KAAI;IAC/D,OAAO,IAAIA,aAAQ,CAA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CAAC,IAAI,KAAI;gBACd,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;gBACjC,IAAI,CAAC,IAAI,EAAE;AACT,oBAAA,OAAO,EAAE;gBACX;AACA,gBAAA,OAAO,UAAU,CAAC,IAAI,CAAC;YACzB,CAAC;AACD,YAAA,cAAc,EAAE,CAAC,EAAE,UAAU,EAAE,KAAI;AACjC,gBAAA,MAAM,GAAG,GAAG,UAAU,CAAC,UAAU,CAAC;AAClC,gBAAA,IAAI,GAAG,IAAI,IAAI,EAAE;AACf,oBAAA,OAAO,EAAE;gBACX;gBACA,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC;gBAChC,IAAI,CAAC,IAAI,EAAE;AACT,oBAAA,OAAO,EAAE;gBACX;AACA,gBAAA,OAAO,UAAU,CAAC,IAAI,CAAC;YACzB,CAAC;SACF;AACH,IAAA,CAAC;AACH;;;;"}
|
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
import type { RemarkDocxPlugin } from "
|
|
2
|
-
|
|
3
|
-
* A plugin to render "image" nodes on browser.
|
|
4
|
-
*/
|
|
5
|
-
export declare const browserImagePlugin: () => RemarkDocxPlugin;
|
|
6
|
-
interface NodeImagePluginOptions {
|
|
1
|
+
import type { RemarkDocxPlugin } from "../../types";
|
|
2
|
+
interface ImagePluginOptions {
|
|
7
3
|
/**
|
|
8
4
|
* A function to resolve image data from url.
|
|
9
5
|
* @default fetch
|
|
10
6
|
*/
|
|
11
|
-
load
|
|
7
|
+
load?: (url: string) => Promise<ArrayBuffer>;
|
|
12
8
|
}
|
|
13
9
|
/**
|
|
14
|
-
* A plugin to render "image" nodes
|
|
10
|
+
* A plugin to render "image" nodes
|
|
15
11
|
*/
|
|
16
|
-
export declare const
|
|
12
|
+
export declare const imagePlugin: ({ load, }?: ImagePluginOptions) => RemarkDocxPlugin;
|
|
17
13
|
export {};
|