-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmd-to-docx.mjs
More file actions
186 lines (159 loc) · 5.09 KB
/
Copy pathmd-to-docx.mjs
File metadata and controls
186 lines (159 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import {
convertMarkdownFileInNode,
resolveNodeStyleOptions,
} from '@marktodocx/runtime-node';
function printUsage() {
console.log([
'Usage:',
' node md-to-docx.mjs <input.md> [output.docx] [options]',
'',
'Options:',
' --style-preset <name> Base style preset (default|minimal|report)',
' --margin-preset <name> Page margin preset (default|compact|wide)',
' --style-json <json|path> Style options JSON string or JSON file path',
' --set <key=value> Targeted style override, repeatable',
' --help, -h Show this help',
'',
'Environment defaults:',
' MARKTODOCX_STYLE_PRESET',
' MARKTODOCX_MARGIN_PRESET',
' MARKTODOCX_STYLE_JSON',
' MARKTODOCX_STYLE_SET',
'',
'Examples:',
' node md-to-docx.mjs report.md',
' node md-to-docx.mjs report.md output.docx --style-preset minimal',
' node md-to-docx.mjs report.md --style-json ./style-options.json --set body.fontSizePt=12',
].join('\n'));
}
function readOptionValue(currentArg, argv, index, optionName) {
const equalsIndex = currentArg.indexOf('=');
if (equalsIndex >= 0) {
return {
value: currentArg.slice(equalsIndex + 1),
nextIndex: index,
};
}
if (index + 1 >= argv.length) {
throw new Error(`${optionName} requires a value`);
}
return {
value: argv[index + 1],
nextIndex: index + 1,
};
}
function parseArgs(argv) {
const options = {
styleSet: [],
};
const positional = [];
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === '--help' || arg === '-h') {
options.help = true;
continue;
}
if (arg === '--style-preset' || arg.startsWith('--style-preset=')) {
const result = readOptionValue(arg, argv, index, '--style-preset');
options.stylePreset = result.value;
index = result.nextIndex;
continue;
}
if (arg === '--margin-preset' || arg.startsWith('--margin-preset=')) {
const result = readOptionValue(arg, argv, index, '--margin-preset');
options.marginPreset = result.value;
index = result.nextIndex;
continue;
}
if (arg === '--style-json' || arg.startsWith('--style-json=')) {
const result = readOptionValue(arg, argv, index, '--style-json');
options.styleJson = result.value;
index = result.nextIndex;
continue;
}
if (arg === '--set' || arg.startsWith('--set=')) {
const result = readOptionValue(arg, argv, index, '--set');
options.styleSet.push(result.value);
index = result.nextIndex;
continue;
}
if (arg.startsWith('-')) {
throw new Error(`Unknown option: ${arg}`);
}
positional.push(arg);
}
if (options.help) {
return options;
}
if (positional.length === 0 || positional.length > 2) {
throw new Error('CLI requires an input markdown path and accepts at most one output path');
}
options.inputPath = path.resolve(process.cwd(), positional[0]);
options.outputPath = positional[1] ? path.resolve(process.cwd(), positional[1]) : undefined;
return options;
}
function isOptionalModuleMissing(error) {
return error?.code === 'ERR_MODULE_NOT_FOUND'
&& String(error.message || '').includes('@marktodocx/runtime-node-mermaid');
}
async function createOptionalMermaidRenderer(markdown) {
if (!markdown.includes('```mermaid')) {
return null;
}
try {
const { createPuppeteerMermaidRenderer } = await import('@marktodocx/runtime-node-mermaid');
return createPuppeteerMermaidRenderer();
} catch (error) {
if (isOptionalModuleMissing(error)) {
throw new Error(
'This document contains Mermaid diagrams. Install @marktodocx/runtime-node-mermaid to enable Mermaid rendering on the Node CLI path.'
);
}
throw error;
}
}
async function main() {
let options;
try {
options = parseArgs(process.argv.slice(2));
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
console.error('');
printUsage();
process.exit(1);
}
if (options.help) {
printUsage();
return;
}
let mermaidRenderer = null;
try {
const markdown = await fs.readFile(options.inputPath, 'utf8');
const styleOptions = await resolveNodeStyleOptions({
cwd: process.cwd(),
env: process.env,
stylePreset: options.stylePreset,
marginPreset: options.marginPreset,
styleJson: options.styleJson,
styleSet: options.styleSet,
});
mermaidRenderer = await createOptionalMermaidRenderer(markdown);
const result = await convertMarkdownFileInNode({
inputPath: options.inputPath,
outputPath: options.outputPath,
styleOptions,
renderMermaid: mermaidRenderer?.renderMermaidToImageTag?.bind(mermaidRenderer),
});
console.log(`Wrote ${result.outputPath}`);
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
process.exitCode = 1;
} finally {
await mermaidRenderer?.close?.();
}
}
main();