cladogram 0.0.0-stage → 0.2.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 (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +316 -2
  3. package/bin/clado.js +326 -0
  4. package/examples/showcase/README.md +115 -0
  5. package/examples/showcase/baseline.md +365 -0
  6. package/examples/showcase/build.sh +48 -0
  7. package/examples/showcase/calls.md +136 -0
  8. package/examples/showcase/fulfill-cn.md +351 -0
  9. package/examples/showcase/fulfill.md +271 -0
  10. package/examples/showcase/index.md +187 -0
  11. package/examples/showcase/order.md +195 -0
  12. package/examples/showcase/promo.md +580 -0
  13. package/examples/showcase/refund.md +966 -0
  14. package/examples/showcase/seckill.md +787 -0
  15. package/package.json +42 -4
  16. package/skills/clado/SKILL.md +732 -0
  17. package/skills/clado/examples/01-/351/234/200/346/261/202/346/240/221.md +32 -0
  18. package/skills/clado/examples/02-/346/225/260/346/215/256/350/241/250.md +44 -0
  19. package/skills/clado/examples/03-/346/216/245/345/217/243/346/270/205/345/215/225.md +51 -0
  20. package/skills/clado/examples/04-/346/226/271/346/263/225/351/223/276.md +70 -0
  21. package/skills/clado/examples/05-/346/216/245/345/217/243/350/260/203/347/224/250.md +58 -0
  22. package/skills/clado/examples/06-/350/260/203/347/224/250/351/223/276.md +49 -0
  23. package/skills/clado/examples/07-/346/213/206/346/226/207/346/241/243/index.md +38 -0
  24. package/skills/clado/examples/07-/346/213/206/346/226/207/346/241/243/refund.md +33 -0
  25. package/skills/clado/examples/07-/346/213/206/346/226/207/346/241/243/seckill.md +56 -0
  26. package/skills/clado/examples/08-/347/234/237/345/256/236/350/247/204/346/250/241//346/216/245/345/217/243/345/245/221/347/272/246.md +122 -0
  27. package/skills/clado/examples/08-/347/234/237/345/256/236/350/247/204/346/250/241//346/225/205/351/232/234/345/244/215/347/233/230.md +99 -0
  28. package/skills/clado/examples/08-/347/234/237/345/256/236/350/247/204/346/250/241//350/260/203/347/224/250/351/223/276.md +219 -0
  29. package/skills/clado/examples/build.sh +44 -0
  30. package/skills/clado/references/diagnostics.md +110 -0
  31. package/src/core/diagnostics.js +39 -0
  32. package/src/core/parse.js +425 -0
  33. package/src/html/app-css.js +322 -0
  34. package/src/html/escape.js +8 -0
  35. package/src/html/page.js +163 -0
  36. package/src/html/runtime.js +764 -0
  37. package/src/html/scope-css.js +146 -0
  38. package/src/util/term.js +18 -0
  39. package/src/vendor/github-markdown-dark.css +1124 -0
  40. package/src/vendor/github-markdown-light.css +1124 -0
  41. package/src/vendor/highlight.min.js +1262 -0
  42. package/src/vendor/hljs-dark.css +118 -0
  43. package/src/vendor/hljs-light.css +118 -0
  44. package/src/vendor/marked.umd.js +81 -0
  45. package/src/vendor/mermaid.min.js +3636 -0
@@ -0,0 +1,425 @@
1
+ /**
2
+ * 把一篇 `.md` 解析成 `DOC`(形状见 `docs/spec-0.2.md` §四)。
3
+ *
4
+ * 这个文件只做三件事:
5
+ *
6
+ * 1. 切出 front matter,剩下的按**标题**切成树:`#` 是一张卡,`##`…`######` 是卡里的层;
7
+ * 2. 每个标题下面到下一个标题之间的正文,原样收进它的 `md`(一行都不改,
8
+ * 怎么渲染是客户端用 marked 的事);
9
+ * 3. 把两种边(独占一行的 `→ 目标`、正文里的内联链接)的目标**在这里一次性**换算成内部 id,
10
+ * 结果同时写进 `targets`(给客户端查表)和 `edges`(只给排布用)。
11
+ *
12
+ * ★ 为什么目标要在这里换算:客户端只认 id。0.1.0 后期就是因为「正文里存标题、
13
+ * 渲染按 id 查」对不上,5 条边静悄悄丢了 4 条。换算只做一次,就在这儿。
14
+ *
15
+ * ★ `→ 目标` 那一行**不删**,改写成 `→ [目标](目标)` —— 这样它就是一个普通内联链接,
16
+ * 端口自然会落在这一行上,卡片里也看得见「这条线为什么在这儿」。
17
+ */
18
+
19
+ /** 开头 `---` 包的 front matter(只认最前面那一块) */
20
+ const FRONT_MATTER = /^---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/;
21
+
22
+ /** `## 标题` / `## 标题 ##`(闭合的井号要吃掉) */
23
+ const HEADING = /^ {0,3}(#{1,6})(?:\s+(.*?))?\s*#*\s*$/;
24
+ /** 七级及以上:降级当六级,报 MD_HEADING_TOO_DEEP */
25
+ const TOO_DEEP = /^ {0,3}(#{7,})(?:\s+(.*?))?\s*#*\s*$/;
26
+ /** ``` 或 ~~~ 围栏 */
27
+ const FENCE = /^ {0,3}(`{3,}|~{3,})[ \t]*([^`]*)$/;
28
+ /**
29
+ * Markdown 内联链接 `[文字](目标)`,目标后面允许跟 "标题"。
30
+ *
31
+ * 目标是 `<带空格的名字>` 时走尖括号那一支 —— CommonMark 的写法,marked 会渲染成
32
+ * `%20`,客户端 `targetOf()` 解码回来正好查得到表。文档里的标题本来就常带空格
33
+ * (`POST /seckill/token`、`库存预热到 Redis`),不认尖括号这些边就写不出来。
34
+ */
35
+ const INLINE_LINK = /\[[^\]]*\]\(\s*(?:<([^<>\n]*)>|([^()\s]+))(?:\s+(?:"[^"]*"|'[^']*'))?\s*\)/g;
36
+
37
+ /**
38
+ * 一眼就能看出不是「文档里的东西」的目标 —— 当普通链接,不连线。
39
+ * 和 0.1.0 保持一字不差:外链、协议、锚点、根路径、各种文件后缀。
40
+ */
41
+ export const EXTERNAL = /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/|#|\.{1,2}\/)|\.(?:md|markdown|html?|png|jpe?g|gif|svg|webp|pdf|zip|txt|csv|json|ya?ml)$/i;
42
+
43
+ /** 「一块内容 ≤ 100 字」那条约定的阈值(`MD_CONTENT_LONG`) */
44
+ export const CONTENT_LIMIT = 100;
45
+
46
+ // ---------------------------------------------------------------- 小工具
47
+
48
+ /** 做 id 用:留下字母数字汉字,别的都折成一个连字符 */
49
+ function slug(s) {
50
+ const t = String(s)
51
+ .toLowerCase()
52
+ .replace(/[^a-z0-9\u4e00-\u9fff]+/g, '-')
53
+ .replace(/^-+|-+$/g, '');
54
+ return t || 'x';
55
+ }
56
+
57
+ /**
58
+ * 数一块内容有多少字(`MD_CONTENT_LONG` 用)。
59
+ *
60
+ * 两条「不算」:**代码块不算**(代码不是话多),**链接的 URL 不算**
61
+ * (`→ [PayCallbackFacade#verifySign](PayCallbackFacade#verifySign)` 数的是
62
+ * 看得见的 `→ PayCallbackFacade#verifySign`,不是后面那串重复的地址)。
63
+ */
64
+ export function contentLength(md) {
65
+ let n = 0;
66
+ let fence = null;
67
+ for (const line of String(md).split(/\r?\n/)) {
68
+ const f = FENCE.exec(line);
69
+ if (f) {
70
+ const ch = f[1][0];
71
+ if (!fence) fence = ch;
72
+ else if (ch === fence) fence = null;
73
+ continue;
74
+ }
75
+ if (fence) continue;
76
+ const visible = line
77
+ .replace(/!?\[([^\]]*)\]\([^()\s]*(?:\s+"[^"]*")?\)/g, '$1') // [文字](url) → 文字
78
+ .replace(/^\s{0,3}#{1,6}\s+/, '') // 行内小标题的 #
79
+ .replace(/^\s{0,3}>\s?/, '') // 引用
80
+ .replace(/[*_`~]/g, '');
81
+ n += visible.replace(/\s+/g, '').length;
82
+ }
83
+ return n;
84
+ }
85
+
86
+ // ---------------------------------------------------------------- front matter
87
+
88
+ /**
89
+ * 切 front matter。只认最开头那一块,只认 `key: value` 一行一个。
90
+ * 不引 YAML —— 我们只有 `title` 一个键,够用。
91
+ */
92
+ export function splitFrontMatter(text) {
93
+ const m = FRONT_MATTER.exec(text);
94
+ if (!m) return { body: text, meta: {}, had: false };
95
+ const meta = {};
96
+ for (const line of m[1].split(/\r?\n/)) {
97
+ const kv = /^([A-Za-z_][\w-]*)\s*:\s*(.*)$/.exec(line);
98
+ if (!kv) continue;
99
+ let v = kv[2].trim();
100
+ if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) v = v.slice(1, -1);
101
+ meta[kv[1]] = v;
102
+ }
103
+ return { body: text.slice(m[0].length), meta, had: true };
104
+ }
105
+
106
+ // ---------------------------------------------------------------- 目标解析
107
+
108
+ /**
109
+ * 把一个「人写的目标」换算成内部 id。
110
+ *
111
+ * 认三种写法(见 spec §2.4):卡片标题原文 / 节点标题原文 / `卡片标题.节点标题.…` 的完整路径。
112
+ * 对不上返回 null,对上多个返回 `{ ambiguous: [...] }` —— **绝不猜**。
113
+ */
114
+ export function makeResolver(cards) {
115
+ /** 全文档的标题 → 命中列表 */
116
+ const byTitle = new Map();
117
+ const add = (title, id) => {
118
+ if (!title) return;
119
+ if (!byTitle.has(title)) byTitle.set(title, []);
120
+ byTitle.get(title).push(id);
121
+ };
122
+ const walk = (nodes) => {
123
+ for (const n of nodes) {
124
+ add(n.title, n.id);
125
+ if (n.kids && n.kids.length) walk(n.kids);
126
+ }
127
+ };
128
+ for (const c of cards) {
129
+ add(c.title, c.id);
130
+ walk(c.nodes);
131
+ }
132
+
133
+ /** `A.B.C`:从某个标题的子节点里一层层往下走 */
134
+ const down = (nodes, rest) => {
135
+ for (const n of nodes) {
136
+ if (rest === n.title) return n.id;
137
+ if (rest.startsWith(n.title + '.') && n.kids && n.kids.length) {
138
+ const r = down(n.kids, rest.slice(n.title.length + 1));
139
+ if (r) return r;
140
+ }
141
+ }
142
+ return null;
143
+ };
144
+
145
+ return function resolve(raw) {
146
+ const t = String(raw == null ? '' : raw).trim();
147
+ if (!t) return null;
148
+
149
+ // ① 完整路径:卡片标题打头,剩下的往下走
150
+ for (const c of cards) {
151
+ if (t === c.title) return { id: c.id };
152
+ if (t.startsWith(c.title + '.') && c.nodes.length) {
153
+ const r = down(c.nodes, t.slice(c.title.length + 1));
154
+ if (r) return { id: r };
155
+ }
156
+ }
157
+ // ② 标题原文:全文档唯一才认
158
+ const hit = byTitle.get(t);
159
+ if (hit && hit.length === 1) return { id: hit[0] };
160
+ if (hit && hit.length > 1) return { ambiguous: hit };
161
+ return null;
162
+ };
163
+ }
164
+
165
+ // ---------------------------------------------------------------- 主函数
166
+
167
+ /**
168
+ * @param {string} text `.md` 的全文
169
+ * @param {{path?: string, title?: string, fileBase?: string}} opts
170
+ * @returns {{doc: object, diagnostics: object[]}}
171
+ */
172
+ export function parseDocument(text, opts = {}) {
173
+ const where = opts.path || '';
174
+ const diagnostics = [];
175
+ // ★ line 必须带上:诊断不带行号,用户拿着一句「连不到 xxx」在几百行的文档里没法找。
176
+ const at = (severity, code, message, hint, line) => diagnostics.push({
177
+ severity, code, message,
178
+ ...(where ? { path: where } : {}),
179
+ ...(line ? { line } : {}),
180
+ ...(hint ? { hint } : {}),
181
+ });
182
+
183
+ const fm = splitFrontMatter(text);
184
+ const lines = fm.body.split(/\r?\n/);
185
+
186
+ // ---- 第一遍:切标题,攒每个标题的正文 ----------------------------------
187
+
188
+ /** 一个标题攒下来的原始行。`md` 最后才 join,行号留着报诊断 */
189
+ const makeEntry = (level, title, line) => ({ level, title, line, body: [] });
190
+
191
+ const entries = []; // 按出现顺序
192
+ const preambleLines = [];
193
+ let current = null;
194
+ let fence = null; // 当前围栏的字符(` 或 ~),null = 不在围栏里
195
+ let fenceLen = 0;
196
+ let hasMermaid = false;
197
+
198
+ for (let i = 0; i < lines.length; i++) {
199
+ const line = lines[i];
200
+ const f = FENCE.exec(line);
201
+ if (f) {
202
+ const ch = f[1][0];
203
+ if (!fence) {
204
+ fence = ch;
205
+ fenceLen = f[1].length;
206
+ if (/^mermaid\b/i.test(f[2].trim())) hasMermaid = true;
207
+ } else if (ch === fence && f[1].length >= fenceLen) {
208
+ fence = null;
209
+ }
210
+ (current ? current.body : preambleLines).push(line);
211
+ continue;
212
+ }
213
+ if (fence) { (current ? current.body : preambleLines).push(line); continue; }
214
+
215
+ const deep = TOO_DEEP.exec(line);
216
+ const head = HEADING.exec(line);
217
+ if (deep) {
218
+ at('warn', 'MD_HEADING_TOO_DEEP',
219
+ `「${(deep[2] || '').trim()}」用了 ${deep[1].length} 级标题(最多 6 级)`,
220
+ '已经按 6 级处理', i + 1);
221
+ }
222
+ if (head || deep) {
223
+ const level = head ? head[1].length : 6;
224
+ const title = ((head ? head[2] : deep[2]) || '').trim();
225
+ if (!title) {
226
+ at('warn', 'MD_EMPTY_HEADING', `第 ${i + 1} 行有一个空标题(${'#'.repeat(level)})`, '补个名字,或者删掉', i + 1);
227
+ }
228
+ current = makeEntry(level, title, i + 1);
229
+ entries.push(current);
230
+ continue;
231
+ }
232
+ (current ? current.body : preambleLines).push(line);
233
+ }
234
+
235
+ if (!entries.some((e) => e.level === 1)) {
236
+ at('error', 'MD_NO_CARD', '整篇一个 `#` 都没有 —— 一张卡都画不出来',
237
+ '至少要写一行 "# 标题";`#` 是卡片的边界', 1);
238
+ }
239
+
240
+ // ---- 第二遍:按层级建树 -------------------------------------------------
241
+
242
+ const cards = [];
243
+ const stack = []; // 从当前卡往下的一条链
244
+ const allocId = (() => {
245
+ const used = new Set();
246
+ return (title) => {
247
+ const base = slug(title);
248
+ let id = base;
249
+ let n = 2;
250
+ while (used.has(id)) id = `${base}-${n++}`;
251
+ used.add(id);
252
+ return id;
253
+ };
254
+ })();
255
+
256
+ for (const e of entries) {
257
+ const node = { id: allocId(e.title), d: e.level, title: e.title, _entry: e, kids: [] };
258
+ if (e.level === 1) {
259
+ const card = { id: node.id, title: node.title, _entry: e, nodes: [] };
260
+ cards.push(card);
261
+ stack.length = 0;
262
+ stack.push({ level: 1, card, kids: card.nodes });
263
+ continue;
264
+ }
265
+ if (!cards.length) {
266
+ // `#` 之前不该有 `##`(front matter 之外的正文只可能是 preamble),保险起见当成一句提示
267
+ at('warn', 'MD_NO_CARD', `第 ${e.line} 行的「${e.title}」出现在任何 \`#\` 之前,接不上去`, '把它挪到某张卡下面', e.line);
268
+ continue;
269
+ }
270
+ // 往上找第一个层级比自己小的
271
+ while (stack.length > 1 && stack[stack.length - 1].level >= e.level) stack.pop();
272
+ const parent = stack[stack.length - 1];
273
+ if (parent.level !== e.level - 1 && e.level > parent.level + 1) {
274
+ at('warn', 'MD_HEADING_SKIP',
275
+ `「${e.title}」从 ${parent.level} 级跳到了 ${e.level} 级`,
276
+ '照常解析,但层级会看着别扭', e.line);
277
+ }
278
+ parent.kids.push(node);
279
+ stack.push({ level: e.level, card: parent.card, kids: node.kids });
280
+ }
281
+
282
+ // 重名(同一个父亲下面)
283
+ const dupCheck = (kids) => {
284
+ const seen = new Set();
285
+ for (const k of kids) {
286
+ if (seen.has(k.title)) {
287
+ at('warn', 'MD_DUPLICATE_HEADING', `同一个下面有两个「${k.title}」`, '改一个名字,或者合并', k._entry && k._entry.line);
288
+ }
289
+ seen.add(k.title);
290
+ dupCheck(k.kids);
291
+ }
292
+ };
293
+ for (const c of cards) dupCheck(c.nodes);
294
+ {
295
+ const seen = new Set();
296
+ for (const c of cards) {
297
+ if (seen.has(c.title)) at('warn', 'MD_DUPLICATE_HEADING', `有两张卡都叫「${c.title}」`, '改一个名字', c._entry && c._entry.line);
298
+ seen.add(c.title);
299
+ }
300
+ }
301
+
302
+ // ---- 节点 → 所属卡片。第三遍要用(见下),排布那一遍也要用,所以先建好 -------
303
+
304
+ // ★ 名字别叫 owner —— 第三遍扫内联链接时有个变量就叫 owner,
305
+ // 撞名之后 `owner.get` 会变成「在字符串上找 .get」。
306
+ const cardOf = new Map(); // 节点 id(含卡片自己)→ 卡片 id
307
+ for (const c of cards) {
308
+ cardOf.set(c.id, c.id);
309
+ const mark = (ns) => { for (const n of ns) { cardOf.set(n.id, c.id); mark(n.kids); } };
310
+ mark(c.nodes);
311
+ }
312
+
313
+ // ---- 第三遍:正文 + 边 --------------------------------------------------
314
+
315
+ const resolve = makeResolver(cards);
316
+ const targets = Object.create(null);
317
+ const edgePairs = [];
318
+
319
+ /** 从一张卡出发,逐层处理每个节点 */
320
+ const finish = (node, card) => {
321
+ const e = node._entry;
322
+ // ★ 以前这里还要**重写**正文(把 `→ X` 换成内联链接),所以得逐行切围栏。
323
+ // 箭头语法砍掉之后正文原样就是正文,不用再切了 —— 但扫边那一步仍然要跳围栏,
324
+ // 见上面的 outsideFences()。
325
+ const md = e.body.join('\n').replace(/^\s*\n+/, '').replace(/\s+$/, '').replace(/\n{3,}/g, '\n\n');
326
+
327
+ // 正文里的内联链接:能对上的记进 targets(客户端渲染完 marked 后靠它换 a.edge)。
328
+ // ★ 对不上**不算错** —— `[文字](目标)` 本来就是普通 Markdown 链接,
329
+ // 代码里 `arr[i](x)` 这种东西也长这样。代价是打错卡片名就静悄悄少一条线。
330
+ // ★ **逐行**扫:链接不会跨行,这样每条都能报出准确的行号。
331
+ // ★ 围栏里的跳过 —— 那是代码样例,不是边:写一篇「怎么用 clado」的文档时,
332
+ // 围栏里示范用的 `[文字](目标)` 不该在图上长出一条线来。
333
+ let inFence = false;
334
+ for (let i = 0; i < e.body.length; i++) {
335
+ const line = e.body[i];
336
+ if (FENCE.test(line)) { inFence = !inFence; continue; }
337
+ if (inFence) continue;
338
+ const inlineRe = new RegExp(INLINE_LINK.source, 'g');
339
+ let m;
340
+ while ((m = inlineRe.exec(line))) {
341
+ const raw = m[1] || m[2]; // m[1] 是 `<带空格>` 那一支,m[2] 是普通目标
342
+ // ★ `[文字](#标题)` 是最自然的「跳到那张卡」写法,但我们的文档里标题**并没有 id**
343
+ // (doc 模式的 section 挂的是 data-id),所以原样放着它在任何模式下都是个死链。
344
+ // 把开头的 `#` 剥掉再查一次表:能对上就是一条边(点一下会闪目标卡),
345
+ // 对不上就还是普通锚点,跟改之前一样。
346
+ // 注意 `#/foo`、`#!foo` 是前端路由写法,不剥。
347
+ const t = (raw[0] === '#' && raw.length > 1 && raw[1] !== '/' && raw[1] !== '!')
348
+ ? raw.slice(1) : raw;
349
+ if (EXTERNAL.test(t) || raw in targets) continue;
350
+ let got = resolve(t);
351
+ if (!got && t.includes('%')) { // 有人手写成 `(POST%20/x)` 的,解码再查一次
352
+ try { got = resolve(decodeURIComponent(t)); } catch { got = null; }
353
+ }
354
+ if (!got) continue;
355
+ const lineNo = e.line + 1 + i;
356
+ if (got.ambiguous) {
357
+ at('error', 'MD_EDGE_AMBIGUOUS',
358
+ `「${t}」能对上 ${got.ambiguous.length} 个地方:${got.ambiguous.join('、')}`,
359
+ '用 `卡片标题.标题` 的完整路径指名道姓', lineNo);
360
+ targets[raw] = '';
361
+ continue;
362
+ }
363
+ const mine = e.level === 1 ? card.id : node.id;
364
+ const theirs = cardOf.get(got.id) || got.id;
365
+ if (theirs === mine) continue;
366
+ // 键用 raw —— 客户端是拿**渲染出来的 href** 去查表的,剥过 `#` 就查不到了
367
+ targets[raw] = theirs;
368
+ edgePairs.push([mine, theirs]);
369
+ }
370
+ }
371
+
372
+ if (!md) return; // 光杆标题,没内容也没边
373
+
374
+ const n = contentLength(md);
375
+ if (n > CONTENT_LIMIT) {
376
+ at('warn', 'MD_CONTENT_LONG',
377
+ `「${e.title}」这一块有 ${n} 字(建议 ≤ ${CONTENT_LIMIT})`,
378
+ '超了说明这一层讲了两件事,拆成两个子标题', e.line);
379
+ }
380
+ return md;
381
+ };
382
+
383
+ const cleanCards = cards.map((c) => {
384
+ const card = { id: c.id, title: c.title, md: finish({ id: c.id, d: 1, title: c.title, _entry: c._entry, kids: [] }, c) || '', nodes: [] };
385
+ const walk = (nodes, into) => {
386
+ for (const nd of nodes) {
387
+ const md = finish(nd, c) || '';
388
+ const node = { id: nd.id, d: nd.d, title: nd.title, md, kids: [] };
389
+ into.push(node);
390
+ walk(nd.kids, node.kids);
391
+ }
392
+ };
393
+ walk(c.nodes, card.nodes);
394
+ return card;
395
+ });
396
+
397
+ // ---- 排布用的卡片级边(去重、去掉自环) --------------------------------
398
+
399
+ const seenEdge = new Set();
400
+ const cardEdges = [];
401
+ for (const [a, b] of edgePairs) {
402
+ const ca = cardOf.get(a);
403
+ const cb = cardOf.get(b);
404
+ if (!ca || !cb || ca === cb) continue;
405
+ const key = `${ca}\u0000${cb}`;
406
+ if (seenEdge.has(key)) continue;
407
+ seenEdge.add(key);
408
+ cardEdges.push([ca, cb]);
409
+ }
410
+
411
+ const title = opts.title || fm.meta.title || (cleanCards[0] && cleanCards[0].title) || opts.fileBase || '';
412
+ if (!fm.meta.title && !opts.title) {
413
+ at('info', 'MD_NO_PREAMBLE', '没有 front matter 的 `title`,文档标题用第一个 `#` 顶上');
414
+ }
415
+
416
+ const doc = {
417
+ title,
418
+ preamble: preambleLines.join('\n').replace(/^\s*\n+/, '').replace(/\s+$/, ''),
419
+ cards: cleanCards,
420
+ targets: { ...targets },
421
+ edges: cardEdges,
422
+ hasMermaid,
423
+ };
424
+ return { doc, diagnostics };
425
+ }