reactive-md 0.6.0 → 0.6.1
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/CHANGELOG-ch.md +203 -194
- package/CHANGELOG.md +203 -194
- package/CONTRIBUTING-ch.md +67 -67
- package/CONTRIBUTING.md +69 -69
- package/README-ch.md +157 -157
- package/README-en.md +184 -184
- package/README.md +184 -184
- package/SECURITY-ch.md +81 -81
- package/SECURITY.md +81 -81
- package/THIRD_PARTY_NOTICES.md +313 -209
- package/bin/reactive-md.mjs +39 -39
- package/cli/args.mjs +181 -181
- package/cli/assets.mjs +390 -390
- package/cli/build.mjs +268 -268
- package/cli/document.mjs +130 -130
- package/cli/errors.mjs +61 -61
- package/cli/fs-utils.mjs +133 -133
- package/cli/glob.mjs +203 -203
- package/cli/help.mjs +138 -138
- package/cli/lint.mjs +177 -177
- package/cli/main.mjs +55 -55
- package/cli/migrate.mjs +368 -368
- package/cli/package.mjs +210 -210
- package/dist-library/{authoring-DSC0VsNK.js → authoring-BEVyth5X.js} +1 -1
- package/dist-library/compiler-Cm3ppMxG.js +46044 -0
- package/dist-library/{react-Dk94rcOp.js → react-Bh7_oxZp.js} +20110 -20113
- package/dist-library/reactive-md-authoring.js +2 -2
- package/dist-library/reactive-md-print.css +284 -284
- package/dist-library/reactive-md-react-unstyled.js +1 -1
- package/dist-library/reactive-md-react.js +1 -1
- package/dist-library/reactive-md.css +1 -1
- package/dist-library/reactive-md.js +3 -3
- package/dist-library/{runtime-D8KbnCd0.js → runtime-DlFYQ4s_.js} +1 -1
- package/dist-library/types/local/math-display.d.ts +22 -23
- package/dist-library/types/local/math-view.d.ts +6 -7
- package/dist-standalone/reactive-md-standalone.css +1 -1
- package/dist-standalone/reactive-md-standalone.js +318 -62
- package/docs/compatibility/compatibility-ch.md +121 -119
- package/docs/compatibility/compatibility-en.md +121 -119
- package/docs/compatibility/source-versioning-ch.md +159 -159
- package/docs/compatibility/source-versioning-en.md +159 -159
- package/docs/development/component-development-ch.md +210 -210
- package/docs/development/component-development-en.md +210 -210
- package/docs/development/component-protocol-ch.md +747 -747
- package/docs/development/component-protocol-en.md +747 -747
- package/docs/development/performance-ch.md +76 -76
- package/docs/development/performance-en.md +76 -76
- package/docs/development/releasing-ch.md +108 -108
- package/docs/development/releasing-en.md +108 -108
- package/docs/integration/author-tools-ch.md +62 -62
- package/docs/integration/author-tools-en.md +62 -62
- package/docs/integration/shareable-export-ch.md +209 -209
- package/docs/integration/shareable-export-en.md +211 -211
- package/docs/reference/computations-formatting-ch.md +87 -87
- package/docs/reference/computations-formatting-en.md +87 -87
- package/docs/reference/constraints-collections-ch.md +84 -84
- package/docs/reference/constraints-collections-en.md +84 -84
- package/docs/reference/diagnostics-counting-ch.md +104 -104
- package/docs/reference/diagnostics-counting-en.md +104 -104
- package/docs/reference/local-components-v2-ch.md +138 -138
- package/docs/reference/local-components-v2-en.md +104 -104
- package/docs/reference/local-interaction-syntax-ch.md +515 -511
- package/docs/reference/local-interaction-syntax-en.md +511 -507
- package/docs/reference/theme-protocol-ch.md +107 -107
- package/docs/reference/theme-protocol-en.md +107 -107
- package/docs/releases/release-0.4.0-ch.md +89 -89
- package/docs/releases/release-0.4.0-en.md +89 -89
- package/docs/releases/release-0.4.1-ch.md +23 -23
- package/docs/releases/release-0.4.1-en.md +23 -23
- package/docs/releases/release-0.6.1-ch.md +27 -0
- package/docs/releases/release-0.6.1-en.md +27 -0
- package/examples/conformance/README.md +53 -53
- package/examples/conformance/component-protocol-1.json +482 -482
- package/examples/conformance/component-protocol-1.schema.json +76 -76
- package/examples/conformance/local-draft-1.json +50 -50
- package/examples/conformance/local-draft-2.json +212 -212
- package/examples/integration/README.md +47 -47
- package/examples/integration/core/index.mjs +32 -32
- package/examples/integration/worker/index.html +19 -19
- package/examples/integration/worker/main.js +61 -61
- package/examples/integration/worker/package.json +13 -13
- package/examples/integration/worker/worker.js +3 -3
- package/examples/local/README.md +74 -74
- package/examples/local/charts.reactive.md +73 -73
- package/examples/local/recipes/invoice-zh.reactive.md +36 -36
- package/examples/local/recipes/invoice.reactive.md +36 -36
- package/examples/local/recipes/workshop-zh.reactive.md +33 -33
- package/examples/local/recipes/workshop.reactive.md +33 -33
- package/examples/local/showcase/README.md +25 -25
- package/examples/local/showcase/explore.reactive.md +102 -102
- package/examples/local/showcase/flow.reactive.md +86 -86
- package/examples/local/showcase/forms.reactive.md +126 -126
- package/examples/local/showcase/functions.reactive.md +119 -119
- package/examples/local/showcase/layout.reactive.md +100 -100
- package/examples/local/showcase/statistics.reactive.md +108 -108
- package/examples/local/showcase/tasks.reactive.md +89 -89
- package/examples/local/showcase/zh/explore.reactive.md +102 -102
- package/examples/local/showcase/zh/flow.reactive.md +86 -86
- package/examples/local/showcase/zh/forms.reactive.md +126 -126
- package/examples/local/showcase/zh/functions.reactive.md +119 -119
- package/examples/local/showcase/zh/layout.reactive.md +100 -100
- package/examples/local/showcase/zh/statistics.reactive.md +108 -108
- package/examples/local/showcase/zh/tasks.reactive.md +89 -89
- package/package.json +152 -152
- package/dist-library/compiler-CRD88BMt.js +0 -24964
package/cli/build.mjs
CHANGED
|
@@ -1,268 +1,268 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { collectImageNodes, collectInlineAssets, createAssetPolicy } from './assets.mjs';
|
|
3
|
-
import { displayPath, extractDocumentTitle, formatDiagnostic } from './document.mjs';
|
|
4
|
-
import { CliError, EXIT, errorText } from './errors.mjs';
|
|
5
|
-
import {
|
|
6
|
-
assertDistinctPaths,
|
|
7
|
-
assertOutputDirectory,
|
|
8
|
-
atomicWriteUtf8,
|
|
9
|
-
readTextFile,
|
|
10
|
-
} from './fs-utils.mjs';
|
|
11
|
-
import {
|
|
12
|
-
loadCore,
|
|
13
|
-
loadReact,
|
|
14
|
-
loadReactRenderer,
|
|
15
|
-
readArtifactText,
|
|
16
|
-
readNotices,
|
|
17
|
-
readPackageInfo,
|
|
18
|
-
} from './package.mjs';
|
|
19
|
-
|
|
20
|
-
/** @param {NodeJS.WritableStream} stream @param {string} file @param {any[]} diagnostics */
|
|
21
|
-
function writeDiagnostics(stream, file, diagnostics) {
|
|
22
|
-
for (const diagnostic of diagnostics) {
|
|
23
|
-
stream.write(`${formatDiagnostic(file, diagnostic)}\n`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* `reactive-md build`
|
|
29
|
-
* @param {any} options
|
|
30
|
-
* @param {{packageRoot: string, stdout: NodeJS.WritableStream, stderr: NodeJS.WritableStream}} context
|
|
31
|
-
*/
|
|
32
|
-
export async function runBuild(options, context) {
|
|
33
|
-
const { packageRoot, stdout, stderr } = context;
|
|
34
|
-
const inputPath = path.resolve(options.input);
|
|
35
|
-
const outputPath = path.resolve(options.output);
|
|
36
|
-
const fileLabel = displayPath(inputPath);
|
|
37
|
-
|
|
38
|
-
const source = await readTextFile(inputPath, {
|
|
39
|
-
label: 'Cannot read input document',
|
|
40
|
-
exitCode: EXIT.IO,
|
|
41
|
-
});
|
|
42
|
-
await assertDistinctPaths(inputPath, outputPath);
|
|
43
|
-
await assertOutputDirectory(outputPath);
|
|
44
|
-
|
|
45
|
-
const packageInfo = await readPackageInfo(packageRoot);
|
|
46
|
-
const generator = { name: 'reactive-md', version: packageInfo.version };
|
|
47
|
-
|
|
48
|
-
const core = await loadCore(packageRoot, [
|
|
49
|
-
'compileLocal',
|
|
50
|
-
'createLocalRuntime',
|
|
51
|
-
'createLocalStateFile',
|
|
52
|
-
'resolveLocalStateBaseline',
|
|
53
|
-
'createShareableHtml',
|
|
54
|
-
]);
|
|
55
|
-
|
|
56
|
-
let compiled;
|
|
57
|
-
try {
|
|
58
|
-
compiled = core.compileLocal(source);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
throw new CliError(
|
|
61
|
-
`The compiler failed on "${fileLabel}": ${errorText(error)}`,
|
|
62
|
-
EXIT.DOCUMENT,
|
|
63
|
-
{ cause: error },
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
if (!compiled || typeof compiled !== 'object' || !Array.isArray(compiled.diagnostics)) {
|
|
67
|
-
throw new CliError('The core build returned an invalid compile result. Run "npm run build" again.', EXIT.DOCUMENT);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const diagnostics = compiled.diagnostics;
|
|
71
|
-
const errors = diagnostics.filter((diagnostic) => diagnostic?.severity === 'error');
|
|
72
|
-
const warnings = diagnostics.filter((diagnostic) => diagnostic?.severity === 'warning');
|
|
73
|
-
if (compiled.ok !== true || errors.length > 0) {
|
|
74
|
-
writeDiagnostics(stderr, fileLabel, diagnostics);
|
|
75
|
-
if (diagnostics.length === 0) {
|
|
76
|
-
stderr.write(`reactive-md: error: "${fileLabel}" could not be compiled.\n`);
|
|
77
|
-
}
|
|
78
|
-
throw new CliError('', EXIT.DOCUMENT, { reported: true });
|
|
79
|
-
}
|
|
80
|
-
if (warnings.length > 0) {
|
|
81
|
-
writeDiagnostics(stderr, fileLabel, warnings);
|
|
82
|
-
if (options.strict) {
|
|
83
|
-
stderr.write('reactive-md: error: warnings are treated as errors because --strict is set.\n');
|
|
84
|
-
throw new CliError('', EXIT.DOCUMENT, { reported: true });
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/* ------------------------------------------------------------ state baseline */
|
|
89
|
-
let outcome;
|
|
90
|
-
let stateLabel = fileLabel;
|
|
91
|
-
try {
|
|
92
|
-
if (options.stateFile !== undefined) {
|
|
93
|
-
const statePath = path.resolve(options.stateFile);
|
|
94
|
-
stateLabel = displayPath(statePath);
|
|
95
|
-
const stateText = await readTextFile(statePath, {
|
|
96
|
-
label: 'Cannot read state file',
|
|
97
|
-
exitCode: EXIT.IO,
|
|
98
|
-
});
|
|
99
|
-
outcome = core.resolveLocalStateBaseline(compiled.document, stateText);
|
|
100
|
-
} else {
|
|
101
|
-
const generated = core.createLocalStateFile(
|
|
102
|
-
compiled.document,
|
|
103
|
-
compiled.document.initialState,
|
|
104
|
-
generator,
|
|
105
|
-
);
|
|
106
|
-
outcome = core.resolveLocalStateBaseline(compiled.document, generated);
|
|
107
|
-
}
|
|
108
|
-
} catch (error) {
|
|
109
|
-
if (error instanceof CliError) throw error;
|
|
110
|
-
throw new CliError(
|
|
111
|
-
`Cannot resolve the exported state for "${fileLabel}": ${errorText(error)}`,
|
|
112
|
-
EXIT.DOCUMENT,
|
|
113
|
-
{ cause: error },
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (!outcome || outcome.ok !== true || !outcome.baseline) {
|
|
118
|
-
const stateDiagnostics = Array.isArray(outcome?.diagnostics) ? outcome.diagnostics : [];
|
|
119
|
-
if (stateDiagnostics.length > 0) {
|
|
120
|
-
for (const diagnostic of stateDiagnostics) {
|
|
121
|
-
// Parsing/digest/parameter-set failures belong to the state file.
|
|
122
|
-
// Runtime constraint failures keep their document node position.
|
|
123
|
-
const code = typeof diagnostic?.code === 'string' ? diagnostic.code : '';
|
|
124
|
-
const diagnosticFile = code.startsWith('local.state.') ? stateLabel : fileLabel;
|
|
125
|
-
stderr.write(`${formatDiagnostic(diagnosticFile, diagnostic)}\n`);
|
|
126
|
-
}
|
|
127
|
-
} else {
|
|
128
|
-
stderr.write(`reactive-md: error: the exported state could not be resolved for "${fileLabel}".\n`);
|
|
129
|
-
}
|
|
130
|
-
throw new CliError('', EXIT.DOCUMENT, { reported: true });
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const baseline = outcome.baseline;
|
|
134
|
-
let stateFile;
|
|
135
|
-
try {
|
|
136
|
-
stateFile = core.createLocalStateFile(baseline.document, baseline.state, generator);
|
|
137
|
-
} catch (error) {
|
|
138
|
-
throw new CliError(
|
|
139
|
-
`Cannot finalize the exported state for "${fileLabel}": ${errorText(error)}`,
|
|
140
|
-
EXIT.DOCUMENT,
|
|
141
|
-
{ cause: error },
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/* ------------------------------------------------------------------- assets */
|
|
146
|
-
const imageNodes = collectImageNodes(baseline.document.nodes);
|
|
147
|
-
/** @type {Map<string, string>} */
|
|
148
|
-
let byUrl = new Map();
|
|
149
|
-
// The shell records how many image nodes the document contains, including
|
|
150
|
-
// images the author chose to omit. Inline payloads are deduplicated
|
|
151
|
-
// separately by content.
|
|
152
|
-
const imageCount = imageNodes.length;
|
|
153
|
-
|
|
154
|
-
if (options.assets === 'omit') {
|
|
155
|
-
if (imageNodes.length > 0) {
|
|
156
|
-
stderr.write(
|
|
157
|
-
`reactive-md: warning: ${imageNodes.length} image(s) will render as alt text because --assets omit was selected.\n`,
|
|
158
|
-
);
|
|
159
|
-
if (options.strict) {
|
|
160
|
-
throw new CliError(
|
|
161
|
-
`--strict treats omitted images as an error; ${imageNodes.length} image(s) would not be inlined. `
|
|
162
|
-
+ 'Re-run without --assets omit or without --strict.',
|
|
163
|
-
EXIT.ASSET,
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
} else if (imageNodes.length > 0) {
|
|
168
|
-
const collected = await collectInlineAssets({
|
|
169
|
-
document: baseline.document,
|
|
170
|
-
inputPath,
|
|
171
|
-
assetRoot: options.assetRoot,
|
|
172
|
-
});
|
|
173
|
-
byUrl = collected.byUrl;
|
|
174
|
-
}
|
|
175
|
-
const { urlPolicy, imageResolver } = createAssetPolicy(byUrl);
|
|
176
|
-
|
|
177
|
-
/* ------------------------------------------------------------- build inputs */
|
|
178
|
-
const css = `${await readArtifactText(packageRoot, 'css')}\n${await readArtifactText(packageRoot, 'printCss')}`;
|
|
179
|
-
let runtimeScript;
|
|
180
|
-
if (options.mode === 'interactive') {
|
|
181
|
-
runtimeScript = await readArtifactText(packageRoot, 'standalone');
|
|
182
|
-
}
|
|
183
|
-
// The shared shell records verified image URLs in the auto-mount payload so
|
|
184
|
-
// the standalone renderer can hydrate the same data URLs without a global
|
|
185
|
-
// image-policy relaxation. Static output needs only the SSR markup.
|
|
186
|
-
const embeddedImages = options.mode === 'interactive' && byUrl.size > 0
|
|
187
|
-
? Object.fromEntries(byUrl)
|
|
188
|
-
: undefined;
|
|
189
|
-
const { createElement, renderToString } = await loadReact();
|
|
190
|
-
const { LocalDocumentView } = await loadReactRenderer(packageRoot);
|
|
191
|
-
const notices = await readNotices(packageRoot);
|
|
192
|
-
|
|
193
|
-
/* -------------------------------------------------------------------- render */
|
|
194
|
-
let runtime;
|
|
195
|
-
try {
|
|
196
|
-
runtime = core.createLocalRuntime(baseline.document);
|
|
197
|
-
} catch (error) {
|
|
198
|
-
throw new CliError(
|
|
199
|
-
`Cannot create the document runtime for "${fileLabel}": ${errorText(error)}`,
|
|
200
|
-
EXIT.DOCUMENT,
|
|
201
|
-
{ cause: error },
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
let staticMarkup;
|
|
206
|
-
try {
|
|
207
|
-
const props = {
|
|
208
|
-
document: baseline.document,
|
|
209
|
-
runtime,
|
|
210
|
-
mode: options.mode,
|
|
211
|
-
locale: options.locale,
|
|
212
|
-
colorScheme: options.colorScheme,
|
|
213
|
-
urlPolicy,
|
|
214
|
-
imageResolver,
|
|
215
|
-
};
|
|
216
|
-
if (options.mode === 'interactive') props.staticFirst = true;
|
|
217
|
-
staticMarkup = renderToString(createElement(LocalDocumentView, props));
|
|
218
|
-
} catch (error) {
|
|
219
|
-
throw new CliError(
|
|
220
|
-
`HTML rendering failed for "${fileLabel}": ${errorText(error)}`,
|
|
221
|
-
EXIT.DOCUMENT,
|
|
222
|
-
{ cause: error },
|
|
223
|
-
);
|
|
224
|
-
} finally {
|
|
225
|
-
try {
|
|
226
|
-
runtime.dispose();
|
|
227
|
-
} catch {
|
|
228
|
-
/* Disposal is always best effort; the render error is more important. */
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (typeof staticMarkup !== 'string' || staticMarkup.trim() === '') {
|
|
233
|
-
throw new CliError(`The React renderer produced empty markup for "${fileLabel}".`, EXIT.DOCUMENT);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const explicitTitle = options.title !== undefined ? options.title.trim() : '';
|
|
237
|
-
const title = explicitTitle || extractDocumentTitle(baseline.document, path.basename(inputPath));
|
|
238
|
-
|
|
239
|
-
let html;
|
|
240
|
-
try {
|
|
241
|
-
html = core.createShareableHtml({
|
|
242
|
-
title,
|
|
243
|
-
source: baseline.document.source,
|
|
244
|
-
state: stateFile,
|
|
245
|
-
staticMarkup,
|
|
246
|
-
css,
|
|
247
|
-
mode: options.mode,
|
|
248
|
-
...(runtimeScript === undefined ? {} : { runtimeScript }),
|
|
249
|
-
locale: options.locale,
|
|
250
|
-
colorScheme: options.colorScheme,
|
|
251
|
-
generator,
|
|
252
|
-
assets: options.assets,
|
|
253
|
-
imageCount,
|
|
254
|
-
...(embeddedImages === undefined ? {} : { images: embeddedImages }),
|
|
255
|
-
notices,
|
|
256
|
-
});
|
|
257
|
-
} catch (error) {
|
|
258
|
-
throw new CliError(
|
|
259
|
-
`Cannot assemble the shareable HTML for "${fileLabel}": ${errorText(error)}`,
|
|
260
|
-
EXIT.DOCUMENT,
|
|
261
|
-
{ cause: error },
|
|
262
|
-
);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
await atomicWriteUtf8(outputPath, html);
|
|
266
|
-
stdout.write(`Wrote ${options.output}\n`);
|
|
267
|
-
return EXIT.OK;
|
|
268
|
-
}
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { collectImageNodes, collectInlineAssets, createAssetPolicy } from './assets.mjs';
|
|
3
|
+
import { displayPath, extractDocumentTitle, formatDiagnostic } from './document.mjs';
|
|
4
|
+
import { CliError, EXIT, errorText } from './errors.mjs';
|
|
5
|
+
import {
|
|
6
|
+
assertDistinctPaths,
|
|
7
|
+
assertOutputDirectory,
|
|
8
|
+
atomicWriteUtf8,
|
|
9
|
+
readTextFile,
|
|
10
|
+
} from './fs-utils.mjs';
|
|
11
|
+
import {
|
|
12
|
+
loadCore,
|
|
13
|
+
loadReact,
|
|
14
|
+
loadReactRenderer,
|
|
15
|
+
readArtifactText,
|
|
16
|
+
readNotices,
|
|
17
|
+
readPackageInfo,
|
|
18
|
+
} from './package.mjs';
|
|
19
|
+
|
|
20
|
+
/** @param {NodeJS.WritableStream} stream @param {string} file @param {any[]} diagnostics */
|
|
21
|
+
function writeDiagnostics(stream, file, diagnostics) {
|
|
22
|
+
for (const diagnostic of diagnostics) {
|
|
23
|
+
stream.write(`${formatDiagnostic(file, diagnostic)}\n`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* `reactive-md build`
|
|
29
|
+
* @param {any} options
|
|
30
|
+
* @param {{packageRoot: string, stdout: NodeJS.WritableStream, stderr: NodeJS.WritableStream}} context
|
|
31
|
+
*/
|
|
32
|
+
export async function runBuild(options, context) {
|
|
33
|
+
const { packageRoot, stdout, stderr } = context;
|
|
34
|
+
const inputPath = path.resolve(options.input);
|
|
35
|
+
const outputPath = path.resolve(options.output);
|
|
36
|
+
const fileLabel = displayPath(inputPath);
|
|
37
|
+
|
|
38
|
+
const source = await readTextFile(inputPath, {
|
|
39
|
+
label: 'Cannot read input document',
|
|
40
|
+
exitCode: EXIT.IO,
|
|
41
|
+
});
|
|
42
|
+
await assertDistinctPaths(inputPath, outputPath);
|
|
43
|
+
await assertOutputDirectory(outputPath);
|
|
44
|
+
|
|
45
|
+
const packageInfo = await readPackageInfo(packageRoot);
|
|
46
|
+
const generator = { name: 'reactive-md', version: packageInfo.version };
|
|
47
|
+
|
|
48
|
+
const core = await loadCore(packageRoot, [
|
|
49
|
+
'compileLocal',
|
|
50
|
+
'createLocalRuntime',
|
|
51
|
+
'createLocalStateFile',
|
|
52
|
+
'resolveLocalStateBaseline',
|
|
53
|
+
'createShareableHtml',
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
let compiled;
|
|
57
|
+
try {
|
|
58
|
+
compiled = core.compileLocal(source);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw new CliError(
|
|
61
|
+
`The compiler failed on "${fileLabel}": ${errorText(error)}`,
|
|
62
|
+
EXIT.DOCUMENT,
|
|
63
|
+
{ cause: error },
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (!compiled || typeof compiled !== 'object' || !Array.isArray(compiled.diagnostics)) {
|
|
67
|
+
throw new CliError('The core build returned an invalid compile result. Run "npm run build" again.', EXIT.DOCUMENT);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const diagnostics = compiled.diagnostics;
|
|
71
|
+
const errors = diagnostics.filter((diagnostic) => diagnostic?.severity === 'error');
|
|
72
|
+
const warnings = diagnostics.filter((diagnostic) => diagnostic?.severity === 'warning');
|
|
73
|
+
if (compiled.ok !== true || errors.length > 0) {
|
|
74
|
+
writeDiagnostics(stderr, fileLabel, diagnostics);
|
|
75
|
+
if (diagnostics.length === 0) {
|
|
76
|
+
stderr.write(`reactive-md: error: "${fileLabel}" could not be compiled.\n`);
|
|
77
|
+
}
|
|
78
|
+
throw new CliError('', EXIT.DOCUMENT, { reported: true });
|
|
79
|
+
}
|
|
80
|
+
if (warnings.length > 0) {
|
|
81
|
+
writeDiagnostics(stderr, fileLabel, warnings);
|
|
82
|
+
if (options.strict) {
|
|
83
|
+
stderr.write('reactive-md: error: warnings are treated as errors because --strict is set.\n');
|
|
84
|
+
throw new CliError('', EXIT.DOCUMENT, { reported: true });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* ------------------------------------------------------------ state baseline */
|
|
89
|
+
let outcome;
|
|
90
|
+
let stateLabel = fileLabel;
|
|
91
|
+
try {
|
|
92
|
+
if (options.stateFile !== undefined) {
|
|
93
|
+
const statePath = path.resolve(options.stateFile);
|
|
94
|
+
stateLabel = displayPath(statePath);
|
|
95
|
+
const stateText = await readTextFile(statePath, {
|
|
96
|
+
label: 'Cannot read state file',
|
|
97
|
+
exitCode: EXIT.IO,
|
|
98
|
+
});
|
|
99
|
+
outcome = core.resolveLocalStateBaseline(compiled.document, stateText);
|
|
100
|
+
} else {
|
|
101
|
+
const generated = core.createLocalStateFile(
|
|
102
|
+
compiled.document,
|
|
103
|
+
compiled.document.initialState,
|
|
104
|
+
generator,
|
|
105
|
+
);
|
|
106
|
+
outcome = core.resolveLocalStateBaseline(compiled.document, generated);
|
|
107
|
+
}
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if (error instanceof CliError) throw error;
|
|
110
|
+
throw new CliError(
|
|
111
|
+
`Cannot resolve the exported state for "${fileLabel}": ${errorText(error)}`,
|
|
112
|
+
EXIT.DOCUMENT,
|
|
113
|
+
{ cause: error },
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!outcome || outcome.ok !== true || !outcome.baseline) {
|
|
118
|
+
const stateDiagnostics = Array.isArray(outcome?.diagnostics) ? outcome.diagnostics : [];
|
|
119
|
+
if (stateDiagnostics.length > 0) {
|
|
120
|
+
for (const diagnostic of stateDiagnostics) {
|
|
121
|
+
// Parsing/digest/parameter-set failures belong to the state file.
|
|
122
|
+
// Runtime constraint failures keep their document node position.
|
|
123
|
+
const code = typeof diagnostic?.code === 'string' ? diagnostic.code : '';
|
|
124
|
+
const diagnosticFile = code.startsWith('local.state.') ? stateLabel : fileLabel;
|
|
125
|
+
stderr.write(`${formatDiagnostic(diagnosticFile, diagnostic)}\n`);
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
stderr.write(`reactive-md: error: the exported state could not be resolved for "${fileLabel}".\n`);
|
|
129
|
+
}
|
|
130
|
+
throw new CliError('', EXIT.DOCUMENT, { reported: true });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const baseline = outcome.baseline;
|
|
134
|
+
let stateFile;
|
|
135
|
+
try {
|
|
136
|
+
stateFile = core.createLocalStateFile(baseline.document, baseline.state, generator);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
throw new CliError(
|
|
139
|
+
`Cannot finalize the exported state for "${fileLabel}": ${errorText(error)}`,
|
|
140
|
+
EXIT.DOCUMENT,
|
|
141
|
+
{ cause: error },
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/* ------------------------------------------------------------------- assets */
|
|
146
|
+
const imageNodes = collectImageNodes(baseline.document.nodes);
|
|
147
|
+
/** @type {Map<string, string>} */
|
|
148
|
+
let byUrl = new Map();
|
|
149
|
+
// The shell records how many image nodes the document contains, including
|
|
150
|
+
// images the author chose to omit. Inline payloads are deduplicated
|
|
151
|
+
// separately by content.
|
|
152
|
+
const imageCount = imageNodes.length;
|
|
153
|
+
|
|
154
|
+
if (options.assets === 'omit') {
|
|
155
|
+
if (imageNodes.length > 0) {
|
|
156
|
+
stderr.write(
|
|
157
|
+
`reactive-md: warning: ${imageNodes.length} image(s) will render as alt text because --assets omit was selected.\n`,
|
|
158
|
+
);
|
|
159
|
+
if (options.strict) {
|
|
160
|
+
throw new CliError(
|
|
161
|
+
`--strict treats omitted images as an error; ${imageNodes.length} image(s) would not be inlined. `
|
|
162
|
+
+ 'Re-run without --assets omit or without --strict.',
|
|
163
|
+
EXIT.ASSET,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
} else if (imageNodes.length > 0) {
|
|
168
|
+
const collected = await collectInlineAssets({
|
|
169
|
+
document: baseline.document,
|
|
170
|
+
inputPath,
|
|
171
|
+
assetRoot: options.assetRoot,
|
|
172
|
+
});
|
|
173
|
+
byUrl = collected.byUrl;
|
|
174
|
+
}
|
|
175
|
+
const { urlPolicy, imageResolver } = createAssetPolicy(byUrl);
|
|
176
|
+
|
|
177
|
+
/* ------------------------------------------------------------- build inputs */
|
|
178
|
+
const css = `${await readArtifactText(packageRoot, 'css')}\n${await readArtifactText(packageRoot, 'printCss')}`;
|
|
179
|
+
let runtimeScript;
|
|
180
|
+
if (options.mode === 'interactive') {
|
|
181
|
+
runtimeScript = await readArtifactText(packageRoot, 'standalone');
|
|
182
|
+
}
|
|
183
|
+
// The shared shell records verified image URLs in the auto-mount payload so
|
|
184
|
+
// the standalone renderer can hydrate the same data URLs without a global
|
|
185
|
+
// image-policy relaxation. Static output needs only the SSR markup.
|
|
186
|
+
const embeddedImages = options.mode === 'interactive' && byUrl.size > 0
|
|
187
|
+
? Object.fromEntries(byUrl)
|
|
188
|
+
: undefined;
|
|
189
|
+
const { createElement, renderToString } = await loadReact();
|
|
190
|
+
const { LocalDocumentView } = await loadReactRenderer(packageRoot);
|
|
191
|
+
const notices = await readNotices(packageRoot);
|
|
192
|
+
|
|
193
|
+
/* -------------------------------------------------------------------- render */
|
|
194
|
+
let runtime;
|
|
195
|
+
try {
|
|
196
|
+
runtime = core.createLocalRuntime(baseline.document);
|
|
197
|
+
} catch (error) {
|
|
198
|
+
throw new CliError(
|
|
199
|
+
`Cannot create the document runtime for "${fileLabel}": ${errorText(error)}`,
|
|
200
|
+
EXIT.DOCUMENT,
|
|
201
|
+
{ cause: error },
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let staticMarkup;
|
|
206
|
+
try {
|
|
207
|
+
const props = {
|
|
208
|
+
document: baseline.document,
|
|
209
|
+
runtime,
|
|
210
|
+
mode: options.mode,
|
|
211
|
+
locale: options.locale,
|
|
212
|
+
colorScheme: options.colorScheme,
|
|
213
|
+
urlPolicy,
|
|
214
|
+
imageResolver,
|
|
215
|
+
};
|
|
216
|
+
if (options.mode === 'interactive') props.staticFirst = true;
|
|
217
|
+
staticMarkup = renderToString(createElement(LocalDocumentView, props));
|
|
218
|
+
} catch (error) {
|
|
219
|
+
throw new CliError(
|
|
220
|
+
`HTML rendering failed for "${fileLabel}": ${errorText(error)}`,
|
|
221
|
+
EXIT.DOCUMENT,
|
|
222
|
+
{ cause: error },
|
|
223
|
+
);
|
|
224
|
+
} finally {
|
|
225
|
+
try {
|
|
226
|
+
runtime.dispose();
|
|
227
|
+
} catch {
|
|
228
|
+
/* Disposal is always best effort; the render error is more important. */
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (typeof staticMarkup !== 'string' || staticMarkup.trim() === '') {
|
|
233
|
+
throw new CliError(`The React renderer produced empty markup for "${fileLabel}".`, EXIT.DOCUMENT);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const explicitTitle = options.title !== undefined ? options.title.trim() : '';
|
|
237
|
+
const title = explicitTitle || extractDocumentTitle(baseline.document, path.basename(inputPath));
|
|
238
|
+
|
|
239
|
+
let html;
|
|
240
|
+
try {
|
|
241
|
+
html = core.createShareableHtml({
|
|
242
|
+
title,
|
|
243
|
+
source: baseline.document.source,
|
|
244
|
+
state: stateFile,
|
|
245
|
+
staticMarkup,
|
|
246
|
+
css,
|
|
247
|
+
mode: options.mode,
|
|
248
|
+
...(runtimeScript === undefined ? {} : { runtimeScript }),
|
|
249
|
+
locale: options.locale,
|
|
250
|
+
colorScheme: options.colorScheme,
|
|
251
|
+
generator,
|
|
252
|
+
assets: options.assets,
|
|
253
|
+
imageCount,
|
|
254
|
+
...(embeddedImages === undefined ? {} : { images: embeddedImages }),
|
|
255
|
+
notices,
|
|
256
|
+
});
|
|
257
|
+
} catch (error) {
|
|
258
|
+
throw new CliError(
|
|
259
|
+
`Cannot assemble the shareable HTML for "${fileLabel}": ${errorText(error)}`,
|
|
260
|
+
EXIT.DOCUMENT,
|
|
261
|
+
{ cause: error },
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
await atomicWriteUtf8(outputPath, html);
|
|
266
|
+
stdout.write(`Wrote ${options.output}\n`);
|
|
267
|
+
return EXIT.OK;
|
|
268
|
+
}
|