nx-kv 0.65.4 → 0.66.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 (242) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/assets/nx-kv/SKILL.md +7 -2
  3. package/assets/nx-kv/references/A02-/346/217/220/344/272/244/344/273/273/345/212/241.md +59 -4
  4. package/assets/nx-kv/references/A06-/345/217/202/350/200/203/344/273/223/345/272/223/351/251/261/345/212/250/347/232/204/345/274/200/345/217/221.md +143 -0
  5. package/assets/nx-kv/references/A07-/344/273/273/345/212/241/351/223/276/345/233/236/351/241/276/344/270/216/346/243/200/347/264/242.md +86 -0
  6. package/package.json +2 -2
  7. package/src/core/config.ts +49 -0
  8. package/src/core/db/driver.ts +1 -1
  9. package/src/core/db/migrations/index.ts +2 -0
  10. package/src/core/db/migrations/v14.sql +37 -0
  11. package/src/core/db/version.ts +1 -1
  12. package/src/core/workflow/types.ts +1 -1
  13. package/src/modules/bundled/actions/_manifest.generated.ts +12 -12
  14. package/src/modules/bundled/actions/{bundled-get.ts → skill-get.ts} +4 -4
  15. package/src/modules/bundled/actions/{bundled-groups.ts → skill-groups.ts} +4 -4
  16. package/src/modules/bundled/actions/{bundled-install.ts → skill-install.ts} +4 -4
  17. package/src/modules/bundled/actions/{bundled-list.ts → skill-list.ts} +4 -4
  18. package/src/modules/claim/actions/_manifest.generated.ts +3 -3
  19. package/src/modules/claim/actions/{localtask-claim.ts → localtask-get.ts} +15 -6
  20. package/src/modules/claim/index.ts +1 -1
  21. package/src/modules/claim/panel-escort.ts +1 -1
  22. package/src/modules/claim/wait.ts +5 -5
  23. package/src/modules/docs/actions/_manifest.generated.ts +37 -37
  24. package/src/modules/docs/actions/{doc-attach.ts → docs-attach.ts} +4 -4
  25. package/src/modules/docs/actions/{doc-attachments.ts → docs-attachments.ts} +4 -4
  26. package/src/modules/docs/actions/{doc-detach.ts → docs-detach.ts} +4 -4
  27. package/src/modules/docs/actions/{doc-get.ts → docs-get.ts} +4 -4
  28. package/src/modules/docs/actions/{doc-link-file.ts → docs-link-file.ts} +3 -3
  29. package/src/modules/docs/actions/{doc-list.ts → docs-list.ts} +4 -4
  30. package/src/modules/docs/actions/{doc-mdfile.ts → docs-mdfile.ts} +3 -3
  31. package/src/modules/docs/actions/{doc-mdfiles.ts → docs-mdfiles.ts} +3 -3
  32. package/src/modules/docs/actions/{docs-notebind.ts → docs-note-bind.ts} +3 -3
  33. package/src/modules/docs/actions/{doc-ref.ts → docs-ref.ts} +4 -4
  34. package/src/modules/docs/actions/{doc-remove.ts → docs-remove.ts} +4 -4
  35. package/src/modules/docs/actions/{doc-update.ts → docs-set.ts} +4 -4
  36. package/src/modules/docs/actions/{doc-unbind.ts → docs-unbind.ts} +4 -4
  37. package/src/modules/docs/host-scrub.ts +1 -1
  38. package/src/modules/docs/index.ts +1 -1
  39. package/src/modules/localtask/actions/_manifest.generated.ts +14 -14
  40. package/src/modules/localtask/actions/{localtask-cancelMany.ts → localtask-cancel-many.ts} +3 -3
  41. package/src/modules/localtask/actions/{localtask-closeMany.ts → localtask-close-many.ts} +3 -3
  42. package/src/modules/localtask/actions/{localtask-removeMany.ts → localtask-remove-many.ts} +3 -3
  43. package/src/modules/localtask/actions/{localtask-get.ts → localtask-show.ts} +14 -5
  44. package/src/modules/localtask/actions/localtask-tasks.ts +19 -4
  45. package/src/modules/localtask/actions/{localtask-timegoalLog.ts → localtask-timegoal-log.ts} +3 -3
  46. package/src/modules/localtask/actions/{localtask-topic-removeMany.ts → localtask-topic-remove-many.ts} +3 -3
  47. package/src/modules/localtask/infra/repo/links.ts +1 -1
  48. package/src/modules/localtask/infra/repo/settings.ts +4 -1
  49. package/src/modules/localtask/lifecycle/task-lifecycle.ts +11 -7
  50. package/src/modules/localtask/lifecycle/task-ops.ts +5 -4
  51. package/src/modules/localtask/lifecycle/task-remove.ts +1 -1
  52. package/src/modules/localtask/lifecycle/task-view.ts +25 -0
  53. package/src/modules/localtask/ui/action-render.ts +16 -2
  54. package/src/modules/localtask/ui/contract-views.ts +9 -4
  55. package/src/modules/promptplug/plugins/goal-prompt.ts +1 -1
  56. package/src/modules/promptplug/template-store.ts +163 -58
  57. package/src/modules/setting/actions/setting-set.ts +1 -0
  58. package/src/modules/setting/service.ts +15 -1
  59. package/src/modules/templates/actions/_manifest.generated.ts +18 -18
  60. package/src/modules/templates/actions/{templates-add.ts → template-add.ts} +4 -4
  61. package/src/modules/templates/actions/{templates-default.ts → template-default.ts} +4 -4
  62. package/src/modules/templates/actions/{templates-list.ts → template-list.ts} +4 -4
  63. package/src/modules/templates/actions/{templates-remove.ts → template-remove.ts} +4 -4
  64. package/src/modules/templates/actions/{templates-reorder.ts → template-reorder.ts} +4 -4
  65. package/src/modules/templates/actions/{templates-update.ts → template-update.ts} +4 -4
  66. package/src/web/frontend/App.tsx +10 -9
  67. package/src/web/frontend/api/cli-tree.json +216 -216
  68. package/src/web/frontend/api/client.generated.ts +242 -240
  69. package/src/web/frontend/components/doc/doc-head.tsx +1 -1
  70. package/src/web/frontend/components/layout/crumbs.ts +3 -9
  71. package/src/web/frontend/components/md/{attach-area.tsx → attachments/attach-area.tsx} +5 -5
  72. package/src/web/frontend/components/md/{attach-bar.tsx → attachments/attach-bar.tsx} +1 -1
  73. package/src/web/frontend/components/md/{attach-common.ts → attachments/attach-common.ts} +1 -1
  74. package/src/web/frontend/components/md/{attach-modal.tsx → attachments/attach-modal.tsx} +4 -4
  75. package/src/web/frontend/components/md/{attach-picker.tsx → attachments/attach-picker.tsx} +4 -4
  76. package/src/web/frontend/components/md/{attachments.ts → attachments/attachments.ts} +1 -1
  77. package/src/web/frontend/components/md/{composer-launch.ts → composer/composer-launch.ts} +3 -3
  78. package/src/web/frontend/components/md/{composer-panel.tsx → composer/composer-panel.tsx} +12 -12
  79. package/src/web/frontend/components/md/{use-live-preview.ts → hooks/use-live-preview.ts} +8 -8
  80. package/src/web/frontend/components/md/{use-slash-menu.ts → hooks/use-slash-menu.ts} +4 -4
  81. package/src/web/frontend/components/md/{use-source-editor.ts → hooks/use-source-editor.ts} +7 -7
  82. package/src/web/frontend/components/md/{sel-menu.tsx → menus/sel-menu.tsx} +1 -1
  83. package/src/web/frontend/components/md/model/autolink-angle/index.ts +20 -0
  84. package/src/web/frontend/components/md/model/code/index.ts +30 -0
  85. package/src/web/frontend/components/md/model/divider/index.ts +19 -0
  86. package/src/web/frontend/components/md/model/emphasis/index.ts +21 -0
  87. package/src/web/frontend/components/md/model/escape/index.ts +35 -0
  88. package/src/web/frontend/components/md/model/footnote/index.ts +77 -0
  89. package/src/web/frontend/components/md/model/heading/index.ts +19 -0
  90. package/src/web/frontend/components/md/model/highlight/index.ts +10 -0
  91. package/src/web/frontend/components/md/model/image/index.ts +31 -0
  92. package/src/web/frontend/components/md/model/inline-scan.ts +47 -0
  93. package/src/web/frontend/components/md/model/link-ref/index.ts +41 -0
  94. package/src/web/frontend/components/md/model/list-common.ts +97 -0
  95. package/src/web/frontend/components/md/model/math/index.ts +70 -0
  96. package/src/web/frontend/components/md/model/math/latex.ts +106 -0
  97. package/src/web/frontend/components/md/model/ordered-list/index.ts +27 -0
  98. package/src/web/frontend/components/md/model/paragraph/index.ts +19 -0
  99. package/src/web/frontend/components/md/model/quote/index.ts +63 -0
  100. package/src/web/frontend/components/md/model/registry.ts +74 -0
  101. package/src/web/frontend/components/md/model/table/index.ts +100 -0
  102. package/src/web/frontend/components/md/model/types.ts +92 -0
  103. package/src/web/frontend/components/md/model/unordered-list/index.ts +37 -0
  104. package/src/web/frontend/components/md/{live-block.tsx → panes/live-block.tsx} +3 -3
  105. package/src/web/frontend/components/md/{md-panes-helpers.ts → panes/md-panes-helpers.ts} +15 -4
  106. package/src/web/frontend/components/md/{md-panes-keys.ts → panes/md-panes-keys.ts} +3 -3
  107. package/src/web/frontend/components/md/{md-panes-root.tsx → panes/md-panes-root.tsx} +2 -2
  108. package/src/web/frontend/components/md/{md-panes.tsx → panes/md-panes.tsx} +12 -12
  109. package/src/web/frontend/components/md/{ack-rail.tsx → rails/ack-rail.tsx} +2 -2
  110. package/src/web/frontend/components/md/{block-rail.tsx → rails/block-rail.tsx} +2 -2
  111. package/src/web/frontend/components/md/{doc-outline-rail.tsx → rails/doc-outline-rail.tsx} +1 -1
  112. package/src/web/frontend/components/md/{shortcut-sheet.tsx → shell/shortcut-sheet.tsx} +1 -1
  113. package/src/web/frontend/components/md/{tool-bar.tsx → shell/tool-bar.tsx} +5 -7
  114. package/src/web/frontend/components/md/standalone/task-chain-graph.tsx +194 -0
  115. package/src/web/frontend/components/md/{task-chain.tsx → standalone/task-chain.tsx} +34 -11
  116. package/src/web/frontend/components/md/{template-editor.tsx → standalone/template-editor.tsx} +2 -2
  117. package/src/web/frontend/components/repos/get-loop-members.tsx +54 -0
  118. package/src/web/frontend/components/repos/get-loop-modal.tsx +6 -4
  119. package/src/web/frontend/components/sidebar/brand-row.tsx +30 -0
  120. package/src/web/frontend/components/sidebar/drag.ts +53 -1
  121. package/src/web/frontend/components/sidebar/index.tsx +121 -62
  122. package/src/web/frontend/components/sidebar/model.ts +102 -7
  123. package/src/web/frontend/components/sidebar/ns-add-modal.tsx +81 -0
  124. package/src/web/frontend/components/sidebar/ns-dialogs.tsx +67 -0
  125. package/src/web/frontend/components/sidebar/ns-manage-modal.tsx +56 -0
  126. package/src/web/frontend/components/sidebar/ns-modal.tsx +58 -0
  127. package/src/web/frontend/components/sidebar/ns-observe-modal.tsx +36 -0
  128. package/src/web/frontend/components/sidebar/ns-picker.tsx +134 -0
  129. package/src/web/frontend/components/sidebar/ns-row.tsx +70 -0
  130. package/src/web/frontend/components/sidebar/row-menu.tsx +39 -5
  131. package/src/web/frontend/components/sidebar/search-row.tsx +37 -0
  132. package/src/web/frontend/components/sidebar/use-ns.ts +178 -0
  133. package/src/web/frontend/components/sidebar/use-sidebar-ns.ts +64 -0
  134. package/src/web/frontend/components/task-panel/task-row.tsx +26 -0
  135. package/src/web/frontend/components/tool-acts/bar.tsx +7 -7
  136. package/src/web/frontend/components/tool-acts/modules.ts +7 -4
  137. package/src/web/frontend/components/use-rail-drag.ts +1 -1
  138. package/src/web/frontend/modules/board/ack-nodes.ts +1 -1
  139. package/src/web/frontend/modules/board/ack-rail-pos.ts +1 -1
  140. package/src/web/frontend/modules/board/task-search.ts +43 -0
  141. package/src/web/frontend/modules/md/attach-mention.ts +2 -2
  142. package/src/web/frontend/modules/md/doc-store.ts +2 -2
  143. package/src/web/frontend/modules/md/md-blocks.ts +18 -1
  144. package/src/web/frontend/modules/md/md-edit-ops.ts +18 -3
  145. package/src/web/frontend/modules/md/md-render.ts +108 -179
  146. package/src/web/frontend/modules/md/styles/docs.css +89 -0
  147. package/src/web/frontend/modules/md/styles/editor.css +25 -7
  148. package/src/web/frontend/modules/md/styles/panels.css +107 -2
  149. package/src/web/frontend/page/dir/landing.tsx +35 -0
  150. package/src/web/frontend/page/dir/task-actions.ts +2 -2
  151. package/src/web/frontend/page/dir/task-chain.ts +5 -2
  152. package/src/web/frontend/page/dir/task-dock-panel.tsx +19 -28
  153. package/src/web/frontend/page/dir/task-feedback.ts +4 -4
  154. package/src/web/frontend/page/dir/task-utils.ts +19 -3
  155. package/src/web/frontend/page/dir/task.tsx +16 -13
  156. package/src/web/frontend/page/doc/doc-dock.tsx +1 -1
  157. package/src/web/frontend/page/doc/index.tsx +4 -4
  158. package/src/web/frontend/page/group/index.tsx +47 -2
  159. package/src/web/frontend/page/progress/api.ts +5 -0
  160. package/src/web/frontend/page/progress/chain-view.tsx +108 -0
  161. package/src/web/frontend/page/progress/index.tsx +210 -152
  162. package/src/web/frontend/page/repos/index.tsx +0 -11
  163. package/src/web/frontend/page/repos/repo-table.tsx +1 -20
  164. package/src/web/frontend/page/settings/api.ts +0 -4
  165. package/src/web/frontend/page/settings/index.tsx +1 -12
  166. package/src/web/frontend/page/settings/prompt-template-editor.tsx +1 -1
  167. package/src/web/frontend/page/settings/prompt-templates-section.tsx +1 -1
  168. package/src/web/frontend/page/templates/index.tsx +3 -3
  169. package/src/web/frontend/page/templates/manage.tsx +1 -1
  170. package/src/web/frontend/panels.tsx +2 -8
  171. package/src/web/frontend/prefs.ts +4 -3
  172. package/src/web/frontend/registry.ts +0 -1
  173. package/src/web/frontend/routes/index.ts +1 -2
  174. package/src/web/frontend/routes/static.ts +0 -1
  175. package/src/web/frontend/styles-v2/dag.css +17 -2
  176. package/src/web/frontend/styles-v2/dir.css +0 -36
  177. package/src/web/frontend/styles-v2/sidebar.css +92 -1
  178. package/src/web/frontend/types.ts +22 -1
  179. package/src/web/frontend/use-ns-persist.ts +63 -0
  180. package/src/web/frontend/workspace/areas.ts +1 -3
  181. package/src/web/frontend/workspace/panels.ts +1 -1
  182. package/src/web/public/assets/bar-Dy_QDX2h.js +1 -0
  183. package/src/web/public/assets/board-view-Cm8189wG.js +1 -0
  184. package/src/web/public/assets/composer-launch-B7DH53Yf.js +1 -0
  185. package/src/web/public/assets/composer-panel-CWFFEe5a.js +1 -0
  186. package/src/web/public/assets/dag-DjtLPr3j.js +1 -0
  187. package/src/web/public/assets/dir-DPboO_1M.js +3 -0
  188. package/src/web/public/assets/doc-_iteSM2X.js +6 -0
  189. package/src/web/public/assets/get-loop-modal-BW2dD9kv.js +2 -0
  190. package/src/web/public/assets/group-UEOT29Zq.js +1 -0
  191. package/src/web/public/assets/index-BpNXBbNQ.css +1 -0
  192. package/src/web/public/assets/index-BxrcTrww.js +3 -0
  193. package/src/web/public/assets/md-history-Bj790fdn.js +46 -0
  194. package/src/web/public/assets/progress-MPse2JLQ.js +3 -0
  195. package/src/web/public/assets/refs-DiIPoqnJ.js +2 -0
  196. package/src/web/public/assets/{repo-form-modal-DChK4NSX.js → repo-form-modal-CNLYR12c.js} +1 -1
  197. package/src/web/public/assets/repos-BGx-yAXd.js +12 -0
  198. package/src/web/public/assets/route-crumb-DX9C4jmQ.js +1 -0
  199. package/src/web/public/assets/settings-buy_qJGW.js +1 -0
  200. package/src/web/public/assets/task-DHlwHURz.js +13 -0
  201. package/src/web/public/assets/task-status-Cjsm0xF7.js +7 -0
  202. package/src/web/public/assets/{task-utils-cBR6Gj_c.js → task-utils-CJfMODCP.js} +1 -1
  203. package/src/web/public/assets/templates-DdMaVWok.js +10 -0
  204. package/src/web/public/assets/tool-bar-D5wLBLRl.js +28 -0
  205. package/src/web/public/assets/use-rail-drag-CNOHXWng.js +1 -0
  206. package/src/web/public/index.html +11 -13
  207. package/src/web/frontend/modules/md/progress-chain.ts +0 -34
  208. package/src/web/frontend/page/overview/api.ts +0 -29
  209. package/src/web/frontend/page/overview/index.tsx +0 -378
  210. package/src/web/frontend/page/settings/sidebar-section.tsx +0 -75
  211. package/src/web/public/assets/bar-kaVB6FAa.js +0 -1
  212. package/src/web/public/assets/board-view-Cwot54HC.js +0 -1
  213. package/src/web/public/assets/composer-launch-BMTU-BD9.js +0 -1
  214. package/src/web/public/assets/composer-panel-BF6gjNXv.js +0 -1
  215. package/src/web/public/assets/dag-BRE33yI8.js +0 -1
  216. package/src/web/public/assets/dir-B0IW4NIn.js +0 -3
  217. package/src/web/public/assets/doc-DsRb-4WT.js +0 -6
  218. package/src/web/public/assets/frontmatter-Bcseml1q.js +0 -2
  219. package/src/web/public/assets/group-CS8VP6z0.js +0 -1
  220. package/src/web/public/assets/index-BT2y9cTX.js +0 -2
  221. package/src/web/public/assets/index-DSAamoav.css +0 -1
  222. package/src/web/public/assets/md-history-CJ1N9-Jf.js +0 -39
  223. package/src/web/public/assets/md-parse-DAYCREP1.js +0 -2
  224. package/src/web/public/assets/model-TD5kGXzR.js +0 -2
  225. package/src/web/public/assets/overview-Bsq58sDd.js +0 -1
  226. package/src/web/public/assets/progress-B4DfpsyS.js +0 -3
  227. package/src/web/public/assets/refs-DIiA_49Z.js +0 -2
  228. package/src/web/public/assets/repos-PGFYkUgf.js +0 -12
  229. package/src/web/public/assets/route-crumb-SiMi2C_T.js +0 -1
  230. package/src/web/public/assets/settings-BzIllshQ.js +0 -1
  231. package/src/web/public/assets/status-tag-C9YVBvQh.js +0 -1
  232. package/src/web/public/assets/task-BL5uxmJh.js +0 -11
  233. package/src/web/public/assets/task-row-cE7qblNK.js +0 -1
  234. package/src/web/public/assets/templates-Cfn3Xmsf.js +0 -10
  235. package/src/web/public/assets/use-rail-drag-Boxl7pOG.js +0 -1
  236. package/src/web/public/assets/workspace-DojQ3RP4.js +0 -28
  237. /package/src/web/frontend/components/md/{use-block-drag.ts → hooks/use-block-drag.ts} +0 -0
  238. /package/src/web/frontend/components/md/{slash-menu.tsx → menus/slash-menu.tsx} +0 -0
  239. /package/src/web/frontend/components/md/{doc-keys.ts → panes/doc-keys.ts} +0 -0
  240. /package/src/web/frontend/components/md/{find-bar.tsx → shell/find-bar.tsx} +0 -0
  241. /package/src/web/frontend/components/md/{status-bar.tsx → shell/status-bar.tsx} +0 -0
  242. /package/src/web/public/assets/{md-history-CLIs9Dr0.css → tool-bar-CLIs9Dr0.css} +0 -0
@@ -0,0 +1,97 @@
1
+ // 列表解析的公共件:把「一圈列表项」解析成条目树,由无序 / 有序两条语法共用。
2
+ //
3
+ // 支持三种真实写法(旧实现只认第一种,后两种会整段掉成普通段落):
4
+ // 1. 平铺项: `- a` / `- b`
5
+ // 2. 项内续行: `- 正文` + 缩进更深的普通行(同一项内换行,`<br>` 连接)
6
+ // 3. 嵌套子列表: `- 正文` + 缩进更深的列表项(交给 ctx.block 递归渲染)
7
+ //
8
+ // 只消费「首项缩进相等」的那些行;遇到更浅或顶格的其它内容就收手——不猜 lazy
9
+ // continuation(顶格续行),否则列表会把后面整段正文吞进来。
10
+ import type { BlockCtx } from './types.js';
11
+
12
+ /** 一条列表项:复选框标记(可见性由调用方决定)、正文行、子块行、序号。 */
13
+ export interface ListItem {
14
+ /** 任务列表的 `[ ]` / `[x]` 原文(没有则为空串)。 */
15
+ box: string;
16
+ /** 项内正文行(续行已去掉缩进)。 */
17
+ body: string[];
18
+ /** 缩进更深的子块行(原样保留缩进,供递归解析)。 */
19
+ sub: string[];
20
+ /** 有序列表的序号(无序列表恒为 0)。 */
21
+ num: number;
22
+ }
23
+
24
+ /** 一行匹配结果的形状(各语法自己的正则负责填)。 */
25
+ export interface ListLineMatch {
26
+ indent: number;
27
+ num: number;
28
+ box: string;
29
+ body: string;
30
+ }
31
+
32
+ /** 把一行解析成 ListLineMatch,不是列表项则 null。 */
33
+ export type ListLineParser = (line: string) => ListLineMatch | null;
34
+
35
+ /** 当前行缩进(tab 按 1 个字符算,与正则口径一致)。 */
36
+ export function indentOf(line: string): number {
37
+ return (/^[ \t]*/.exec(line)?.[0] ?? '').length;
38
+ }
39
+
40
+ /**
41
+ * 解析从 `i` 开始的一圈列表项。
42
+ *
43
+ * @param baseIndent 首项缩进;只有同缩进的项才算「同一圈」的新项
44
+ */
45
+ export function collectListItems(
46
+ lines: string[],
47
+ i: number,
48
+ parse: ListLineParser,
49
+ baseIndent: number,
50
+ ): { items: ListItem[]; next: number } {
51
+ const items: ListItem[] = [];
52
+ let j = i;
53
+ while (j < lines.length) {
54
+ const line = lines[j] ?? '';
55
+ const hit = parse(line);
56
+ if (hit && hit.indent === baseIndent) {
57
+ items.push({ box: hit.box, body: [hit.body], sub: [], num: hit.num });
58
+ j += 1;
59
+ continue;
60
+ }
61
+ const cur = items[items.length - 1];
62
+ if (!cur || !line.trim() || indentOf(line) <= baseIndent) break;
63
+ // 缩进更深的列表项 → 子列表;普通行 → 当前项的续行
64
+ if (hit) cur.sub.push(line);
65
+ else cur.body.push(line.trim());
66
+ j += 1;
67
+ }
68
+ return { items, next: j };
69
+ }
70
+
71
+ /**
72
+ * 把条目数组包成 `<ul>` / `<ol>`。
73
+ *
74
+ * `liParts` 返回 `{ attr, body }`:`attr` 是 `<li` 之后的属性串(含前导空格),
75
+ * `body` 是 `<li …>` 与 `</li>` 之间的内容。它拿到正文已行内渲染的串与子块 HTML。
76
+ *
77
+ * 有序列表首项序号 > 1 时输出 `start` 属性(CommonMark 语义)。
78
+ */
79
+ export function wrapList(
80
+ items: ListItem[],
81
+ tag: 'ul' | 'ol',
82
+ ctx: BlockCtx,
83
+ liParts: (item: ListItem, inner: string, subHtml: string, index: number) => {
84
+ attr: string;
85
+ body: string;
86
+ },
87
+ ): string {
88
+ const lis = items.map((it, k) => {
89
+ const inner = it.body.map((l) => ctx.inline(l)).join('<br>');
90
+ const sub = it.sub.length ? ctx.block(it.sub) : '';
91
+ const parts = liParts(it, inner, sub, k);
92
+ return `<li${parts.attr}>${parts.body}</li>`;
93
+ });
94
+ const start = items[0]?.num ?? 1;
95
+ const open = tag === 'ol' && start > 1 ? `<ol start="${start}">` : `<${tag}>`;
96
+ return `${open}${lis.join('')}</${tag}>`;
97
+ }
@@ -0,0 +1,70 @@
1
+ // 数学公式语法:块级 `$$ … $$`(可跨行)+ 行内 `$ … $`。
2
+ //
3
+ // 块级规则:以 `$$` 开头即接管,直到闭合 `$$`;单行 `$$…$$` 直接成块。
4
+ // 行内规则:在**已转义**的 HTML 串上识别 `$…$`(跳过 `<code>`/`<a>` 内部,避免
5
+ // 误伤代码与链接里的美元号)。
6
+ //
7
+ // 渲染委托给同目录 latex.ts(零依赖轻量实现,含 matrix 矩阵)。
8
+ import type { BlockMatch, BlockRule, InlineRule } from '../types.js';
9
+ import { renderLatex, stripMathDelims } from './latex.js';
10
+
11
+ /** 块级公式:`$$` 独占行或同行闭合。 */
12
+ export const mathBlockSyntax: BlockRule = {
13
+ id: 'math',
14
+ priority: 15,
15
+ match(lines, i): BlockMatch | null {
16
+ const line = lines[i] ?? '';
17
+ const t = line.trim();
18
+ if (!t.startsWith('$$')) return null;
19
+
20
+ // 单行 $$...$$(且不是仅 "$$" 开头后紧跟内容无闭合)
21
+ if (t.length > 4 && t.endsWith('$$') && !t.startsWith('$$$$')) {
22
+ const expr = stripMathDelims(t);
23
+ return { html: `<div class="md-math-block">${renderLatex(expr)}</div>`, next: i + 1 };
24
+ }
25
+
26
+ // 多行:收集到下一个单独的 `$$`
27
+ const body: string[] = [];
28
+ let j = i + 1;
29
+ const firstInline = t.slice(2).trim(); // "$$ x" 同行起头的内容
30
+ if (firstInline) body.push(firstInline);
31
+ let closed = false;
32
+ while (j < lines.length) {
33
+ const cur = lines[j] ?? '';
34
+ if (cur.trim() === '$$') {
35
+ closed = true;
36
+ j += 1;
37
+ break;
38
+ }
39
+ body.push(cur);
40
+ j += 1;
41
+ }
42
+ if (!closed && !body.length) return null;
43
+ return { html: `<div class="md-math-block">${renderLatex(body.join('\n'))}</div>`, next: j };
44
+ },
45
+ };
46
+
47
+ /** 行内公式:`$…$`(跳过标签与代码内部)。 */
48
+ export const mathInlineRule: InlineRule = (html: string): string => {
49
+ const parts = html.split(/(<[^>]*>)/);
50
+ let skip = 0;
51
+ const out: string[] = [];
52
+ for (const p of parts) {
53
+ if (p.startsWith('<')) {
54
+ const tag = /^<(\/?)\s*([a-zA-Z][\w-]*)/.exec(p);
55
+ const name = (tag && tag[2] ? tag[2] : '').toLowerCase();
56
+ if (name === 'code' || name === 'a') {
57
+ if (tag && tag[1] === '/') skip = Math.max(0, skip - 1);
58
+ else if (!p.endsWith('/>')) skip += 1;
59
+ }
60
+ out.push(p);
61
+ continue;
62
+ }
63
+ if (skip > 0) {
64
+ out.push(p);
65
+ continue;
66
+ }
67
+ out.push(p.replace(/(?<!\$)\$(?!\$)([^$\n]+?)\$(?!\$)/g, (_m, expr: string) => `<span class="md-math-inline">${renderLatex(expr.trim())}</span>`));
68
+ }
69
+ return out.join('');
70
+ };
@@ -0,0 +1,106 @@
1
+ // 轻量 LaTeX → HTML 渲染(零依赖,遵循仓库「面板包体保持轻量」的既有取舍)。
2
+ //
3
+ // 覆盖日常文档够用的子集:矩阵环境(matrix/pmatrix/bmatrix/vmatrix)、分式、
4
+ // 根式、上下标、常用希腊字母与运算符、换行。不是完整 TeX——遇到未覆盖的命令
5
+ // 退化为原样文本,绝不抛错(渲染器在阅读态高频调用,不能因一条公式白屏)。
6
+ //
7
+ // 为什么不引 KaTeX:`md-render.ts` 头注释明确不引入第三方解析依赖;KaTeX 会显著
8
+ // 增大浏览器包,且 `layering.test.mjs` 要求 web 层依赖可控。这里以「能看懂」为准。
9
+
10
+ /** HTML 转义(本模块内联一份,避免把渲染器私有 esc 耦进来)。 */
11
+ function esc(s: string): string {
12
+ return s.replace(/[&<>"']/g, (c) =>
13
+ c === '&' ? '&amp;' : c === '<' ? '&lt;' : c === '>' ? '&gt;' : c === '"' ? '&quot;' : '&#39;',
14
+ );
15
+ }
16
+
17
+ /** 常用命令 → Unicode / 结构标记。 */
18
+ const SYMBOLS: Record<string, string> = {
19
+ alpha: 'α', beta: 'β', gamma: 'γ', delta: 'δ', epsilon: 'ε', zeta: 'ζ', eta: 'η',
20
+ theta: 'θ', iota: 'ι', kappa: 'κ', lambda: 'λ', mu: 'μ', nu: 'ν', xi: 'ξ', pi: 'π',
21
+ rho: 'ρ', sigma: 'σ', tau: 'τ', phi: 'φ', chi: 'χ', psi: 'ψ', omega: 'ω',
22
+ Gamma: 'Γ', Delta: 'Δ', Theta: 'Θ', Lambda: 'Λ', Xi: 'Ξ', Pi: 'Π', Sigma: 'Σ',
23
+ Phi: 'Φ', Psi: 'Ψ', Omega: 'Ω',
24
+ times: '×', div: '÷', cdot: '·', pm: '±', mp: '∓',
25
+ le: '≤', leq: '≤', ge: '≥', geq: '≥', ne: '≠', neq: '≠', approx: '≈', equiv: '≡',
26
+ infty: '∞', sum: '∑', prod: '∏', int: '∫', partial: '∂', nabla: '∇',
27
+ to: '→', rightarrow: '→', leftarrow: '←', leftrightarrow: '↔', Rightarrow: '⇒',
28
+ cdots: '⋯', ldots: '…', dots: '…', quad: ' ', qquad: ' ',
29
+ };
30
+
31
+ const MATRIX_ENVS = ['matrix', 'pmatrix', 'bmatrix', 'vmatrix', 'Bmatrix'];
32
+
33
+ /** 把矩阵环境渲染成可排列的行列结构(CSS 负责括号与网格)。 */
34
+ function renderMatrix(env: string, body: string): string {
35
+ const rows = body
36
+ .split(/\\\\/)
37
+ .map((r) => r.trim())
38
+ .filter((r) => r.length);
39
+ const rowHtml = rows
40
+ .map(
41
+ (r) =>
42
+ '<span class="md-mrow">' +
43
+ r
44
+ .split('&')
45
+ .map((c) => `<span class="md-mcell">${renderLatex(c.trim())}</span>`)
46
+ .join('') +
47
+ '</span>',
48
+ )
49
+ .join('');
50
+ return `<span class="md-matrix md-matrix-${env}">${rowHtml}</span>`;
51
+ }
52
+
53
+ /** 递归渲染一段 LaTeX(已假定非矩阵部分)。 */
54
+ export function renderLatex(src: string): string {
55
+ let s = String(src ?? '');
56
+
57
+ // 1) 矩阵环境先抽成占位符(其 HTML 不能再被后续 esc 二次转义)
58
+ const matrixHtml: string[] = [];
59
+ for (const env of MATRIX_ENVS) {
60
+ const re = new RegExp(`\\\\begin\\{${env}\\}([\\s\\S]*?)\\\\end\\{${env}\\}`, 'g');
61
+ s = s.replace(re, (_m, body: string) => {
62
+ matrixHtml.push(renderMatrix(env, body));
63
+ return `\u0001${matrixHtml.length - 1}\u0001`;
64
+ });
65
+ }
66
+
67
+ // 2) 转义剩余文本(占位符是控制字符,不受影响),再做结构替换
68
+ s = esc(s);
69
+
70
+ // 3) 分式(迭代展开,支持一层嵌套分母/分子里的简单花括号)
71
+ for (let k = 0; k < 6; k++) {
72
+ const before = s;
73
+ s = s.replace(
74
+ /\\frac\{([^{}]*)\}\{([^{}]*)\}/g,
75
+ '<span class="md-frac"><span class="md-frac-num">$1</span><span class="md-frac-den">$2</span></span>',
76
+ );
77
+ if (s === before) break;
78
+ }
79
+
80
+ // 4) 根式
81
+ s = s.replace(/\\sqrt\{([^{}]*)\}/g, '<span class="md-sqrt">√<span class="md-sqrt-x">$1</span></span>');
82
+
83
+ // 5) 上下标:{...} 与单字符两种
84
+ s = s.replace(/\^\{([^{}]*)\}/g, '<sup>$1</sup>').replace(/\^(\w)/g, '<sup>$1</sup>');
85
+ s = s.replace(/_\{([^{}]*)\}/g, '<sub>$1</sub>').replace(/_(\w)/g, '<sub>$1</sub>');
86
+
87
+ // 6) 希腊字母 / 运算符
88
+ s = s.replace(/\\([A-Za-z]+)/g, (m, name: string) => (SYMBOLS[name] !== undefined ? SYMBOLS[name] : m));
89
+
90
+ // 7) 间距与换行收尾
91
+ s = s.replace(/\\[,;:]|\\ |\\!/g, ' ').replace(/\\\\/g, '<br>').replace(/\n/g, '<br>');
92
+
93
+ // 8) 还原矩阵 HTML(占位符 → 已渲染结构)
94
+ s = s.replace(/\u0001(\d+)\u0001/g, (_m, n: string) => matrixHtml[Number(n)] ?? '');
95
+ return s;
96
+ }
97
+
98
+ /** 归一:去掉公式两端的 `$` / `$$` / `\(` `\)` / `\[` `\]` 包裹。 */
99
+ export function stripMathDelims(raw: string): string {
100
+ const s = String(raw ?? '').trim();
101
+ if (s.startsWith('$$') && s.endsWith('$$')) return s.slice(2, -2).trim();
102
+ if (s.startsWith('$') && s.endsWith('$')) return s.slice(1, -1).trim();
103
+ if (s.startsWith('\\[') && s.endsWith('\\]')) return s.slice(2, -2).trim();
104
+ if (s.startsWith('\\(') && s.endsWith('\\)')) return s.slice(2, -2).trim();
105
+ return s;
106
+ }
@@ -0,0 +1,27 @@
1
+ // 有序列表语法:连续的 `1. ` / `1) ` 行 → `<ol><li>…</li></ol>`。
2
+ //
3
+ // 原渲染器只认无序 `- `,有序列表会掉成段落;这里补上。支持项内续行与缩进嵌套
4
+ // 子列表(解析见 list-common)。首项序号 > 1 时输出 `start` 属性(`3.` 起头的列表
5
+ // 不该被强行从 1 重新编号)。
6
+ import type { BlockMatch, BlockRule } from '../types.js';
7
+ import { collectListItems, indentOf, wrapList, type ListLineMatch } from '../list-common.js';
8
+
9
+ const ITEM_RE = /^([ \t]*)(\d+)[.)]\s+(.*)$/;
10
+
11
+ function parse(line: string): ListLineMatch | null {
12
+ const m = ITEM_RE.exec(line);
13
+ if (!m) return null;
14
+ return { indent: indentOf(line), num: Number(m[2] ?? 1), box: '', body: m[3] ?? '' };
15
+ }
16
+
17
+ export const orderedListSyntax: BlockRule = {
18
+ id: 'ordered-list',
19
+ priority: 40,
20
+ match(lines, i, ctx): BlockMatch | null {
21
+ const first = parse(lines[i] ?? '');
22
+ if (!first) return null;
23
+ const { items, next } = collectListItems(lines, i, parse, first.indent);
24
+ const html = wrapList(items, 'ol', ctx, (_it, inner, sub) => ({ attr: '', body: `${inner}${sub}` }));
25
+ return { html, next };
26
+ },
27
+ };
@@ -0,0 +1,19 @@
1
+ // 段落语法:连续未被其它规则接管的行,合成**一个**段落,行间 `<br>`。
2
+ //
3
+ // 修复的缺陷:旧编排层是「每行一个 <p>」,于是块模型里同一块的多行(用户在一个块
4
+ // 里回车换行得到的)被渲染成多个独立段落——段间距突然变大,和编辑器里看到的
5
+ // 一个块对不上。markdown 语义里,连续非空行属于同一段落,换行是**软换行**;
6
+ // 本仓库的编辑器视图按块编辑,块内换行应当可见,所以软换行统一渲染成 `<br>`。
7
+ //
8
+ // 这个函数不是 BlockRule:段落是「没有任何规则命中」的兜底形态,由编排层在确认
9
+ // 其它规则都不匹配后调用,因此不需要参与 priority 竞争。
10
+ import type { BlockCtx } from '../types.js';
11
+
12
+ /**
13
+ * 多行合一段。
14
+ *
15
+ * @param lines 该段落占用的行(已确认没有任何块级规则命中、且非空行)
16
+ */
17
+ export function paragraphHtml(lines: string[], ctx: BlockCtx): string {
18
+ return `<p>${lines.map((l) => ctx.inline(l)).join('<br>')}</p>`;
19
+ }
@@ -0,0 +1,63 @@
1
+ // 引用 / callout 语法:连续的 `>` 行合成一个块。
2
+ //
3
+ // 首行带 `[!TYPE] 标题` → 渲染成醒目色块(div.md-callout-<type>,GFM/Obsidian
4
+ // 口径);无标记 → 普通 blockquote。
5
+ //
6
+ // 块级内容:引用里可以正常写列表 / 代码块 / 表格——去掉 `>` 前缀后交给 ctx.block
7
+ // 递归渲染。callout 是卡片视觉,**纯文本正文仍按行 `<br>` 拼接**(保持既有观感与
8
+ // 测试口径);只有检测到块级语法时才走递归。
9
+ import type { BlockMatch, BlockRule } from '../types.js';
10
+
11
+ /** 去掉引用前缀后的行里,是否含块级语法的起头(决定走递归还是纯文本)。 */
12
+ const BLOCKY_RE = /^[ \t]*(?:[-*+]\s|\d+[.)]\s|```|\||#{1,6}\s|>)/m;
13
+
14
+ export const quoteSyntax: BlockRule = {
15
+ id: 'quote',
16
+ priority: 25,
17
+ match(lines, i, ctx): BlockMatch | null {
18
+ if (!(lines[i] ?? '').startsWith('>')) return null;
19
+ const q: string[] = [];
20
+ let j = i;
21
+ while (j < lines.length && (lines[j] ?? '').startsWith('>')) {
22
+ q.push((lines[j] ?? '').replace(/^>[ \t]?/, ''));
23
+ j += 1;
24
+ }
25
+ const first = q[0] ?? '';
26
+ const cm = first.match(/^\[!([A-Za-z][\w-]*)\][ \t]*(.*)$/);
27
+ if (cm) {
28
+ const type = String(cm[1] || '').toLowerCase();
29
+ const title = String(cm[2] || '').trim();
30
+ const rest = q.slice(1);
31
+ const bodyHtml = BLOCKY_RE.test(rest.join('\n'))
32
+ ? ctx.block(rest)
33
+ : splitParagraphs(rest)
34
+ .map((p) => p.map((l) => ctx.inline(l)).join('<br>'))
35
+ .join('<br><br>');
36
+ const html =
37
+ `<div class="md-callout md-callout-${ctx.esc(type)}">` +
38
+ (title ? `<div class="md-callout-t">${ctx.inline(title)}</div>` : '') +
39
+ `<div class="md-callout-b">${bodyHtml}</div></div>`;
40
+ return { html, next: j };
41
+ }
42
+ // 普通引用:整段交回渲染流程(段落合并、列表、代码块都按正文语义走)
43
+ return { html: `<blockquote>${ctx.block(q)}</blockquote>`, next: j };
44
+ },
45
+ };
46
+
47
+ /** 按空行切成段落组(callout 纯文本正文用)。 */
48
+ function splitParagraphs(lines: string[]): string[][] {
49
+ const paras: string[][] = [];
50
+ let cur: string[] = [];
51
+ for (const l of lines) {
52
+ if (!l.trim()) {
53
+ if (cur.length) {
54
+ paras.push(cur);
55
+ cur = [];
56
+ }
57
+ continue;
58
+ }
59
+ cur.push(l);
60
+ }
61
+ if (cur.length) paras.push(cur);
62
+ return paras;
63
+ }
@@ -0,0 +1,74 @@
1
+ // 语法注册表:**全仓唯一**的「有哪些语法、按什么顺序」清单。
2
+ //
3
+ // 新增一个语法 = 在 model/ 下加一个目录(导出 BlockRule / InlineRule / DocRule /
4
+ // InlinePreRule)+ 在这里追加一行 import 与数组项。md-render 只按表遍历,
5
+ // 不认识具体语法——这就是「一语法一文件夹 + 集中注册」的挂载点。
6
+ //
7
+ // 四张表对应渲染流水线的四个阶段(先手写定义、再改原文、再生成 HTML、最后套行内):
8
+ // DOC_RULES 抽走定义行(脚注 / 引用式链接),登记查表
9
+ // INLINE_PRE_RULES 改**原始文本**(转义、脚注标记、引用式链接展开)
10
+ // BLOCK_RULES 逐行块级匹配,priority 小者先试
11
+ // INLINE_RULES 作用在**已转义 HTML** 上(强调 / 高亮 / 公式 / 尖括号链接)
12
+ //
13
+ // 块级优先级复刻原渲染器 if 分支的先后:代码块 → 公式块 → 表格 → 图片 → 引用 →
14
+ // 标题 → 有序 → 分隔线 → 无序。分隔线必须早于无序列表:`* * *` 两者都像。
15
+ import type { BlockRule, DocRule, InlinePreRule, InlineRule } from './types.js';
16
+
17
+ import { codeBlockSyntax } from './code/index.js';
18
+ import { mathBlockSyntax, mathInlineRule } from './math/index.js';
19
+ import { tableSyntax } from './table/index.js';
20
+ import { imageSyntax } from './image/index.js';
21
+ import { quoteSyntax } from './quote/index.js';
22
+ import { headingSyntax } from './heading/index.js';
23
+ import { orderedListSyntax } from './ordered-list/index.js';
24
+ import { dividerSyntax } from './divider/index.js';
25
+ import { unorderedListSyntax } from './unordered-list/index.js';
26
+ import { emphasisInlineRule } from './emphasis/index.js';
27
+ import { highlightInlineRule } from './highlight/index.js';
28
+ import { angleAutolinkInlineRule } from './autolink-angle/index.js';
29
+ import { footnoteDocRule, footnotePreRule } from './footnote/index.js';
30
+ import { linkRefDocRule, linkRefPreRule } from './link-ref/index.js';
31
+ import { escapePreRule } from './escape/index.js';
32
+
33
+ /** 文档级预处理(抽定义行),按注册顺序执行。 */
34
+ export const DOC_RULES: readonly DocRule[] = Object.freeze([footnoteDocRule, linkRefDocRule]);
35
+
36
+ /**
37
+ * 行内预处理(原始文本 → 原始文本),按注册顺序执行。
38
+ * 转义排最前:被 `\` 保护起来的字符不该再被脚注/链接规则认领。
39
+ */
40
+ export const INLINE_PRE_RULES: readonly InlinePreRule[] = Object.freeze([
41
+ escapePreRule,
42
+ footnotePreRule,
43
+ linkRefPreRule,
44
+ ]);
45
+
46
+ /** 全部块级语法,按优先级升序(渲染器按此顺序尝试匹配)。 */
47
+ export const BLOCK_RULES: readonly BlockRule[] = Object.freeze(
48
+ [
49
+ codeBlockSyntax,
50
+ mathBlockSyntax,
51
+ tableSyntax,
52
+ imageSyntax,
53
+ quoteSyntax,
54
+ headingSyntax,
55
+ orderedListSyntax,
56
+ dividerSyntax,
57
+ unorderedListSyntax,
58
+ ].sort((a, b) => a.priority - b.priority),
59
+ );
60
+
61
+ /**
62
+ * 行内语法,按**套用顺序**排列(顺序敏感)。
63
+ * 强调(斜体/删除线)先于公式:公式内容里的 `*` 不该被当强调,但公式规则会
64
+ * 跳过已生成的标签内部,二者互不重叠。尖括号链接最后——它匹配的是转义后的
65
+ * `&lt;…&gt;` 实体,前面几步不该再碰它。
66
+ */
67
+ export const INLINE_RULES: readonly InlineRule[] = Object.freeze([
68
+ emphasisInlineRule,
69
+ highlightInlineRule,
70
+ mathInlineRule,
71
+ angleAutolinkInlineRule,
72
+ ]);
73
+
74
+ export type { BlockRule, DocRule, InlinePreRule, InlineRule } from './types.js';
@@ -0,0 +1,100 @@
1
+ // 表格语法(GFM pipe table):`| a | b |` + 分隔行 `| --- | :--: |` + 数据行。
2
+ //
3
+ // 一条块级规则:match 命中「表头行 + 分隔行」即整段接管,连续管道行一并消费;
4
+ // 单元格文本复用渲染器的行内渲染(ctx.inline),所以单元格里的粗体/斜体/公式/
5
+ // 链接都能生效。对齐由分隔行的冒号决定(:-- 左 / :-: 中 / --: 右)。
6
+ import type { BlockMatch, BlockRule } from '../types.js';
7
+
8
+ /**
9
+ * 拆分一行管道表:去掉首尾竖线后按**未转义**的 `|` 切、逐格 trim。
10
+ *
11
+ * `\|` 是单元格内的字面竖线(GFM 规定),必须在切分前区分开,否则「a \| b」会被
12
+ * 切成两格。切完把 `\|` 还原成 `|`——转义符是**切分**用的,不该留在单元格文本里。
13
+ */
14
+ function splitCells(line: string): string[] {
15
+ let s = line.trim();
16
+ if (s.startsWith('|')) s = s.slice(1);
17
+ if (s.endsWith('|') && !s.endsWith('\\|')) s = s.slice(0, -1);
18
+ const cells: string[] = [];
19
+ let cur = '';
20
+ for (let k = 0; k < s.length; k++) {
21
+ const ch = s[k];
22
+ if (ch === '\\' && s[k + 1] === '|') {
23
+ cur += '|';
24
+ k += 1;
25
+ continue;
26
+ }
27
+ if (ch === '|') {
28
+ cells.push(cur.trim());
29
+ cur = '';
30
+ continue;
31
+ }
32
+ cur += ch;
33
+ }
34
+ cells.push(cur.trim());
35
+ return cells;
36
+ }
37
+
38
+ /** 分隔行判定:由 `---` / `:--` / `--:` / `:-:` 之类格子组成,至少一个 `|`。 */
39
+ function isDelimiterRow(line: string): boolean {
40
+ const s = line.trim();
41
+ if (!s.includes('|') && !/^:?-{1,}:?$/.test(s)) return false;
42
+ const cells = splitCells(s);
43
+ if (!cells.length) return false;
44
+ return cells.every((c) => /^:?-{1,}:?$/.test(c));
45
+ }
46
+
47
+ /** 从分隔行解析每列对齐:left / center / right / ''(默认)。 */
48
+ function parseAligns(line: string): string[] {
49
+ return splitCells(line).map((c) => {
50
+ const left = c.startsWith(':');
51
+ const right = c.endsWith(':');
52
+ if (left && right) return 'center';
53
+ if (right) return 'right';
54
+ if (left) return 'left';
55
+ return '';
56
+ });
57
+ }
58
+
59
+ function alignAttr(align: string): string {
60
+ return align ? ` style="text-align:${align}"` : '';
61
+ }
62
+
63
+ function renderRow(cells: string[], tag: 'th' | 'td', aligns: string[], inline: (t: string) => string): string {
64
+ return (
65
+ '<tr>' +
66
+ cells
67
+ .map((c, k) => `<${tag}${alignAttr(aligns[k] ?? '')}>${inline(c)}</${tag}>`)
68
+ .join('') +
69
+ '</tr>'
70
+ );
71
+ }
72
+
73
+ export const tableSyntax: BlockRule = {
74
+ id: 'table',
75
+ priority: 20,
76
+ match(lines, i, ctx): BlockMatch | null {
77
+ const header = lines[i] ?? '';
78
+ const delim = lines[i + 1] ?? '';
79
+ if (!header.includes('|')) return null;
80
+ if (!isDelimiterRow(delim)) return null;
81
+ const aligns = parseAligns(delim);
82
+ const headCells = splitCells(header);
83
+ if (!headCells.length) return null;
84
+
85
+ const body: string[] = [];
86
+ let j = i + 2;
87
+ while (j < lines.length) {
88
+ const row = lines[j] ?? '';
89
+ if (!row.trim() || !row.includes('|')) break;
90
+ body.push(renderRow(splitCells(row), 'td', aligns, ctx.inline));
91
+ j += 1;
92
+ }
93
+ let html = '<table class="md-table"><thead>';
94
+ html += renderRow(headCells, 'th', aligns, ctx.inline);
95
+ html += '</thead>';
96
+ if (body.length) html += `<tbody>${body.join('')}</tbody>`;
97
+ html += '</table>';
98
+ return { html, next: j };
99
+ },
100
+ };
@@ -0,0 +1,92 @@
1
+ // md 语法模型层的公共类型(components/md/model 的契约)。
2
+ //
3
+ // 设计目标:**一个语法 = 一个目录**(model/<syntax>/index.ts),每个目录导出一条
4
+ // 可注册的规则;registry.ts 汇总成有序表,md-render 只做「遍历规则 → 拼 HTML」。
5
+ // 新增语法 = 加一个目录 + 在 registry 注册一次,不改渲染主流程(参见参考仓库
6
+ // milkdown 的 $nodeSchema 注册表 / blocknote 的 BlockSpec 注册表,取其「声明式
7
+ // 描述 + 集中注册」的骨架,但不引入其 ProseMirror 依赖)。
8
+ //
9
+ // 规则是**纯函数**:不碰 DOM、不持状态;渲染器把行数组与上下文传进来,规则返回
10
+ // 一段 HTML 与「消费到第几行」,多行语法(表格 / 公式块 / 代码块 / 列表)因此能
11
+ // 在不依赖跨行状态机的前提下表达。
12
+
13
+ /**
14
+ * 文档级定义表(脚注正文、引用式链接目标)。
15
+ *
16
+ * 这两类语法的定义行(`[^1]: 说明` / `[标签]: https://…`)**不就地渲染**,而是被
17
+ * 抽走登记在这里,由引用点(`[^1]` / `[文字][标签]`)查表——所以需要一次全文扫描,
18
+ * 单独一层 DocRule(见下),不能塞进逐行的 BlockRule。
19
+ */
20
+ export interface DocCtx {
21
+ /** 脚注:标签 → 正文(多行按 `\n` 存)。 */
22
+ footnotes: Map<string, string[]>;
23
+ /** 引用式链接:标签(小写归一) → 目标 URL。 */
24
+ refLinks: Map<string, string>;
25
+ /** HTML 转义(collect 里生成提示串时用)。 */
26
+ esc: (value: unknown) => string;
27
+ }
28
+
29
+ /**
30
+ * 文档级预处理规则:**抽取定义行**,把可查表登记进 DocCtx,返回剩下的行。
31
+ *
32
+ * 与 BlockRule 的区别:它不产出 HTML,只做「把定义从正文里拿走并记账」。
33
+ * 顺序敏感(先注册的先扫描),因此脚注与链接定义互不干扰即可。
34
+ */
35
+ export interface DocRule {
36
+ id: string;
37
+ collect: (lines: string[], ctx: DocCtx) => string[];
38
+ }
39
+
40
+ /** 规则渲染时可用的上下文(由 md-render 构造一次,逐个规则传入)。 */
41
+ export interface BlockCtx {
42
+ /**
43
+ * 子块渲染(列表嵌套、引用内的块级内容):把一段行交给完整渲染流程。
44
+ * 传入的行已经是「去掉外层前缀」的干净行,递归不会回到同一处。
45
+ */
46
+ block: (lines: string[]) => string;
47
+ /** 文档级定义表(脚注 / 引用式链接)。 */
48
+ doc: DocCtx;
49
+ /** 行内渲染(链接 / 双链 / 附件 / 强调 / 行内公式;规则内对单元格/标题文本复用)。 */
50
+ inline: (text: string) => string;
51
+ /** HTML 转义(& < > " ')。 */
52
+ esc: (value: unknown) => string;
53
+ /** 标题锚点 id 分配(同文同名自动去重;只用标题规则需要)。 */
54
+ headingId: (text: string, used: Set<string>) => string;
55
+ /** 图片名 → URL 映射(v1 草稿的 name→dataURL 兜底;图片规则用)。 */
56
+ imgMap: Record<string, string>;
57
+ /** 已分配的标题 id 集合(渲染级共享,保证同文内不撞)。 */
58
+ usedIds: Set<string>;
59
+ }
60
+
61
+ /** 一条块级规则的匹配结果:`html` 是产出,`next` 是下一条待处理行(含)。 */
62
+ export interface BlockMatch {
63
+ html: string;
64
+ next: number;
65
+ }
66
+
67
+ /** 块级语法规则:match 命中即整段接管,返回消费到第几行。 */
68
+ export interface BlockRule {
69
+ /** 稳定 id(调试 / 注册表去重用)。 */
70
+ id: string;
71
+ /** 匹配优先级(小者先试);等价于旧渲染器里 if 分支的先后顺序。 */
72
+ priority: number;
73
+ match: (lines: string[], i: number, ctx: BlockCtx) => BlockMatch | null;
74
+ }
75
+
76
+ /** 行内语法规则:输入是**已转义**的 HTML 串,返回替换后的串(链式套用)。 */
77
+ export type InlineRule = (html: string) => string;
78
+
79
+ /** 行内预处理的环境(还没转义,只有定义表可查)。 */
80
+ export interface InlineEnv {
81
+ footnotes: Map<string, string[]>;
82
+ refLinks: Map<string, string>;
83
+ }
84
+
85
+ /**
86
+ * 行内**预处理**规则:吃原始文本、吐原始文本(或占位符),在 HTML 转义之前跑。
87
+ *
88
+ * 为什么需要这一层:转义(反斜杠转义、脚注引用标记、引用式链接)必须在 `esc()`
89
+ * 之前改写原文;一旦 `*` 被转成 `&ast;` 或标签已生成,就再也改不动了。
90
+ * 需要产出 HTML 的规则(尖括号自动链接)不走这里——它在转义之后按实体匹配。
91
+ */
92
+ export type InlinePreRule = (text: string, env: InlineEnv) => string;