cladogram 0.3.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -365
- package/bin/clado.js +126 -72
- package/package.json +11 -8
- package/skills/clado/SKILL.md +123 -747
- package/src/core/links.js +37 -0
- package/src/core/media-links.js +99 -0
- package/src/core/parse.js +81 -82
- package/src/html/app-css.js +51 -75
- package/src/html/canvas-card.js +65 -0
- package/src/html/canvas-chain.js +231 -0
- package/src/html/canvas-focus.js +175 -0
- package/src/html/canvas-ui.css +38 -0
- package/src/html/canvas-ui.js +40 -0
- package/src/html/canvas-wires.js +73 -0
- package/src/html/media-anchors.js +212 -0
- package/src/html/mindmap-connectors.js +78 -0
- package/src/html/mindmap-media.js +39 -0
- package/src/html/page.js +23 -4
- package/src/html/runtime-api.js +51 -0
- package/src/html/runtime-fixes.js +32 -0
- package/src/html/runtime.js +322 -272
- package/src/server/assets/library-canvas.js +96 -0
- package/src/server/assets/library.css +9 -0
- package/src/server/assets/library.html +20 -0
- package/src/server/assets/library.js +127 -0
- package/src/server/assets/tree.js +26 -0
- package/src/server/export.js +53 -0
- package/src/server/mcp.js +108 -0
- package/src/server/workspace.js +84 -0
- package/src/skill/install.js +16 -18
- package/src/skill/targets.js +28 -12
- package/src/skill/templates.js +60 -0
- package/src/util/version.js +3 -0
- package/src/workspace/content.js +72 -0
- package/src/workspace/documents.js +65 -0
- package/src/workspace/references.js +101 -0
- package/src/workspace/service.js +457 -0
- package/src/workspace/storage.js +169 -0
- package/src/workspace/tree.js +66 -0
- package/templates/business-flow.mdc +29 -0
- package/templates/call-chain.mdc +62 -0
- package/templates/data-design.mdc +35 -0
- package/templates/mind-expansion.mdc +52 -0
- package/templates/requirement-overview.mdc +29 -0
- package/templates/system-state.mdc +29 -0
- package/examples/showcase/README.md +0 -115
- package/examples/showcase/baseline.md +0 -365
- package/examples/showcase/build.sh +0 -48
- package/examples/showcase/calls.md +0 -136
- package/examples/showcase/fulfill-cn.md +0 -351
- package/examples/showcase/fulfill.md +0 -271
- package/examples/showcase/index.md +0 -187
- package/examples/showcase/order.md +0 -195
- package/examples/showcase/promo.md +0 -580
- package/examples/showcase/refund.md +0 -966
- package/examples/showcase/seckill.md +0 -787
- package/skills/clado/examples/01-/351/234/200/346/261/202/346/240/221.md +0 -32
- package/skills/clado/examples/02-/346/225/260/346/215/256/350/241/250.md +0 -44
- package/skills/clado/examples/03-/346/216/245/345/217/243/346/270/205/345/215/225.md +0 -51
- package/skills/clado/examples/04-/346/226/271/346/263/225/351/223/276.md +0 -70
- package/skills/clado/examples/05-/346/216/245/345/217/243/350/260/203/347/224/250.md +0 -58
- package/skills/clado/examples/06-/350/260/203/347/224/250/351/223/276.md +0 -49
- package/skills/clado/examples/07-/346/213/206/346/226/207/346/241/243/index.md +0 -38
- package/skills/clado/examples/07-/346/213/206/346/226/207/346/241/243/refund.md +0 -33
- package/skills/clado/examples/07-/346/213/206/346/226/207/346/241/243/seckill.md +0 -56
- 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 +0 -122
- 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 +0 -99
- 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 +0 -219
- package/skills/clado/examples/build.sh +0 -44
- package/skills/clado/references/diagnostics.md +0 -129
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Inline links with balanced parentheses and escaped punctuation. Fences and code are not links. */
|
|
2
|
+
export function markdownLinks(source, { includeImages = false } = {}) {
|
|
3
|
+
const result = []; let fence = null, offset = 0;
|
|
4
|
+
for (const line of source.split(/(?<=\n)/)) {
|
|
5
|
+
const f = /^ {0,3}(`{3,}|~{3,})/.exec(line);
|
|
6
|
+
if (f) { if (!fence) fence = f[1]; else if (f[1][0] === fence[0] && f[1].length >= fence.length) fence = null; offset += line.length; continue; }
|
|
7
|
+
if (fence) { offset += line.length; continue; }
|
|
8
|
+
for (let i = 0; i < line.length; i++) {
|
|
9
|
+
if (line[i] === '\\') { i++; continue; }
|
|
10
|
+
if (line[i] === '`') {
|
|
11
|
+
const run = /^`+/.exec(line.slice(i))[0], end = line.indexOf(run, i + run.length);
|
|
12
|
+
if (end >= 0) { i = end + run.length - 1; continue; }
|
|
13
|
+
}
|
|
14
|
+
if (line[i] !== '[' || (!includeImages && line[i - 1] === '!')) continue;
|
|
15
|
+
let j = i + 1, depth = 1;
|
|
16
|
+
for (; j < line.length && depth; j++) { if (line[j] === '\\') j++; else if (line[j] === '[') depth++; else if (line[j] === ']') depth--; }
|
|
17
|
+
if (depth || line[j] !== '(') continue;
|
|
18
|
+
const label = line.slice(i + 1, j - 1); let k = j + 1;
|
|
19
|
+
while (/\s/.test(line[k] || '') && k < line.length) k++;
|
|
20
|
+
const angle = line[k] === '<', start = angle ? ++k : k; depth = 0;
|
|
21
|
+
for (; k < line.length; k++) {
|
|
22
|
+
const ch = line[k]; if (ch === '\\') { k++; continue; }
|
|
23
|
+
if (angle ? ch === '>' : (ch === ')' && !depth) || /\s/.test(ch)) break;
|
|
24
|
+
if (!angle && ch === '(') depth++; else if (!angle && ch === ')') depth--;
|
|
25
|
+
}
|
|
26
|
+
const end = k; if (depth || (angle && line[k++] !== '>')) continue;
|
|
27
|
+
while (/\s/.test(line[k] || '') && k < line.length) k++;
|
|
28
|
+
let title;
|
|
29
|
+
if (line[k] === '"' || line[k] === "'") { const quote = line[k++], titleStart = k; while (k < line.length && line[k] !== quote) { if (line[k] === '\\') k++; k++; } title = line.slice(titleStart, k); if (line[k++] !== quote) continue; while (/\s/.test(line[k] || '') && k < line.length) k++; }
|
|
30
|
+
if (line[k] !== ')') continue;
|
|
31
|
+
result.push({ start: offset + i, end: offset + k + 1, targetStart: offset + start, targetEnd: offset + end, label, target: line.slice(start, end).replace(/\\([\\()[\]<>])/g, '$1'), ...(title === undefined ? {} : { title }), ...(includeImages ? { image: line[i - 1] === '!' } : {}) });
|
|
32
|
+
i = k;
|
|
33
|
+
}
|
|
34
|
+
offset += line.length;
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { runInNewContext } from 'node:vm';
|
|
3
|
+
|
|
4
|
+
const vendor = { exports: {} };
|
|
5
|
+
runInNewContext(readFileSync(new URL('../vendor/marked.umd.js', import.meta.url), 'utf8'), { module: vendor, exports: vendor.exports });
|
|
6
|
+
const { lexer } = vendor.exports;
|
|
7
|
+
|
|
8
|
+
export function mediaLinkPoint(title) {
|
|
9
|
+
const match = /^clado:(code|table|mermaid):([^\s;]+)(?:;text:([^\s;]+))?(?:;to:[1-9]\d*:[1-9]\d*)?$/.exec(title || '');
|
|
10
|
+
if (!match) return null;
|
|
11
|
+
const [, kind, value, encodedText] = match;
|
|
12
|
+
if (encodedText && kind !== 'code') return null;
|
|
13
|
+
if (kind === 'mermaid') return /^[\w.-]+$/.test(value) ? { kind, nodeId: value } : null;
|
|
14
|
+
if (!/^[1-9]\d*$/.test(value) || !Number.isSafeInteger(+value)) return null;
|
|
15
|
+
try { return { kind, [kind === 'code' ? 'line' : 'row']: +value, ...(encodedText ? { text: decodeURIComponent(encodedText) } : {}) }; }
|
|
16
|
+
catch { return null; }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function mediaTargetPoint(title) {
|
|
20
|
+
const match = /;to:([1-9]\d*):([1-9]\d*)$/.exec(title || '');
|
|
21
|
+
return match ? { kind: 'table', table: +match[1], row: +match[2] } : null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function mediaLinkTitle(point) {
|
|
25
|
+
return 'clado:' + point.kind + ':' + (point.line ?? point.row ?? point.nodeId) + (point.text ? ';text:' + encodeURIComponent(point.text) : '');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// A method name inside a longer identifier is not a second occurrence.
|
|
29
|
+
export function codeReferenceOffset(line, reference) {
|
|
30
|
+
const identifier = /^[\p{ID_Start}_$][\p{ID_Continue}$]*$/u.test(reference);
|
|
31
|
+
const word = /[\p{ID_Continue}$]/u;
|
|
32
|
+
let match = -1;
|
|
33
|
+
for (let index = line.indexOf(reference); index >= 0; index = line.indexOf(reference, index + 1)) {
|
|
34
|
+
const end = index + reference.length;
|
|
35
|
+
if (identifier && (word.test(line[index - 1] || '') || word.test(line[end] || ''))) continue;
|
|
36
|
+
if (match >= 0) return -1;
|
|
37
|
+
match = index;
|
|
38
|
+
}
|
|
39
|
+
return match;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function mediaBlock(token) {
|
|
43
|
+
if (token?.type === 'table') return { kind: 'table', rows: token.rows.length };
|
|
44
|
+
if (token?.type !== 'code') return null;
|
|
45
|
+
if (/^mermaid(?:\s|$)/i.test(token.lang || '')) return { kind: 'mermaid', source: token.text };
|
|
46
|
+
return { kind: 'code', lines: token.text.split('\n') };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function blockMedia(text) {
|
|
50
|
+
const tokens = lexer(text).filter(token => token.type !== 'space');
|
|
51
|
+
return tokens.length === 1 ? mediaBlock(tokens[0]) : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function tableBlock(markdown, index) {
|
|
55
|
+
let offset = 0, count = 0;
|
|
56
|
+
for (const token of lexer(markdown)) {
|
|
57
|
+
if (token.type === 'table' && ++count === index) return { blockStart: offset, blockEnd: offset + token.raw.trimEnd().length, rows: token.rows.length };
|
|
58
|
+
offset += token.raw.length;
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function mediaPointFits(media, point) {
|
|
64
|
+
if (media?.kind !== point?.kind) return false;
|
|
65
|
+
if (point.kind === 'code') {
|
|
66
|
+
const line = media.lines[point.line - 1];
|
|
67
|
+
if (line === undefined) return false;
|
|
68
|
+
if (!point.text) return true;
|
|
69
|
+
return codeReferenceOffset(line, point.text) >= 0;
|
|
70
|
+
}
|
|
71
|
+
if (point.kind === 'table') return point.row <= media.rows;
|
|
72
|
+
const statements = media.source.split(/\n|;/).filter(line => !/^\s*(?:%%|flowchart\b|graph\b|classDef\b|class\s|style\s|linkStyle\b|direction\b|end\s*$)/.test(line));
|
|
73
|
+
return statements.some(line => {
|
|
74
|
+
const graph = line.replace(/"(?:[^"\\]|\\.)*"|\[[^\]\n]*\]|\{[^{}\n]*\}|\([^()\n]*\)|\|[^|\n]*\|/g, ' ');
|
|
75
|
+
return graph.replace(/[-.=]+[><]|[<][-.=]+|[-.=]{2,}/g, ' ').split(/[^\w.-]+/).includes(point.nodeId);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// A standalone annotation follows its media block. Consecutive annotations share that block.
|
|
80
|
+
export function mediaLinkLocations(markdown, links) {
|
|
81
|
+
const annotated = links.filter(link => link.title?.startsWith('clado:'));
|
|
82
|
+
if (!annotated.length) return new Map();
|
|
83
|
+
const locations = new Map();
|
|
84
|
+
let offset = 0, media = null, blockStart, blockEnd;
|
|
85
|
+
for (const token of lexer(markdown)) {
|
|
86
|
+
const end = offset + token.raw.length;
|
|
87
|
+
const block = mediaBlock(token);
|
|
88
|
+
if (block) { media = block; blockStart = offset; blockEnd = end; }
|
|
89
|
+
else if (token.type !== 'space') {
|
|
90
|
+
const link = annotated.find(item => item.start >= offset && item.end <= end);
|
|
91
|
+
if (link && token.raw.trim() === markdown.slice(link.start, link.end)) {
|
|
92
|
+
const point = mediaLinkPoint(link.title);
|
|
93
|
+
locations.set(link.start, { point, valid: !!point && mediaPointFits(media, point), blockStart, blockEnd });
|
|
94
|
+
} else { media = null; blockStart = blockEnd = undefined; }
|
|
95
|
+
}
|
|
96
|
+
offset = end;
|
|
97
|
+
}
|
|
98
|
+
return locations;
|
|
99
|
+
}
|
package/src/core/parse.js
CHANGED
|
@@ -1,22 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
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
|
-
* 端口自然会落在这一行上,卡片里也看得见「这条线为什么在这儿」。
|
|
2
|
+
* MDC / Markdown → 卡片、L1–L6层级和正文内链。
|
|
3
|
+
* 头部保留模板规则,正文内容不改写;链接目标在解析时解析为渲染器ID。
|
|
4
|
+
* 独占的箭头文字没有特殊语法,只有实际Markdown内链形成关系。
|
|
17
5
|
*/
|
|
18
6
|
|
|
19
|
-
|
|
7
|
+
import { parse as parseYaml } from 'yaml';
|
|
8
|
+
import { markdownLinks } from './links.js';
|
|
9
|
+
import { mediaLinkLocations } from './media-links.js';
|
|
10
|
+
|
|
11
|
+
/** 开头 `---` 包的 front matter */
|
|
20
12
|
const FRONT_MATTER = /^---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/;
|
|
21
13
|
|
|
22
14
|
/** `## 标题` / `## 标题 ##`(闭合的井号要吃掉) */
|
|
@@ -25,23 +17,21 @@ const HEADING = /^ {0,3}(#{1,6})(?:\s+(.*?))?\s*#*\s*$/;
|
|
|
25
17
|
const TOO_DEEP = /^ {0,3}(#{7,})(?:\s+(.*?))?\s*#*\s*$/;
|
|
26
18
|
/** ``` 或 ~~~ 围栏 */
|
|
27
19
|
const FENCE = /^ {0,3}(`{3,}|~{3,})[ \t]*([^`]*)$/;
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
*/
|
|
35
|
-
const INLINE_LINK = /\[[^\]]*\]\(\s*(?:<([^<>\n]*)>|([^()\s]+))(?:\s+(?:"[^"]*"|'[^']*'))?\s*\)/g;
|
|
20
|
+
/** 解析器与MCP节点树共用的标题识别,包含空标题和超深标题降级。 */
|
|
21
|
+
export function headingOf(line) {
|
|
22
|
+
const m = HEADING.exec(line) || TOO_DEEP.exec(line);
|
|
23
|
+
return m ? { level: Math.min(6, m[1].length), title: (m[2] || '').trim() } : null;
|
|
24
|
+
}
|
|
25
|
+
export function fenceOf(line) { return FENCE.exec(line); }
|
|
36
26
|
|
|
37
27
|
/**
|
|
38
28
|
* 一眼就能看出不是「文档里的东西」的目标 —— 当普通链接,不连线。
|
|
39
29
|
* 和 0.1.0 保持一字不差:外链、协议、锚点、根路径、各种文件后缀。
|
|
40
30
|
*/
|
|
41
|
-
export const EXTERNAL = /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/|#|\.{1,2}\/)|\.(?:
|
|
31
|
+
export const EXTERNAL = /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/|#|\.{1,2}\/)|\.(?:mdc?|markdown|html?|png|jpe?g|gif|svg|webp|pdf|zip|txt|csv|json|ya?ml)$/i;
|
|
42
32
|
|
|
43
|
-
/**
|
|
44
|
-
export const CONTENT_LIMIT =
|
|
33
|
+
/** 「一块内容建议 ≤ 400 字」那条约定的阈值(`MD_CONTENT_LONG`) */
|
|
34
|
+
export const CONTENT_LIMIT = 400;
|
|
45
35
|
|
|
46
36
|
// ---------------------------------------------------------------- 小工具
|
|
47
37
|
|
|
@@ -85,22 +75,17 @@ export function contentLength(md) {
|
|
|
85
75
|
|
|
86
76
|
// ---------------------------------------------------------------- front matter
|
|
87
77
|
|
|
88
|
-
/**
|
|
89
|
-
* 切 front matter。只认最开头那一块,只认 `key: value` 一行一个。
|
|
90
|
-
* 不引 YAML —— 我们只有 `title` 一个键,够用。
|
|
91
|
-
*/
|
|
78
|
+
/** 最开头的 YAML 头部,支持模板规则中的多行文字和数组。 */
|
|
92
79
|
export function splitFrontMatter(text) {
|
|
93
80
|
const m = FRONT_MATTER.exec(text);
|
|
94
|
-
if (!m) return { body: text, meta: {}, had: false };
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
return { body: text.slice(m[0].length), meta, had: true };
|
|
81
|
+
if (!m) return { body: text, meta: {}, had: false, errors: [] };
|
|
82
|
+
let meta = {}, errors = [];
|
|
83
|
+
try {
|
|
84
|
+
meta = parseYaml(m[1], { maxAliasCount: 0, uniqueKeys: true }) || {};
|
|
85
|
+
if (Array.isArray(meta) || typeof meta !== 'object') throw new Error('头部必须为 YAML 字段对象');
|
|
86
|
+
if (meta.title !== undefined && typeof meta.title !== 'string') throw new Error('title 必须是文字');
|
|
87
|
+
} catch (error) { errors = [error.message]; meta = {}; }
|
|
88
|
+
return { body: text.slice(m[0].length), meta, had: true, errors };
|
|
104
89
|
}
|
|
105
90
|
|
|
106
91
|
// ---------------------------------------------------------------- 目标解析
|
|
@@ -129,35 +114,24 @@ export function makeResolver(cards) {
|
|
|
129
114
|
add(c.title, c.id);
|
|
130
115
|
walk(c.nodes);
|
|
131
116
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
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
|
-
};
|
|
117
|
+
const fullPaths = new Map(), suffixPaths = new Map();
|
|
118
|
+
const remember = (map, key, id) => { if (!map.has(key)) map.set(key, []); if (!map.get(key).includes(id)) map.get(key).push(id); };
|
|
119
|
+
const paths = (nodes, parent) => { for (const node of nodes) { const path = [...parent, node.title]; remember(fullPaths, path.join('.'), node.id); for (let i = 1; i < path.length; i++) remember(suffixPaths, path.slice(i).join('.'), node.id); paths(node.nodes || node.kids || [], path); } };
|
|
120
|
+
paths(cards, []);
|
|
144
121
|
|
|
145
122
|
return function resolve(raw) {
|
|
146
123
|
const t = String(raw == null ? '' : raw).trim();
|
|
147
124
|
if (!t) return null;
|
|
148
125
|
|
|
149
|
-
//
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
}
|
|
126
|
+
// 完整路径优先;唯一的中间层级路径也可用于文章重组后的旧引用。
|
|
127
|
+
const exact = fullPaths.get(t);
|
|
128
|
+
if (exact) return exact.length === 1 ? { id: exact[0] } : { ambiguous: exact };
|
|
157
129
|
// ② 标题原文:全文档唯一才认
|
|
158
130
|
const hit = byTitle.get(t);
|
|
159
131
|
if (hit && hit.length === 1) return { id: hit[0] };
|
|
160
132
|
if (hit && hit.length > 1) return { ambiguous: hit };
|
|
133
|
+
const suffix = suffixPaths.get(t);
|
|
134
|
+
if (suffix) return suffix.length === 1 ? { id: suffix[0] } : { ambiguous: suffix };
|
|
161
135
|
return null;
|
|
162
136
|
};
|
|
163
137
|
}
|
|
@@ -165,8 +139,8 @@ export function makeResolver(cards) {
|
|
|
165
139
|
// ---------------------------------------------------------------- 主函数
|
|
166
140
|
|
|
167
141
|
/**
|
|
168
|
-
* @param {string} text `.md` 的全文
|
|
169
|
-
* @param {{path?: string, title?: string, fileBase?: string}} opts
|
|
142
|
+
* @param {string} text `.mdc` 或旧 `.md` 的全文
|
|
143
|
+
* @param {{path?: string, title?: string, fileBase?: string, requireCardConnections?: boolean}} opts
|
|
170
144
|
* @returns {{doc: object, diagnostics: object[]}}
|
|
171
145
|
*/
|
|
172
146
|
export function parseDocument(text, opts = {}) {
|
|
@@ -197,11 +171,13 @@ export function parseDocument(text, opts = {}) {
|
|
|
197
171
|
*/
|
|
198
172
|
if (hadBom) {
|
|
199
173
|
at('warn', 'MD_BOM', '文件开头有个看不见的 UTF-8 BOM,已经替你吃掉了',
|
|
200
|
-
'
|
|
174
|
+
'已兼容该字符,后续内容修改仍通过 MCP。', 1);
|
|
201
175
|
}
|
|
202
176
|
|
|
203
177
|
const fm = splitFrontMatter(text);
|
|
178
|
+
for (const error of fm.errors) at('error', 'MD_FRONT_MATTER_INVALID', '文档头部无法解析:' + error, '修正 YAML 头部格式', 1);
|
|
204
179
|
const lines = fm.body.split(/\r?\n/);
|
|
180
|
+
const lineOffset = text.slice(0,text.length - fm.body.length).split(/\r?\n/).length - 1;
|
|
205
181
|
|
|
206
182
|
// ---- 第一遍:切标题,攒每个标题的正文 ----------------------------------
|
|
207
183
|
|
|
@@ -213,6 +189,7 @@ export function parseDocument(text, opts = {}) {
|
|
|
213
189
|
let current = null;
|
|
214
190
|
let fence = null; // 当前围栏的字符(` 或 ~),null = 不在围栏里
|
|
215
191
|
let fenceLen = 0;
|
|
192
|
+
let fenceLine = 0;
|
|
216
193
|
let hasMermaid = false;
|
|
217
194
|
|
|
218
195
|
for (let i = 0; i < lines.length; i++) {
|
|
@@ -223,6 +200,7 @@ export function parseDocument(text, opts = {}) {
|
|
|
223
200
|
if (!fence) {
|
|
224
201
|
fence = ch;
|
|
225
202
|
fenceLen = f[1].length;
|
|
203
|
+
fenceLine = i + 1 + lineOffset;
|
|
226
204
|
if (/^mermaid\b/i.test(f[2].trim())) hasMermaid = true;
|
|
227
205
|
} else if (ch === fence && f[1].length >= fenceLen) {
|
|
228
206
|
fence = null;
|
|
@@ -237,21 +215,23 @@ export function parseDocument(text, opts = {}) {
|
|
|
237
215
|
if (deep) {
|
|
238
216
|
at('warn', 'MD_HEADING_TOO_DEEP',
|
|
239
217
|
`「${(deep[2] || '').trim()}」用了 ${deep[1].length} 级标题(最多 6 级)`,
|
|
240
|
-
'已经按 6 级处理', i + 1);
|
|
218
|
+
'已经按 6 级处理', i + 1 + lineOffset);
|
|
241
219
|
}
|
|
242
220
|
if (head || deep) {
|
|
243
221
|
const level = head ? head[1].length : 6;
|
|
244
222
|
const title = ((head ? head[2] : deep[2]) || '').trim();
|
|
245
223
|
if (!title) {
|
|
246
|
-
at('warn', 'MD_EMPTY_HEADING', `第 ${i + 1} 行有一个空标题(${'#'.repeat(level)})`, '补个名字,或者删掉', i + 1);
|
|
224
|
+
at('warn', 'MD_EMPTY_HEADING', `第 ${i + 1 + lineOffset} 行有一个空标题(${'#'.repeat(level)})`, '补个名字,或者删掉', i + 1 + lineOffset);
|
|
247
225
|
}
|
|
248
|
-
current = makeEntry(level, title, i + 1);
|
|
226
|
+
current = makeEntry(level, title, i + 1 + lineOffset);
|
|
249
227
|
entries.push(current);
|
|
250
228
|
continue;
|
|
251
229
|
}
|
|
252
230
|
(current ? current.body : preambleLines).push(line);
|
|
253
231
|
}
|
|
254
232
|
|
|
233
|
+
if (fence) at('warn', 'MD_FENCE_UNCLOSED', '代码围栏没有闭合,后续内容会一直被当作代码。', '在代码末尾补上相同字符、至少相同长度的闭合围栏。', fenceLine);
|
|
234
|
+
|
|
255
235
|
if (!entries.some((e) => e.level === 1)) {
|
|
256
236
|
at('error', 'MD_NO_CARD', '整篇一个 `#` 都没有 —— 一张卡都画不出来',
|
|
257
237
|
'至少要写一行 "# 标题";`#` 是卡片的边界', 1);
|
|
@@ -340,7 +320,8 @@ export function parseDocument(text, opts = {}) {
|
|
|
340
320
|
* 字段,把线连到对方的 `## id` 上才说得清是哪个字段对哪个字段。
|
|
341
321
|
* 键和 targets 一样用链接的**原文**(客户端是拿渲染出来的 href 查表的)。
|
|
342
322
|
*/
|
|
343
|
-
const anchors = Object.create(null);
|
|
323
|
+
const anchors = Object.create(null), targetNodes = Object.create(null), titleOf = new Map();
|
|
324
|
+
for (const c of cards) { titleOf.set(c.id,c.title); const walk=ns=>ns.forEach(n=>{titleOf.set(n.id,n.title);walk(n.kids);});walk(c.nodes); }
|
|
344
325
|
const edgePairs = [];
|
|
345
326
|
|
|
346
327
|
/** 从一张卡出发,逐层处理每个节点 */
|
|
@@ -349,7 +330,10 @@ export function parseDocument(text, opts = {}) {
|
|
|
349
330
|
// ★ 以前这里还要**重写**正文(把 `→ X` 换成内联链接),所以得逐行切围栏。
|
|
350
331
|
// 箭头语法砍掉之后正文原样就是正文,不用再切了 —— 但扫边那一步仍然要跳围栏,
|
|
351
332
|
// 见上面的 outsideFences()。
|
|
352
|
-
const
|
|
333
|
+
const source = e.body.join('\n');
|
|
334
|
+
const md = source.replace(/^\s*\n+/, '').replace(/\s+$/, '');
|
|
335
|
+
const links = markdownLinks(source);
|
|
336
|
+
const mediaLocations = mediaLinkLocations(source, links);
|
|
353
337
|
|
|
354
338
|
// 正文里的内联链接:能对上的记进 targets(客户端渲染完 marked 后靠它换 a.edge)。
|
|
355
339
|
// ★ 对不上**不算错** —— `[文字](目标)` 本来就是普通 Markdown 链接,
|
|
@@ -357,15 +341,13 @@ export function parseDocument(text, opts = {}) {
|
|
|
357
341
|
// ★ **逐行**扫:链接不会跨行,这样每条都能报出准确的行号。
|
|
358
342
|
// ★ 围栏里的跳过 —— 那是代码样例,不是边:写一篇「怎么用 clado」的文档时,
|
|
359
343
|
// 围栏里示范用的 `[文字](目标)` 不该在图上长出一条线来。
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
while ((m = inlineRe.exec(line))) {
|
|
368
|
-
const raw = m[1] || m[2]; // m[1] 是 `<带空格>` 那一支,m[2] 是普通目标
|
|
344
|
+
for (const link of links) {
|
|
345
|
+
const lineNo = e.line + source.slice(0, link.start).split('\n').length;
|
|
346
|
+
if (link.title?.startsWith('clado:') && !mediaLocations.get(link.start)?.valid) {
|
|
347
|
+
at('error', 'MD_MEDIA_LINK_SOURCE', '连线起点未匹配到对应代码行、表格行或 Mermaid 节点。', '用 MCP 重新定位到正确的内容块。', lineNo);
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
const raw = link.target;
|
|
369
351
|
// ★ `[文字](#标题)` 是最自然的「跳到那张卡」写法,但我们的文档里标题**并没有 id**
|
|
370
352
|
// (doc 模式的 section 挂的是 data-id),所以原样放着它在任何模式下都是个死链。
|
|
371
353
|
// 把开头的 `#` 剥掉再查一次表:能对上就是一条边(点一下会闪目标卡),
|
|
@@ -373,7 +355,9 @@ export function parseDocument(text, opts = {}) {
|
|
|
373
355
|
// 注意 `#/foo`、`#!foo` 是前端路由写法,不剥。
|
|
374
356
|
const t = (raw[0] === '#' && raw.length > 1 && raw[1] !== '/' && raw[1] !== '!')
|
|
375
357
|
? raw.slice(1) : raw;
|
|
376
|
-
|
|
358
|
+
// 同一个目标可由多张卡引用;targets 是查找表,不能用它跳过
|
|
359
|
+
// 后续来源的边,否则第二张调用卡会被误判为孤立。
|
|
360
|
+
if (EXTERNAL.test(t)) continue;
|
|
377
361
|
/*
|
|
378
362
|
* ★ `目标#锚点`:`#` 后面那一截是**目标卡里的某个下层标题**。
|
|
379
363
|
* 只拿 `#` 前面那截去查卡片表,锚点单独记一份给客户端。
|
|
@@ -404,7 +388,6 @@ export function parseDocument(text, opts = {}) {
|
|
|
404
388
|
if (got) { head = t; anchor = ''; }
|
|
405
389
|
}
|
|
406
390
|
if (!got) continue;
|
|
407
|
-
const lineNo = e.line + 1 + i;
|
|
408
391
|
if (got.ambiguous) {
|
|
409
392
|
at('error', 'MD_EDGE_AMBIGUOUS',
|
|
410
393
|
`「${t}」能对上 ${got.ambiguous.length} 个地方:${got.ambiguous.join('、')}`,
|
|
@@ -414,12 +397,12 @@ export function parseDocument(text, opts = {}) {
|
|
|
414
397
|
}
|
|
415
398
|
const mine = e.level === 1 ? card.id : node.id;
|
|
416
399
|
const theirs = cardOf.get(got.id) || got.id;
|
|
417
|
-
if (
|
|
400
|
+
if (got.id === mine && !anchor) continue;
|
|
418
401
|
// 键用 raw —— 客户端是拿**渲染出来的 href** 去查表的,剥过 `#` 就查不到了
|
|
419
402
|
targets[raw] = theirs;
|
|
403
|
+
if (!anchor && got.id !== theirs) { anchor = titleOf.get(got.id); targetNodes[raw] = got.id; }
|
|
420
404
|
if (anchor) anchors[raw] = anchor;
|
|
421
405
|
edgePairs.push([mine, theirs]);
|
|
422
|
-
}
|
|
423
406
|
}
|
|
424
407
|
|
|
425
408
|
if (!md) return; // 光杆标题,没内容也没边
|
|
@@ -461,6 +444,20 @@ export function parseDocument(text, opts = {}) {
|
|
|
461
444
|
cardEdges.push([ca, cb]);
|
|
462
445
|
}
|
|
463
446
|
|
|
447
|
+
// 交付时检查卡片是否参与真实的卡片级关系。卡内父子、卡内引用和
|
|
448
|
+
// 自连都不算连接另一张卡;入边和出边任意一条即可,不要求全图连通。
|
|
449
|
+
// 单卡画布豁免。编辑过程允许暂存尚未连接的卡片,由 CLI build 显式启用。
|
|
450
|
+
if (opts.requireCardConnections && cards.length > 1) {
|
|
451
|
+
const connected = new Set(cardEdges.flat());
|
|
452
|
+
for (const card of cards) {
|
|
453
|
+
if (connected.has(card.id)) continue;
|
|
454
|
+
at('error', 'MD_CARD_ISOLATED',
|
|
455
|
+
`卡片「${card.title}」没有连接任何其他卡片`,
|
|
456
|
+
'在本卡或另一张卡的正文中添加指向对方的有效内链;自连、卡内引用、网址和跨 Markdown 跳转不计入卡片连线',
|
|
457
|
+
card._entry.line);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
464
461
|
const title = opts.title || fm.meta.title || (cleanCards[0] && cleanCards[0].title) || opts.fileBase || '';
|
|
465
462
|
if (!fm.meta.title && !opts.title) {
|
|
466
463
|
at('info', 'MD_NO_PREAMBLE', '没有 front matter 的 `title`,文档标题用第一个 `#` 顶上');
|
|
@@ -468,10 +465,12 @@ export function parseDocument(text, opts = {}) {
|
|
|
468
465
|
|
|
469
466
|
const doc = {
|
|
470
467
|
title,
|
|
468
|
+
meta: fm.meta,
|
|
471
469
|
preamble: preambleLines.join('\n').replace(/^\s*\n+/, '').replace(/\s+$/, ''),
|
|
472
470
|
cards: cleanCards,
|
|
473
471
|
targets: { ...targets },
|
|
474
472
|
anchors: { ...anchors },
|
|
473
|
+
targetNodes: { ...targetNodes },
|
|
475
474
|
edges: cardEdges,
|
|
476
475
|
hasMermaid,
|
|
477
476
|
};
|