reactive-md 0.5.3 → 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.
Files changed (141) hide show
  1. package/CHANGELOG-ch.md +195 -165
  2. package/CHANGELOG.md +195 -165
  3. package/CONTRIBUTING-ch.md +67 -62
  4. package/CONTRIBUTING.md +69 -64
  5. package/README-ch.md +157 -155
  6. package/README-en.md +184 -181
  7. package/README.md +184 -181
  8. package/SECURITY-ch.md +81 -81
  9. package/SECURITY.md +81 -81
  10. package/THIRD_PARTY_NOTICES.md +585 -1
  11. package/bin/reactive-md.mjs +39 -39
  12. package/cli/args.mjs +181 -181
  13. package/cli/assets.mjs +390 -390
  14. package/cli/build.mjs +268 -268
  15. package/cli/document.mjs +130 -130
  16. package/cli/errors.mjs +61 -61
  17. package/cli/fs-utils.mjs +133 -133
  18. package/cli/glob.mjs +203 -203
  19. package/cli/help.mjs +138 -138
  20. package/cli/lint.mjs +177 -177
  21. package/cli/main.mjs +55 -55
  22. package/cli/migrate.mjs +368 -368
  23. package/cli/package.mjs +210 -210
  24. package/dist-library/{authoring-DJJjKYg9.js → authoring-BEVyth5X.js} +58 -58
  25. package/dist-library/compiler-Cm3ppMxG.js +46044 -0
  26. package/dist-library/react-Bh7_oxZp.js +59655 -0
  27. package/dist-library/reactive-md-authoring.js +2 -2
  28. package/dist-library/reactive-md-editor.js +13 -0
  29. package/dist-library/reactive-md-print.css +284 -284
  30. package/dist-library/reactive-md-react-unstyled.js +1 -1
  31. package/dist-library/reactive-md-react.js +1 -1
  32. package/dist-library/reactive-md.css +1 -1
  33. package/dist-library/reactive-md.js +123 -123
  34. package/dist-library/{runtime-DXtle_6c.js → runtime-DlFYQ4s_.js} +151 -151
  35. package/dist-library/types/editor/types.d.ts +2 -0
  36. package/dist-library/types/local/code-block.d.ts +3 -1
  37. package/dist-library/types/local/code-languages.d.ts +7 -0
  38. package/dist-library/types/local/compiled-data.d.ts +6 -0
  39. package/dist-library/types/local/compiler.d.ts +1 -0
  40. package/dist-library/types/local/diagnostics.d.ts +31 -0
  41. package/dist-library/types/local/diagram-layout.d.ts +69 -0
  42. package/dist-library/types/local/html-names.d.ts +6 -0
  43. package/dist-library/types/local/html-table.d.ts +25 -0
  44. package/dist-library/types/local/math-display.d.ts +22 -22
  45. package/dist-library/types/local/math-view.d.ts +6 -7
  46. package/dist-library/types/local/table-surface.d.ts +10 -0
  47. package/dist-library/types/local/types.d.ts +2 -0
  48. package/dist-library/types/local/visuals.d.ts +9 -0
  49. package/dist-standalone/reactive-md-standalone.css +1 -1
  50. package/dist-standalone/reactive-md-standalone.js +331 -51
  51. package/docs/README.md +84 -0
  52. package/docs/{compatibility-ch.md → compatibility/compatibility-ch.md} +121 -109
  53. package/docs/{compatibility-en.md → compatibility/compatibility-en.md} +121 -109
  54. package/docs/{source-versioning-ch.md → compatibility/source-versioning-ch.md} +159 -159
  55. package/docs/{source-versioning-en.md → compatibility/source-versioning-en.md} +159 -159
  56. package/docs/{component-development-ch.md → development/component-development-ch.md} +210 -210
  57. package/docs/{component-development-en.md → development/component-development-en.md} +210 -210
  58. package/docs/{component-protocol-ch.md → development/component-protocol-ch.md} +747 -747
  59. package/docs/{component-protocol-en.md → development/component-protocol-en.md} +747 -747
  60. package/docs/{performance-ch.md → development/performance-ch.md} +76 -76
  61. package/docs/{performance-en.md → development/performance-en.md} +76 -76
  62. package/docs/{releasing-ch.md → development/releasing-ch.md} +108 -108
  63. package/docs/{releasing-en.md → development/releasing-en.md} +108 -108
  64. package/docs/guides/local-guide-ch.md +513 -0
  65. package/docs/guides/local-guide-en.md +513 -0
  66. package/docs/{author-tools-ch.md → integration/author-tools-ch.md} +57 -57
  67. package/docs/{author-tools-en.md → integration/author-tools-en.md} +57 -57
  68. package/docs/{editor-ch.md → integration/editor-ch.md} +7 -6
  69. package/docs/{editor-en.md → integration/editor-en.md} +7 -6
  70. package/docs/{local-integration-ch.md → integration/local-integration-ch.md} +33 -15
  71. package/docs/{local-integration-en.md → integration/local-integration-en.md} +33 -15
  72. package/docs/{shareable-export-ch.md → integration/shareable-export-ch.md} +221 -209
  73. package/docs/{shareable-export-en.md → integration/shareable-export-en.md} +223 -211
  74. package/docs/{computations-formatting-ch.md → reference/computations-formatting-ch.md} +87 -87
  75. package/docs/{computations-formatting-en.md → reference/computations-formatting-en.md} +87 -87
  76. package/docs/{constraints-collections-ch.md → reference/constraints-collections-ch.md} +84 -84
  77. package/docs/{constraints-collections-en.md → reference/constraints-collections-en.md} +84 -84
  78. package/docs/{diagnostics-counting-ch.md → reference/diagnostics-counting-ch.md} +104 -84
  79. package/docs/{diagnostics-counting-en.md → reference/diagnostics-counting-en.md} +104 -84
  80. package/docs/{html-compatibility-ch.md → reference/html-compatibility-ch.md} +42 -4
  81. package/docs/{html-compatibility-en.md → reference/html-compatibility-en.md} +42 -4
  82. package/docs/{local-components-v2-ch.md → reference/local-components-v2-ch.md} +138 -138
  83. package/docs/{local-components-v2-en.md → reference/local-components-v2-en.md} +104 -104
  84. package/docs/{local-interaction-syntax-ch.md → reference/local-interaction-syntax-ch.md} +515 -492
  85. package/docs/{local-interaction-syntax-en.md → reference/local-interaction-syntax-en.md} +511 -488
  86. package/docs/{theme-protocol-ch.md → reference/theme-protocol-ch.md} +107 -107
  87. package/docs/{theme-protocol-en.md → reference/theme-protocol-en.md} +107 -107
  88. package/docs/{release-0.4.0-ch.md → releases/release-0.4.0-ch.md} +89 -89
  89. package/docs/{release-0.4.0-en.md → releases/release-0.4.0-en.md} +89 -89
  90. package/docs/{release-0.4.1-ch.md → releases/release-0.4.1-ch.md} +23 -23
  91. package/docs/{release-0.4.1-en.md → releases/release-0.4.1-en.md} +23 -23
  92. package/docs/{release-0.5.0-ch.md → releases/release-0.5.0-ch.md} +4 -4
  93. package/docs/{release-0.5.0-en.md → releases/release-0.5.0-en.md} +4 -4
  94. package/docs/{release-0.5.1-ch.md → releases/release-0.5.1-ch.md} +4 -4
  95. package/docs/{release-0.5.1-en.md → releases/release-0.5.1-en.md} +4 -4
  96. package/docs/{release-0.5.2-ch.md → releases/release-0.5.2-ch.md} +3 -3
  97. package/docs/{release-0.5.2-en.md → releases/release-0.5.2-en.md} +3 -3
  98. package/docs/{release-0.5.3-ch.md → releases/release-0.5.3-ch.md} +1 -1
  99. package/docs/{release-0.5.3-en.md → releases/release-0.5.3-en.md} +1 -1
  100. package/docs/releases/release-0.6.0-ch.md +57 -0
  101. package/docs/releases/release-0.6.0-en.md +57 -0
  102. package/docs/releases/release-0.6.1-ch.md +27 -0
  103. package/docs/releases/release-0.6.1-en.md +27 -0
  104. package/examples/conformance/README.md +53 -53
  105. package/examples/conformance/component-protocol-1.json +482 -482
  106. package/examples/conformance/component-protocol-1.schema.json +76 -76
  107. package/examples/conformance/local-draft-1.json +50 -50
  108. package/examples/conformance/local-draft-2.json +212 -212
  109. package/examples/integration/README.md +47 -45
  110. package/examples/integration/core/index.mjs +32 -32
  111. package/examples/integration/worker/index.html +19 -19
  112. package/examples/integration/worker/main.js +61 -61
  113. package/examples/integration/worker/package.json +13 -13
  114. package/examples/integration/worker/worker.js +3 -3
  115. package/examples/local/README.md +74 -74
  116. package/examples/local/charts.reactive.md +73 -73
  117. package/examples/local/html-compat.reactive.md +19 -0
  118. package/examples/local/recipes/invoice-zh.reactive.md +36 -36
  119. package/examples/local/recipes/invoice.reactive.md +36 -36
  120. package/examples/local/recipes/workshop-zh.reactive.md +33 -33
  121. package/examples/local/recipes/workshop.reactive.md +33 -33
  122. package/examples/local/showcase/README.md +25 -25
  123. package/examples/local/showcase/explore.reactive.md +102 -102
  124. package/examples/local/showcase/flow.reactive.md +86 -86
  125. package/examples/local/showcase/forms.reactive.md +126 -126
  126. package/examples/local/showcase/functions.reactive.md +119 -119
  127. package/examples/local/showcase/layout.reactive.md +100 -100
  128. package/examples/local/showcase/statistics.reactive.md +108 -108
  129. package/examples/local/showcase/tasks.reactive.md +89 -89
  130. package/examples/local/showcase/zh/explore.reactive.md +102 -102
  131. package/examples/local/showcase/zh/flow.reactive.md +86 -86
  132. package/examples/local/showcase/zh/forms.reactive.md +126 -126
  133. package/examples/local/showcase/zh/functions.reactive.md +119 -119
  134. package/examples/local/showcase/zh/layout.reactive.md +100 -100
  135. package/examples/local/showcase/zh/statistics.reactive.md +108 -108
  136. package/examples/local/showcase/zh/tasks.reactive.md +89 -89
  137. package/package.json +152 -145
  138. package/dist-library/compiler-UyPwGzJZ.js +0 -22109
  139. package/dist-library/react-fizgU3i8.js +0 -12764
  140. package/docs/local-guide-ch.md +0 -967
  141. package/docs/local-guide-en.md +0 -967
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
+ }