reactive-md 0.3.0 → 0.4.0

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 (175) hide show
  1. package/CHANGELOG-ch.md +96 -0
  2. package/CHANGELOG.md +96 -40
  3. package/CONTRIBUTING-ch.md +62 -0
  4. package/CONTRIBUTING.md +64 -61
  5. package/README-ch.md +122 -0
  6. package/README-en.md +146 -0
  7. package/README.md +146 -88
  8. package/SECURITY-ch.md +80 -0
  9. package/SECURITY.md +80 -68
  10. package/THIRD_PARTY_NOTICES.md +85 -1
  11. package/bin/reactive-md.mjs +40 -0
  12. package/cli/args.mjs +181 -0
  13. package/cli/assets.mjs +390 -0
  14. package/cli/build.mjs +268 -0
  15. package/cli/document.mjs +130 -0
  16. package/cli/errors.mjs +61 -0
  17. package/cli/fs-utils.mjs +133 -0
  18. package/cli/glob.mjs +203 -0
  19. package/cli/help.mjs +138 -0
  20. package/cli/lint.mjs +177 -0
  21. package/cli/main.mjs +55 -0
  22. package/cli/migrate.mjs +368 -0
  23. package/cli/package.mjs +209 -0
  24. package/dist-library/reactive-md-react.js +3603 -1690
  25. package/dist-library/reactive-md.css +1 -1
  26. package/dist-library/reactive-md.js +1798 -4
  27. package/dist-library/runtime-DtjdPXxJ.js +15410 -0
  28. package/dist-library/types/index.d.ts +1 -0
  29. package/dist-library/types/local/LocalDocumentView.d.ts +66 -3
  30. package/dist-library/types/local/authoring.d.ts +63 -0
  31. package/dist-library/types/local/collection-functions.d.ts +4 -0
  32. package/dist-library/types/local/colors.d.ts +2 -0
  33. package/dist-library/types/local/compiler.d.ts +3 -3
  34. package/dist-library/types/local/computations.d.ts +53 -0
  35. package/dist-library/types/local/data-functions.d.ts +22 -0
  36. package/dist-library/types/local/data-table.d.ts +65 -0
  37. package/dist-library/types/local/dates.d.ts +2 -0
  38. package/dist-library/types/local/dependencies.d.ts +31 -0
  39. package/dist-library/types/local/diagnostics.d.ts +1450 -0
  40. package/dist-library/types/local/expression.d.ts +13 -3
  41. package/dist-library/types/local/formatting.d.ts +41 -0
  42. package/dist-library/types/local/heading-anchors.d.ts +17 -0
  43. package/dist-library/types/local/i18n.d.ts +18 -0
  44. package/dist-library/types/local/index.d.ts +18 -1
  45. package/dist-library/types/local/limits.d.ts +33 -1
  46. package/dist-library/types/local/locale.d.ts +1 -0
  47. package/dist-library/types/local/math.d.ts +6 -1
  48. package/dist-library/types/local/parser.d.ts +10 -4
  49. package/dist-library/types/local/render-types.d.ts +5 -1
  50. package/dist-library/types/local/retention.d.ts +23 -0
  51. package/dist-library/types/local/schema.d.ts +25 -3
  52. package/dist-library/types/local/source-migration.d.ts +35 -0
  53. package/dist-library/types/local/state.d.ts +80 -0
  54. package/dist-library/types/local/text-length.d.ts +13 -0
  55. package/dist-library/types/local/theme.d.ts +30 -0
  56. package/dist-library/types/local/types.d.ts +120 -4
  57. package/dist-library/types/local/validation.d.ts +32 -0
  58. package/dist-library/types/local/version.d.ts +1 -1
  59. package/dist-library/types/local/worker.d.ts +82 -0
  60. package/dist-library/types/react/index.d.ts +4 -1
  61. package/dist-library/types/shareable.d.ts +64 -0
  62. package/dist-standalone/reactive-md-standalone.css +2 -0
  63. package/dist-standalone/reactive-md-standalone.js +57 -0
  64. package/docs/author-tools-ch.md +56 -0
  65. package/docs/author-tools-en.md +56 -0
  66. package/docs/compatibility-ch.md +103 -0
  67. package/docs/compatibility-en.md +103 -0
  68. package/docs/component-development-ch.md +210 -0
  69. package/docs/component-development-en.md +210 -0
  70. package/docs/component-protocol-ch.md +710 -0
  71. package/docs/component-protocol-en.md +710 -0
  72. package/docs/computations-formatting-ch.md +87 -0
  73. package/docs/computations-formatting-en.md +87 -0
  74. package/docs/constraints-collections-ch.md +84 -0
  75. package/docs/constraints-collections-en.md +84 -0
  76. package/docs/diagnostics-counting-ch.md +84 -0
  77. package/docs/diagnostics-counting-en.md +84 -0
  78. package/docs/local-components-v2-ch.md +138 -0
  79. package/docs/local-components-v2-en.md +104 -0
  80. package/docs/{local-guide.md → local-guide-ch.md} +961 -936
  81. package/docs/local-guide-en.md +961 -0
  82. package/docs/local-integration-ch.md +363 -0
  83. package/docs/local-integration-en.md +363 -0
  84. package/docs/{local-interaction-syntax.md → local-interaction-syntax-ch.md} +485 -437
  85. package/docs/local-interaction-syntax-en.md +483 -0
  86. package/docs/performance-ch.md +76 -0
  87. package/docs/performance-en.md +76 -0
  88. package/docs/release-0.4.0-ch.md +89 -0
  89. package/docs/release-0.4.0-en.md +89 -0
  90. package/docs/releasing-ch.md +101 -0
  91. package/docs/releasing-en.md +101 -0
  92. package/docs/shareable-export-ch.md +205 -0
  93. package/docs/shareable-export-en.md +207 -0
  94. package/docs/source-versioning-ch.md +159 -0
  95. package/docs/source-versioning-en.md +159 -0
  96. package/docs/theme-protocol-ch.md +107 -0
  97. package/docs/theme-protocol-en.md +107 -0
  98. package/examples/conformance/README.md +53 -11
  99. package/examples/conformance/component-protocol-1.json +482 -0
  100. package/examples/conformance/component-protocol-1.schema.json +76 -0
  101. package/examples/conformance/history/v0.3.1/catalog.reactive.md +20 -0
  102. package/examples/conformance/history/v0.3.1/charts.reactive.md +73 -0
  103. package/examples/conformance/history/v0.3.1/checklist.reactive.md +32 -0
  104. package/examples/conformance/history/v0.3.1/comparison.reactive.md +47 -0
  105. package/examples/conformance/history/v0.3.1/components.reactive.md +44 -0
  106. package/examples/conformance/history/v0.3.1/counter.reactive.md +7 -0
  107. package/examples/conformance/history/v0.3.1/diagnostics.reactive.md +54 -0
  108. package/examples/conformance/history/v0.3.1/diagram.reactive.md +44 -0
  109. package/examples/conformance/history/v0.3.1/gallery.reactive.md +61 -0
  110. package/examples/conformance/history/v0.3.1/local-draft-1.json +50 -0
  111. package/examples/conformance/history/v0.3.1/parameters.reactive.md +36 -0
  112. package/examples/conformance/history/v0.3.1/planning.reactive.md +24 -0
  113. package/examples/conformance/history/v0.3.1/pricing.reactive.md +20 -0
  114. package/examples/conformance/history/v0.3.1/provenance.json +72 -0
  115. package/examples/conformance/local-draft-1.json +50 -49
  116. package/examples/conformance/local-draft-2.json +212 -0
  117. package/examples/integration/README.md +45 -29
  118. package/examples/integration/core/index.mjs +32 -19
  119. package/examples/integration/react/index.html +1 -1
  120. package/examples/integration/react/main.jsx +5 -5
  121. package/examples/integration/worker/index.html +19 -0
  122. package/examples/integration/worker/main.js +61 -0
  123. package/examples/integration/worker/package.json +13 -0
  124. package/examples/integration/worker/worker.js +3 -0
  125. package/examples/local/README.md +72 -66
  126. package/examples/local/catalog.reactive.md +14 -14
  127. package/examples/local/charts.reactive.md +73 -73
  128. package/examples/local/checklist.reactive.md +19 -19
  129. package/examples/local/comparison.reactive.md +23 -23
  130. package/examples/local/components.reactive.md +20 -20
  131. package/examples/local/counter.reactive.md +4 -4
  132. package/examples/local/diagnostics.reactive.md +25 -25
  133. package/examples/local/diagram.reactive.md +20 -20
  134. package/examples/local/gallery.reactive.md +31 -31
  135. package/examples/local/parameters.reactive.md +19 -19
  136. package/examples/local/planning.reactive.md +15 -15
  137. package/examples/local/pricing.reactive.md +10 -10
  138. package/examples/local/recipes/invoice-zh.reactive.md +36 -0
  139. package/examples/local/recipes/invoice.reactive.md +36 -0
  140. package/examples/local/recipes/workshop-zh.reactive.md +33 -0
  141. package/examples/local/recipes/workshop.reactive.md +33 -0
  142. package/examples/local/showcase/README.md +25 -0
  143. package/examples/local/showcase/explore.reactive.md +102 -0
  144. package/examples/local/showcase/flow.reactive.md +86 -0
  145. package/examples/local/showcase/forms.reactive.md +126 -0
  146. package/examples/local/showcase/functions.reactive.md +119 -0
  147. package/examples/local/showcase/layout.reactive.md +100 -0
  148. package/examples/local/showcase/statistics.reactive.md +108 -0
  149. package/examples/local/showcase/tasks.reactive.md +89 -0
  150. package/examples/local/showcase/zh/explore.reactive.md +102 -0
  151. package/examples/local/showcase/zh/flow.reactive.md +86 -0
  152. package/examples/local/showcase/zh/forms.reactive.md +126 -0
  153. package/examples/local/showcase/zh/functions.reactive.md +119 -0
  154. package/examples/local/showcase/zh/layout.reactive.md +100 -0
  155. package/examples/local/showcase/zh/statistics.reactive.md +108 -0
  156. package/examples/local/showcase/zh/tasks.reactive.md +89 -0
  157. package/examples/local/zh/catalog.reactive.md +20 -0
  158. package/examples/local/zh/charts.reactive.md +73 -0
  159. package/examples/local/zh/checklist.reactive.md +32 -0
  160. package/examples/local/zh/comparison.reactive.md +47 -0
  161. package/examples/local/zh/components.reactive.md +44 -0
  162. package/examples/local/zh/counter.reactive.md +7 -0
  163. package/examples/local/zh/diagnostics.reactive.md +54 -0
  164. package/examples/local/zh/diagram.reactive.md +44 -0
  165. package/examples/local/zh/gallery.reactive.md +61 -0
  166. package/examples/local/zh/parameters.reactive.md +36 -0
  167. package/examples/local/zh/planning.reactive.md +24 -0
  168. package/examples/local/zh/pricing.reactive.md +20 -0
  169. package/package.json +90 -75
  170. package/README.en.md +0 -75
  171. package/dist-library/runtime-D4phFXOr.js +0 -10937
  172. package/docs/compatibility.md +0 -67
  173. package/docs/local-integration.md +0 -193
  174. package/docs/performance.md +0 -30
  175. package/docs/releasing.md +0 -92
@@ -0,0 +1,205 @@
1
+ # 可分享文档与静态导出
2
+
3
+ 本文说明 `local-draft-2` 新增的单文件 HTML 导出能力。编译与运行时接口见[本地模式集成](local-integration-ch.md),语法见[交互语法规范](local-interaction-syntax-ch.md),修订声明与迁移见[源码修订与迁移](source-versioning-ch.md),升级说明见[0.4.0 发布与迁移](release-0.4.0-ch.md)。
4
+
5
+ ## 两种产物
6
+
7
+ **交互式 HTML** 是一个自包含的 `.html` 文件。它内嵌原始源码、独立浏览器运行时、React 19、样式表和打印样式。读者通过 `file://` 打开即可交互,不需要 Node.js、React、reactive-md 包或网络连接。
8
+
9
+ 首次渲染是静态内容;内嵌运行时水合完成后再增强为控件。禁用 JavaScript 时,正文、当前值、表格、公式和图表仍然可读。读者刷新后回到导出基线,后续修改不会自动保存。
10
+
11
+ **静态 HTML**(`--mode static`)只包含只读结果,不携带可执行运行时。它适合邮件附件、归档和静态站点。供交互使用的按钮、表单控件和重置操作会被移除。
12
+
13
+ 两种模式都复用 React 渲染器,因此条件内容、插值、表格、公式、图表和布局都按运行时状态求值。
14
+
15
+ ## CLI
16
+
17
+ 包提供 `reactive-md` 命令。构建 HTML 的项目需要安装 React 19 和 React DOM;`lint` 和 `migrate` 不需要。
18
+
19
+ ```sh
20
+ reactive-md build doc.reactive.md -o doc.html
21
+ reactive-md build doc.reactive.md -o doc.html --mode static
22
+ reactive-md build doc.reactive.md -o doc.html --state-file doc.state.json
23
+ reactive-md build doc.reactive.md -o doc.html --assets omit
24
+ reactive-md lint "docs/**/*.reactive.md"
25
+ reactive-md lint "docs/**/*.reactive.md" --json --strict
26
+ ```
27
+
28
+ | 选项 | 含义 |
29
+ | --- | --- |
30
+ | `-o, --output <file>` | 必填输出路径;父目录必须已存在。 |
31
+ | `--mode interactive\|static` | 输出模式,默认 `interactive`。 |
32
+ | `--state-file <json>` | 使用经过校验的 `reactive-md-state/v1` 文件作为打开和重置基线。 |
33
+ | `--assets inline\|omit` | 内联已验证的本地位图(默认),或保留渲染器的替代文本降级。 |
34
+ | `--asset-root <dir>` | 额外允许相对图片路径解析的本地目录。 |
35
+ | `--locale en\|zh` | 渲染标签与校验信息语言,默认 `en`。 |
36
+ | `--color-scheme auto\|light\|dark` | 文档颜色模式,默认 `auto`。 |
37
+ | `--title <text>` | 覆盖 HTML 标题;默认取首个标题或输入文件名。 |
38
+ | `--strict` | 警告也视为失败;配合 `--assets omit` 时,存在被省略图片也失败。 |
39
+
40
+ 退出码区分不同失败:
41
+
42
+ | 退出码 | 含义 |
43
+ | --- | --- |
44
+ | `0` | 成功 |
45
+ | `2` | 参数或用法错误 |
46
+ | `3` | 文档编译、渲染、React 或包内构建产物错误 |
47
+ | `4` | 输入/输出文件错误 |
48
+ | `5` | 资源或安全策略错误 |
49
+
50
+ CLI 没有“当前预览”概念,只有 `--state-file` 可以选择非初值基线。本版本暂不提供 `format`。
51
+
52
+ ## 迁移源码
53
+
54
+ `reactive-md migrate` 把单份源码的修订声明迁移到 `local-draft-2`:
55
+
56
+ ```sh
57
+ reactive-md migrate old.reactive.md --from local-draft-1 -o new.reactive.md
58
+ reactive-md migrate old.reactive.md --check --json
59
+ ```
60
+
61
+ 默认把迁移后的源码写到标准输出。`-o` 使用独占创建,拒绝任何已存在的路径,包括输入文件本身。`--check` 从不写入。`--json` 只输出结构化报告。`--accept-semantic-changes` 允许文本控件长度语义变化并报告警告;`--locale en|zh` 选择显示语言。
62
+
63
+ 迁移被阻止时退出 `1`,成功时退出 `0`。`--check` 在需要迁移或被阻止时退出 `1`,源码已是当前版本时退出 `0`。用法和 I/O 错误退出 `2`。命令不就地覆盖,也不提供批量或 `--write` 模式。检查项、保留字节和状态摘要影响见[源码修订与迁移](source-versioning-ch.md)。
64
+
65
+ ## 起始参数与状态文件
66
+
67
+ 默认导出作者定义的初始参数,保证构建可重复,避免把调试值或敏感输入带给接收者。
68
+
69
+ 状态文件采用版本化结构:
70
+
71
+ ```json
72
+ {
73
+ "format": "reactive-md-state/v1",
74
+ "syntax": "local-draft-2",
75
+ "sourceDigest": "fnv1a32:0123abcd",
76
+ "parameters": {
77
+ "people": 7,
78
+ "plan": "pro"
79
+ },
80
+ "generator": { "name": "example-host", "version": "1.0.0" }
81
+ }
82
+ ```
83
+
84
+ 只保存公开、可序列化的绑定值。文件通过 `sourceDigest` 标识文档,通过 `syntax` 记录语言修订。`@` 开头的内部绑定、revision、节点 ID、AST、计时器、展开状态和焦点都不会写入;内部快照键和 ID 属于实现细节,不是存储格式。
85
+
86
+ 恢复时按源码摘要、语法版本、参数集合和槽位约束逐项校验。未知、缺失或非法参数会报告具体名称并拒绝整个文件,不会部分应用。`range-slider` 等双槽组件按原子写入规则校验。
87
+
88
+ 选中的状态同时成为重置基线。因此打开、刷新和面板重置都会回到导出值,而不是另一个隐藏的默认值。
89
+
90
+ 宿主可以不经过 CLI 生成和应用同一格式:
91
+
92
+ ```ts
93
+ import { exportLocalState, importLocalState } from 'reactive-md';
94
+
95
+ const file = exportLocalState(runtime, { name: 'example-host', version: '1.0.0' });
96
+ const saved = JSON.stringify(file);
97
+
98
+ const diagnostics = importLocalState(runtime, saved);
99
+ ```
100
+
101
+ `exportLocalState` 只读取公开参数。`importLocalState` 校验源码摘要、语法、参数集合和槽位约束后应用;导入失败时运行时状态不变。两者都是宿主显式调用,运行时本身仍不会保存或发送状态。只做校验、不改动运行中实例时使用 `resolveLocalState(document, input)`。
102
+
103
+ Demo 导出对话框可以把当前预览参数下载为状态文件,供后续 CLI 构建使用。较低层的 `createLocalStateFile`、`parseLocalState`、`applyLocalState` 和 `serializeLocalState` 仍可从 `reactive-md` 使用。
104
+
105
+ ## 图片
106
+
107
+ 离线导出默认使用 `--assets inline`:
108
+
109
+ - 只读取文档目录内或显式 `--asset-root` 内的相对路径。
110
+ - 拒绝绝对路径、URL scheme、协议相对地址、反斜杠、路径穿越和符号链接逃逸。
111
+ - 从不自动抓取远程图片。
112
+ - 按文件内容识别 PNG、JPEG、GIF、WebP;拒绝 SVG、伪造 MIME 和未知格式。
113
+ - 单图上限 8 MiB,总量上限 24 MiB,最多 64 个不同资源;相同内容按哈希去重。
114
+ - 保留 alt;无法内联时默认构建失败。`--assets omit` 是显式降级,保留 alt 回退。
115
+
116
+ 渲染器原有的全局安全边界没有放宽:没有宿主 URL 策略时图片仍默认全部拒绝。导出器只在校验本地文件后提供 URL 策略和图片映射。
117
+
118
+ ## 单文件安全与打印
119
+
120
+ HTML 外壳包含:
121
+
122
+ - CSP:禁止连接、frame、object、base 注入和表单提交,只允许内联样式/运行时和 `data:` 图片;
123
+ - 对 JSON、源码、标题和许可证文本做 HTML 与脚本上下文转义;
124
+ - 在不会执行的 `<template>` 中保留 MIT 和第三方 notices;
125
+ - 不引用 CDN、远程样式、字体或运行时。
126
+
127
+ 普通外链在读者主动点击时仍可导航,但打开文档本身不会发起远程请求。该导出器不等同于不可信作者的沙箱;需要隔离时应使用 sandboxed iframe。
128
+
129
+ 打印不依赖临时的状态切换。交互式页面会为当前运行时值输出打印专用节点,`Ctrl+P` 打印读者当前看到的结果。打印样式强制浅色纸张、隐藏工具栏和控件、使用打印机决定纸张边距、避免标题孤立、重复表头、换行长代码和 URL,并把图表限制在页面宽度内。静态 HTML 本身只包含只读值。
130
+
131
+ ## 独立运行时 API
132
+
133
+ 构建会生成 `dist-standalone/reactive-md-standalone.js`(IIFE,全局名 `ReactiveMD`)和 `reactive-md-standalone.css`。二者按普通浏览器资源加载:
134
+
135
+ ```html
136
+ <link rel="stylesheet" href="reactive-md-standalone.css">
137
+ <script src="reactive-md-standalone.js"></script>
138
+ ```
139
+
140
+ 程序化挂载:
141
+
142
+ ```js
143
+ const handle = ReactiveMD.mount(document.getElementById('doc'), {
144
+ source: '# Hello\n\n<slider bind="n" label="Count" initial="2">\n\nValue: {{ n }}',
145
+ locale: 'en',
146
+ colorScheme: 'auto',
147
+ });
148
+ handle.getState(); // reactive-md-state/v1 快照
149
+ handle.unmount();
150
+ ```
151
+
152
+ `ReactiveMD.mountAll(root)` 只扫描一次安全的自动挂载载荷:
153
+
154
+ ```html
155
+ <div id="doc"></div>
156
+ <script type="application/reactive-md+json" data-reactive-md-auto>
157
+ {"version":1,"target":"doc","source":"# Hello","state":{"format":"reactive-md-state/v1","syntax":"local-draft-2","sourceDigest":"fnv1a32:01209266","parameters":{}},"locale":"en","colorScheme":"auto","hydrate":false}
158
+ </script>
159
+ ```
160
+
161
+ 目标只按 `id` 查找,不拼接选择器。source 只作为数据处理,不当作可执行 HTML。故意不解释 `<script type="text/reactive-md">` 的原始文本。
162
+
163
+ ### `<reactive-md-doc>`
164
+
165
+ IIFE 会注册一个 Light DOM 自定义元素。简单选项通过属性传递,复杂选项通过 DOM property 传递。
166
+
167
+ ```html
168
+ <reactive-md-doc src="./doc.reactive.md" locale="en" color-scheme="auto"></reactive-md-doc>
169
+ ```
170
+
171
+ - 属性:`src`、`source`、`locale`、`color-scheme`、`theme`。
172
+ - Property:`source`、`state`、`urlPolicy`、`imageResolver`、`themes`。
173
+ - 事件:`reactive-md-diagnostics`、`reactive-md-state-change`。
174
+ - `src` 通过可取消的 `fetch` 加载;显式 source 不会产生请求。
175
+ - 断开连接时中止请求、移除订阅、卸载 React root 并释放运行时。两个相同 source 的元素拥有独立状态。
176
+
177
+ 元素使用 Light DOM,以复用现有文档样式、标题锚点和打印样式。本版本不提供 Shadow DOM。
178
+
179
+ ## 安全边界
180
+
181
+ - 核心与 React 入口继续保持 React external;只有 standalone IIFE 打包 React。
182
+ - 核心入口仍不加载 React。
183
+ - 状态导出与导入只使用公开绑定名和经过校验的值。
184
+ - 读者修改不会持久化到导出文件。
185
+ - Demo 预览仍按默认策略阻止图片;Demo 没有资源根,因此文档包含图片时会禁用 HTML 导出。含图文档请使用 CLI。
186
+ - 导出格式版本为 `reactive-md-html/v1`,可从生成的 meta 标签检查。
187
+
188
+ ## 产物体积基线
189
+
190
+ `npm run test:shareable` 会记录 standalone 产物体积,并在超过以下回归阈值时失败:
191
+
192
+ | 产物 | 当前基线 | 报警阈值 |
193
+ | --- | --- | --- |
194
+ | `reactive-md-standalone.js` | 约 625.5 kB(gzip 约 186 kB) | 800 KiB / gzip 240 KiB |
195
+ | `reactive-md-standalone.css` | 约 59.7 kB(gzip 约 10.1 kB) | 80 KiB / gzip 20 KiB |
196
+
197
+ 数值会随压缩器和 React 补丁版本小幅变化。阈值远高于正常波动,同时能捕获 React 或渲染器被重复打包的退化。完整 HTML 还包含源码、notices 和内联图片,因此会大于这个基线。
198
+
199
+ ## 当前范围
200
+
201
+ 已实现:CLI 的 `build`/`lint`/`migrate`、交互与静态 HTML、状态文件、本地位图内联、打印输出、standalone 挂载 API、自动挂载和 Web Component。
202
+
203
+ 本版本不包含:`format`、UMD、远程图片抓取、SVG 内联、框架插件(Vite、Astro、Docusaurus、Next.js)、Shadow DOM,以及单文件导出之外的通用 SSR 水合保证。
204
+
205
+ 单文件静态首屏水合由 Playwright Chromium 和本地 Edge 检查覆盖。Firefox、Safari/WebKit 以及通用服务端水合尚未验证,不作支持声明。
@@ -0,0 +1,207 @@
1
+ # Shareable documents and static export
2
+
3
+ This guide covers the single-file HTML exporter introduced with `local-draft-2`. It complements [Local mode integration](local-integration-en.md), the [syntax reference](local-interaction-syntax-en.md), [Source revisions and migration](source-versioning-en.md) and the [0.4.0 release notes](release-0.4.0-en.md).
4
+
5
+ ## Output modes
6
+
7
+ **Interactive HTML** is one self-contained `.html` file. It embeds the original source, the standalone browser runtime, React 19, the current stylesheet and print styles. Readers can open it through `file://` without Node.js, React, the reactive-md package or network access.
8
+
9
+ The first rendered pass is static. After the embedded runtime hydrates it, controls become interactive. With JavaScript disabled the same static values, tables, formulas and charts remain readable. Refreshing restores the exported baseline; the reader's changes are not persisted.
10
+
11
+ **Static HTML** (`--mode static`) contains read-only values and no executable script cargo. It is intended for archives, static sites and email attachments. Interactive action buttons, form widgets and the reset action are removed.
12
+
13
+ Both modes use the same renderer as the React integration, so conditional content, interpolation, tables, formulas, charts and layout follow the runtime state.
14
+
15
+ ## CLI
16
+
17
+ The package exposes the `reactive-md` command. HTML builds require React 19 and React DOM in the project that installs the package; `lint` and `migrate` do not.
18
+
19
+ ```sh
20
+ reactive-md build doc.reactive.md -o doc.html
21
+ reactive-md build doc.reactive.md -o doc.html --mode static
22
+ reactive-md build doc.reactive.md -o doc.html --state-file doc.state.json
23
+ reactive-md build doc.reactive.md -o doc.html --assets omit
24
+ reactive-md lint "docs/**/*.reactive.md"
25
+ reactive-md lint "docs/**/*.reactive.md" --json --strict
26
+ ```
27
+
28
+ Build options:
29
+
30
+ | Option | Meaning |
31
+ | --- | --- |
32
+ | `-o, --output <file>` | Required output path. Its parent directory must already exist. |
33
+ | `--mode interactive\|static` | Output mode. Default: `interactive`. |
34
+ | `--state-file <json>` | Use a validated `reactive-md-state/v1` file as the starting and reset baseline. |
35
+ | `--assets inline\|omit` | Inline verified local raster images (default) or leave the renderer's alt fallback. |
36
+ | `--asset-root <dir>` | Additional local directory that relative image paths may resolve inside. |
37
+ | `--locale en\|zh` | Renderer labels and validation messages. Default: `en`. |
38
+ | `--color-scheme auto\|light\|dark` | Document color mode. Default: `auto`. |
39
+ | `--title <text>` | Override the HTML title. The first heading or the input filename is used otherwise. |
40
+ | `--strict` | Fail on compiler warnings; with `--assets omit`, fail when images are omitted. |
41
+
42
+ Exit codes are separate so build automation can distinguish failures:
43
+
44
+ | Code | Meaning |
45
+ | --- | --- |
46
+ | `0` | Success |
47
+ | `2` | Usage or argument error |
48
+ | `3` | Document compile, render, React, or packaged-artifact failure |
49
+ | `4` | Input/output file failure |
50
+ | `5` | Asset or security-policy failure |
51
+
52
+ The CLI does not use a current preview implicitly. `--state-file` is the only way to select a non-initial baseline. `format` is not part of this release.
53
+
54
+ ## Migrating source
55
+
56
+ `reactive-md migrate` moves one source revision declaration to `local-draft-2`:
57
+
58
+ ```sh
59
+ reactive-md migrate old.reactive.md --from local-draft-1 -o new.reactive.md
60
+ reactive-md migrate old.reactive.md --check --json
61
+ ```
62
+
63
+ By default it prints the migrated source to standard output. `-o` creates a new file with exclusive creation and refuses any existing path, including the input. `--check` never writes. `--json` prints a structured report and nothing else. `--accept-semantic-changes` allows the text-control length change and reports warnings; `--locale en|zh` selects the display language.
64
+
65
+ A migration exits `1` when blocked and `0` on success. `--check` exits `1` when changes are needed or blocked, and `0` when the source is current. Usage and I/O errors exit `2`. The command does not overwrite in place and has no batch or `--write` mode. See [Source revisions and migration](source-versioning-en.md) for the checks, preserved bytes and state-digest consequences.
66
+
67
+ ## Starting values and state files
68
+
69
+ By default, the exported baseline is the document's author initial values. This keeps builds repeatable and avoids leaking trial inputs.
70
+
71
+ A state file has this versioned shape:
72
+
73
+ ```json
74
+ {
75
+ "format": "reactive-md-state/v1",
76
+ "syntax": "local-draft-2",
77
+ "sourceDigest": "fnv1a32:0123abcd",
78
+ "parameters": {
79
+ "people": 7,
80
+ "plan": "pro"
81
+ },
82
+ "generator": { "name": "example-host", "version": "1.0.0" }
83
+ }
84
+ ```
85
+
86
+ Only public, serializable binding values are stored. The file identifies the document through `sourceDigest` and the language revision through `syntax`. Internal bindings (names beginning with `@`), revisions, node IDs, AST data, timers, open panels and focus are never persisted; internal snapshot keys and IDs are implementation details, not a storage format.
87
+
88
+ Resolution verifies the source digest, syntax revision, exact public parameter set and every value against the document's constraints. Unknown, missing or invalid parameters reject the file with a named diagnostic; a mismatched file is never partially applied. Slot controls such as `<range-slider>` are validated atomically.
89
+
90
+ The selected state becomes the document's reset baseline. Opening, refreshing and the panel reset action therefore return to the exported values, not to a different hidden default.
91
+
92
+ Hosts can create and apply the same file without the CLI:
93
+
94
+ ```ts
95
+ import { exportLocalState, importLocalState } from 'reactive-md';
96
+
97
+ const file = exportLocalState(runtime, { name: 'example-host', version: '1.0.0' });
98
+ const saved = JSON.stringify(file);
99
+
100
+ const diagnostics = importLocalState(runtime, saved);
101
+ ```
102
+
103
+ `exportLocalState` reads public parameters only. `importLocalState` validates the source digest, syntax, parameter set and constraints, then applies the values; a failed import leaves the runtime unchanged. Both are explicit host calls: the runtime still does not save or send state by itself. Use `resolveLocalState(document, input)` to validate without touching a live runtime.
104
+
105
+ The demo's export dialog can download the current preview parameters as a state file for a later CLI build. The lower-level `createLocalStateFile`, `parseLocalState`, `applyLocalState` and `serializeLocalState` remain available from `reactive-md`.
106
+
107
+ ## Images
108
+
109
+ Offline export defaults to `--assets inline`:
110
+
111
+ - Only relative paths that resolve inside the document directory, or an explicit `--asset-root`, are read.
112
+ - Absolute paths, URL schemes, protocol-relative URLs, backslashes, path traversal and symlink escapes are rejected.
113
+ - Remote images are never fetched.
114
+ - PNG, JPEG, GIF and WebP are detected by file contents; SVG, forged MIME types and unknown formats are rejected.
115
+ - Files are limited to 8 MiB each, 24 MiB total and 64 distinct payloads. Identical content is deduplicated.
116
+ - Alt text is preserved. A failure stops the build by default. `--assets omit` is the explicit opt-out and leaves the alt fallback.
117
+
118
+ The renderer's global security boundary is unchanged: images are still rejected by default unless a host URL policy explicitly allows them. The exporter supplies a host policy and image resolver after it has verified each local file.
119
+
120
+ ## Single-file security and printing
121
+
122
+ The HTML shell includes:
123
+
124
+ - a CSP that disallows connections, frames, objects, base URL changes and form submission while allowing the inline stylesheet/runtime and `data:` images;
125
+ - HTML- and script-context escaping for JSON, source, titles and license notices;
126
+ - the MIT and third-party notices in a non-executed `<template>`;
127
+ - no CDN or remote stylesheet, font or runtime.
128
+
129
+ Clicking an ordinary external link can still navigate, but opening the document itself performs no remote request. The exporter is not a sandbox for fully untrusted authors; use a sandboxed iframe when that threat model applies.
130
+
131
+ Printing does not require a temporary state switch. The current runtime values are rendered in print-only nodes, so `Ctrl+P` prints the state the reader currently sees. Print CSS forces a light paper theme, hides toolbars and controls, uses printer-owned page margins, keeps headings with following content, repeats table headers, wraps long code and URLs, and constrains charts to page width. Static HTML already contains only read-only values.
132
+
133
+ ## Standalone runtime API
134
+
135
+ The build emits `dist-standalone/reactive-md-standalone.js` (IIFE, global `ReactiveMD`) and `reactive-md-standalone.css`. Load the stylesheet and script as classic browser assets:
136
+
137
+ ```html
138
+ <link rel="stylesheet" href="reactive-md-standalone.css">
139
+ <script src="reactive-md-standalone.js"></script>
140
+ ```
141
+
142
+ Programmatic mounting:
143
+
144
+ ```js
145
+ const handle = ReactiveMD.mount(document.getElementById('doc'), {
146
+ source: '# Hello\n\n<slider bind="n" label="Count" initial="2">\n\nValue: {{ n }}',
147
+ locale: 'en',
148
+ colorScheme: 'auto',
149
+ });
150
+ handle.getState(); // reactive-md-state/v1 snapshot
151
+ handle.unmount();
152
+ ```
153
+
154
+ `ReactiveMD.mountAll(root)` scans only once for safe auto-mount payloads:
155
+
156
+ ```html
157
+ <div id="doc"></div>
158
+ <script type="application/reactive-md+json" data-reactive-md-auto>
159
+ {"version":1,"target":"doc","source":"# Hello","state":{"format":"reactive-md-state/v1","syntax":"local-draft-2","sourceDigest":"fnv1a32:01209266","parameters":{}},"locale":"en","colorScheme":"auto","hydrate":false}
160
+ </script>
161
+ ```
162
+
163
+ Targets are resolved by `id`, never by an interpolated selector. Payload source is data, not executable markup. Raw `<script type="text/reactive-md">` text is deliberately not interpreted.
164
+
165
+ ### `<reactive-md-doc>`
166
+
167
+ The IIFE registers a light-DOM custom element. Simple options are attributes; complex options are DOM properties.
168
+
169
+ ```html
170
+ <reactive-md-doc src="./doc.reactive.md" locale="en" color-scheme="auto"></reactive-md-doc>
171
+ ```
172
+
173
+ - Attributes: `src`, `source`, `locale`, `color-scheme`, `theme`.
174
+ - Properties: `source`, `state`, `urlPolicy`, `imageResolver`, `themes`.
175
+ - Events: `reactive-md-diagnostics` and `reactive-md-state-change`.
176
+ - `src` uses `fetch` with cancellation; an explicit source avoids network access.
177
+ - Disconnecting aborts fetches, removes subscriptions and the React root, and disposes the runtime. Two elements with the same source have independent state.
178
+
179
+ The element uses Light DOM so the existing document CSS, heading anchors and print styles apply. Shadow DOM is intentionally out of scope for this release.
180
+
181
+ ## Security boundaries
182
+
183
+ - Core and React entry points keep React external. Only the standalone IIFE bundles React.
184
+ - The core entry remains React-free.
185
+ - State export and import use only public binding names and validated values.
186
+ - Exported HTML does not persist a reader's changes.
187
+ - The demo preview still blocks images unless a host policy is supplied; the demo export dialog disables HTML export when a document contains images because it has no asset root. Use the CLI for image-bearing documents.
188
+ - The export format version is `reactive-md-html/v1`; inspect it in the generated meta tags.
189
+
190
+ ## Bundle baseline
191
+
192
+ `npm run test:shareable` records the standalone artifact size and fails above these regression alarms:
193
+
194
+ | Artifact | Current baseline | Alarm |
195
+ | --- | --- | --- |
196
+ | `reactive-md-standalone.js` | ≈625.5 kB (≈186 kB gzip) | 800 KiB / 240 KiB gzip |
197
+ | `reactive-md-standalone.css` | ≈59.7 kB (≈10.1 kB gzip) | 80 KiB / 20 KiB gzip |
198
+
199
+ Sizes vary slightly with the minifier and React patch release. The alarm is intentionally well above normal noise while catching a React or renderer duplication regression. The full generated HTML also contains the source, notices and inline images, so it is larger than this baseline.
200
+
201
+ ## Current scope
202
+
203
+ Included: CLI `build`/`lint`/`migrate`, interactive and static HTML, state files, local raster inlining, print output, standalone mount APIs, auto-mount, and the Web Component.
204
+
205
+ Not included in this release: `format`, UMD, remote image fetching, SVG inlining, framework plugins (Vite, Astro, Docusaurus, Next.js), Shadow DOM, and general SSR hydration guarantees outside the single-file export path.
206
+
207
+ Single-file static-first hydration is covered by the Playwright Chromium checks and local Edge checks. Firefox, Safari/WebKit and general server-side hydration remain unverified and are not claimed as supported.
@@ -0,0 +1,159 @@
1
+ # 源码修订与迁移
2
+
3
+ 本文说明 `0.4.0` 中的显式源码修订、编译器的处理方式,以及纯核心 API 与 CLI 迁移工具。语法规则见[语法规范](local-interaction-syntax-ch.md),包版本与 API 兼容规则见[版本与兼容性](compatibility-ch.md),组块升级清单见[0.4.0 发布与迁移](release-0.4.0-ch.md)。
4
+
5
+ ## 声明修订
6
+
7
+ 源码用一行 HTML 注释声明修订:
8
+
9
+ ```markdown
10
+ <!-- reactive-md: local-draft-2 -->
11
+ ```
12
+
13
+ 该指令属于文档前导区,位于 `<theme>` 声明和正文之前。BOM、空行和普通 Markdown 注释可以出现在它前面。只允许一条指令。
14
+
15
+ 行内代码、代码围栏、数学区域和按原文显示的未知标签示例都不是指令。在文档里展示这一行,不会为周围文档声明版本。
16
+
17
+ 编译行为:
18
+
19
+ | 源码 | 结果 |
20
+ | --- | --- |
21
+ | 没有指令 | 按当前修订 `local-draft-2` 解析,与之前一致;不新增警告 |
22
+ | `local-draft-2` | 正常解析 |
23
+ | `local-draft-1` 或其他显式旧修订 | 以 `local.version.*` 诊断拒绝 |
24
+ | 未知、格式错误、重复或位置错误的指令 | 以 `local.version.*` 诊断拒绝 |
25
+
26
+ 版本声明被拒绝时不创建运行时。声明旧修订不会选择旧解析器,也不会让文档在现行规则下可读;必须先迁移源码。
27
+
28
+ 在这一声明出现之前构建的读者(包括 `0.3.1`)会把这条注释当作普通 HTML 注释。它们不校验声明,不拒绝旧修订,也不执行这项检查。声明保护的是在本版本及后续版本中打开的文档;依赖这项检查前,先迁移旧文档。
29
+
30
+ ## 查看声明的修订
31
+
32
+ `inspectLocalSourceVersion` 从 `reactive-md` 导出,不编译,也不创建运行时:
33
+
34
+ ```ts
35
+ import { inspectLocalSourceVersion } from 'reactive-md';
36
+
37
+ const info = inspectLocalSourceVersion(source);
38
+ // {
39
+ // declaredVersion: string | null,
40
+ // effectiveVersion: string,
41
+ // supported: boolean,
42
+ // range: SourceRange | null,
43
+ // diagnostics: Diagnostic[]
44
+ // }
45
+ ```
46
+
47
+ - `declaredVersion` 是指令声明的修订;无指令时为 `null`。
48
+ - `effectiveVersion` 是用于解释判定的修订:有声明时取声明值,否则取当前修订。`supported` 为 `false` 时不进行解释。
49
+ - `supported` 表示当前编译器是否接受该修订。
50
+ - `range` 指向原始源码中的指令;没有指令时为 `null`。
51
+ - `diagnostics` 使用 `local.version.*` 前缀报告格式错误、重复、位置错误、旧修订或未知声明。
52
+
53
+ 无指令源码返回 `declaredVersion: null`、`effectiveVersion: 'local-draft-2'`、`supported: true`,不产生版本诊断。
54
+
55
+ ## 检查升级
56
+
57
+ ```ts
58
+ import { checkLocalUpgrade } from 'reactive-md';
59
+
60
+ const check = checkLocalUpgrade(source);
61
+ // {
62
+ // ok: boolean,
63
+ // fromVersion: string,
64
+ // toVersion: 'local-draft-2',
65
+ // needsMigration: boolean,
66
+ // diagnostics: Diagnostic[]
67
+ // }
68
+ ```
69
+
70
+ `fromVersion` 默认取声明的修订;源码没有声明时取当前修订。可以用 `LocalUpgradeOptions` 覆盖:
71
+
72
+ ```ts
73
+ type LocalUpgradeOptions = {
74
+ fromVersion?: string;
75
+ allowSemanticChanges?: boolean;
76
+ };
77
+
78
+ const check = checkLocalUpgrade(oldSource, { fromVersion: 'local-draft-1' });
79
+ ```
80
+
81
+ 无版本声明的历史文档需要传入 `fromVersion: 'local-draft-1'`,才会执行 draft-1 检查。显式 `fromVersion` 与声明不一致时,检查被拒绝。迁移源只接受 `local-draft-1` 和当前的 `local-draft-2`;旧版 YAML 修订和未知名称会被拒绝。
82
+
83
+ `needsMigration` 表示源码需要写入或替换声明:无声明源码需要补上当前指令,当前源码已经完整,draft-1 源码需要替换指令。版本变化本身可以识别、但存在阻止安全迁移的条件时,`ok` 为 `false`。
84
+
85
+ ## 迁移源码
86
+
87
+ ```ts
88
+ import { migrateLocalSource } from 'reactive-md';
89
+
90
+ const result = migrateLocalSource(oldSource, { fromVersion: 'local-draft-1' });
91
+ // {
92
+ // ok: boolean,
93
+ // fromVersion: string,
94
+ // toVersion: 'local-draft-2',
95
+ // needsMigration: boolean,
96
+ // diagnostics: Diagnostic[],
97
+ // source: string,
98
+ // changed: boolean,
99
+ // edits: { range: SourceRange; text: string }[]
100
+ // }
101
+ ```
102
+
103
+ 迁移只写入或替换元数据注释,保留正文。正文原始字节、UTF-8 BOM 和 CRLF 换行保持不变;操作幂等,已迁移到当前版本的源码返回 `changed: false` 和相同文本。`edits` 定位每处替换。迁移被阻止时不产生迁移结果,调用方传入的内容保持不变。
104
+
105
+ 把 `local-draft-1` 源码迁移到 `local-draft-2` 之前,迁移会阻止:
106
+
107
+ - 使用 17 个新增保留表达式名的绑定:`sum`、`average`、`minOf`、`maxOf`、`formatNumber`、`formatPercent`、`formatScientific`、`formatCurrency`、`trim`、`lower`、`upper`、`startsWith`、`endsWith`、`replace`、`slice`、`dateCompare`、`dateDiff`;
108
+ - 代码之外出现的 15 个新增可识别标签名:`jump`、`anchor`、`text`、`section`、`multi-select`、`range-slider`、`coordinate`、`quiz`、`steps`、`step`、`computed`、`const`、`validation`、`data-table`、`column`;
109
+ - 表达式中的数组语法或新增内置函数调用;字符串字面量中的文本不触发此检查;
110
+ - 源码当前的编译错误。
111
+
112
+ 诊断给出准确位置。迁移不重命名标识符、不改写表达式,也不激活原先按原文显示的标签;这些属于作者对源码的修改。
113
+
114
+ 文本控件需要一次明确的兼容决定。`input` 或 `textarea` 带大于零的 `min-length`,或带 `max-length` 时,astral Unicode 的读者语义会改变:draft-2 按 Unicode 码点计数,draft-1 按 UTF-16 代码单元计数。除非允许语义变化,迁移会阻止这些控件:
115
+
116
+ ```ts
117
+ migrateLocalSource(oldSource, {
118
+ fromVersion: 'local-draft-1',
119
+ allowSemanticChanges: true,
120
+ });
121
+ ```
122
+
123
+ 开启后迁移继续,并为每个受影响的控件返回警告。新的码点上限就是预期读者行为时使用该选项。
124
+
125
+ 清单完成样式是有记录的兼容例外。这项视觉变化本身不阻止迁移,也不按核心运行时语义失败处理。
126
+
127
+ 迁移会改变源码摘要。现有 `reactive-md-state/v1` 文件随后无法通过摘要校验,迁移后应重新导出状态文件。迁移不会改写状态文件。
128
+
129
+ ## CLI
130
+
131
+ ```sh
132
+ reactive-md migrate doc.reactive.md
133
+ reactive-md migrate - --from local-draft-1 -o doc.reactive.md
134
+ reactive-md migrate doc.reactive.md --check
135
+ reactive-md migrate doc.reactive.md --check --json
136
+ reactive-md migrate old.reactive.md --from local-draft-1 --accept-semantic-changes -o new.reactive.md
137
+ ```
138
+
139
+ | 选项 | 含义 |
140
+ | --- | --- |
141
+ | `[file\|-]` | 输入文件;`-` 表示标准输入 |
142
+ | `--from local-draft-1\|local-draft-2` | 源修订;默认取声明或当前修订 |
143
+ | `--check` | 只报告是否需要迁移,从不写入 |
144
+ | `--json` | 标准输出结构化报告,且是唯一的 stdout 内容 |
145
+ | `-o, --output <file>` | 新建文件;拒绝任何已存在的路径,包括输入文件本身 |
146
+ | `--accept-semantic-changes` | 允许文本控件长度语义变化,并报告警告 |
147
+ | `--locale en\|zh` | 诊断和摘要的显示语言 |
148
+
149
+ 默认把迁移后的源码写到标准输出。输入文件不会被覆盖;`-o` 使用独占创建,目标已存在即报错。没有 `-o` 时,请重定向 stdout 保存结果。
150
+
151
+ `--check` 从不写入。它输出诊断和摘要;带 `--json` 时输出 JSON 报告。需要迁移或被阻止时退出 `1`,源码已是当前版本时退出 `0`。普通迁移被阻止时退出 `1`,成功时退出 `0`。用法和 I/O 错误退出 `2`。不要把源码文本混入 JSON 报告。
152
+
153
+ 命令一次迁移一份源码。本版本不提供 `--write`、不就地覆盖,也不提供破坏性批量操作。
154
+
155
+ ## 范围
156
+
157
+ 迁移处理声明的元数据变化,并检查正文能否跨越修订边界。它不是旧版读取器:不解释 YAML `state`/`data`/`computed`/`actions` 配置块、directive 或其他 draft 之前的语法,也不改写正文。在 `local-draft-2` 下已经无法编译的源码会保持阻止状态,直到作者修复报告的位置。
158
+
159
+ `0.4.0` 是 `next` 渠道的预览版。本页说明其中的迁移工具。