@fengwk/kchart 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/NOTICE.md +11 -0
- package/README.md +68 -2
- package/README.zh-CN.md +69 -0
- package/dist/LICENSE +21 -0
- package/dist/NOTICE.md +11 -0
- package/dist/assets/ICONS-LICENSE.txt +53 -0
- package/dist/assets/maps/LICENSE.md +75 -0
- package/dist/assets/maps/METADATA.md +9 -0
- package/dist/assets/maps/REGIONS.md +28 -0
- package/dist/assets/maps/us-states.geojson +1 -0
- package/dist/assets/maps/world.geojson +1 -0
- package/dist/esm/_virtual/_kchart-families.js +48 -0
- package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
- package/dist/esm/assets/icons.js +11 -0
- package/dist/esm/assets/maps/us-states.js +3 -0
- package/dist/esm/assets/maps/world.js +3 -0
- package/dist/esm/assets/themes/claude.js +69 -0
- package/dist/esm/assets/themes/cloudmusic.js +62 -0
- package/dist/esm/assets/themes/kanagawa.js +67 -0
- package/dist/esm/assets/themes/obsidian.js +67 -0
- package/dist/esm/assets/themes/paper.js +61 -0
- package/dist/esm/assets/themes/terminal.js +60 -0
- package/dist/esm/assets/themes/tokyo.js +62 -0
- package/dist/esm/chart/basic.js +1313 -0
- package/dist/esm/chart/catalog.js +476 -0
- package/dist/esm/chart/composition.js +844 -0
- package/dist/esm/chart/context.js +189 -0
- package/dist/esm/chart/distribution.js +442 -0
- package/dist/esm/chart/fields.js +67 -0
- package/dist/esm/chart/maps.js +563 -0
- package/dist/esm/chart/mindmap.js +629 -0
- package/dist/esm/chart/multivariate.js +1229 -0
- package/dist/esm/chart/registry.js +108 -0
- package/dist/esm/chart/relations.js +949 -0
- package/dist/esm/chart/spec.js +1415 -0
- package/dist/esm/chart/stat-compare.js +302 -0
- package/dist/esm/chart/stat-trend.js +181 -0
- package/dist/esm/chart/statistics.js +1007 -0
- package/dist/esm/chart/unit-variants.js +645 -0
- package/dist/esm/chart/variants.js +552 -0
- package/dist/esm/core/color.js +197 -0
- package/dist/esm/core/context.js +256 -0
- package/dist/esm/core/diagnostics.js +23 -0
- package/dist/esm/core/format.js +167 -0
- package/dist/esm/core/geometry.js +130 -0
- package/dist/esm/core/icon-data.js +5 -0
- package/dist/esm/core/icons.js +173 -0
- package/dist/esm/core/scale.js +112 -0
- package/dist/esm/core/scene.js +234 -0
- package/dist/esm/core/svg.js +89 -0
- package/dist/esm/core/text.js +184 -0
- package/dist/esm/core/theme.js +318 -0
- package/dist/esm/diagram/agentflow.js +596 -0
- package/dist/esm/diagram/architecture.js +729 -0
- package/dist/esm/diagram/block.js +988 -0
- package/dist/esm/diagram/blocks.js +687 -0
- package/dist/esm/diagram/c4.js +1002 -0
- package/dist/esm/diagram/class.js +1158 -0
- package/dist/esm/diagram/cynefin.js +749 -0
- package/dist/esm/diagram/emphasis.js +7 -0
- package/dist/esm/diagram/er.js +657 -0
- package/dist/esm/diagram/eventmodeling.js +948 -0
- package/dist/esm/diagram/gantt-date.js +323 -0
- package/dist/esm/diagram/gantt.js +661 -0
- package/dist/esm/diagram/gitgraph.js +472 -0
- package/dist/esm/diagram/graph-layout.js +4046 -0
- package/dist/esm/diagram/graph-parse.js +1366 -0
- package/dist/esm/diagram/graph-route.js +490 -0
- package/dist/esm/diagram/graph.js +553 -0
- package/dist/esm/diagram/ishikawa.js +496 -0
- package/dist/esm/diagram/journey.js +512 -0
- package/dist/esm/diagram/kanban.js +280 -0
- package/dist/esm/diagram/label.js +56 -0
- package/dist/esm/diagram/layers.js +385 -0
- package/dist/esm/diagram/map-label.js +44 -0
- package/dist/esm/diagram/mindmap.js +277 -0
- package/dist/esm/diagram/numeric-composition.js +78 -0
- package/dist/esm/diagram/numeric-model.js +86 -0
- package/dist/esm/diagram/numeric-parse.js +205 -0
- package/dist/esm/diagram/numeric-radar.js +355 -0
- package/dist/esm/diagram/numeric-sankey.js +201 -0
- package/dist/esm/diagram/numeric-xy.js +422 -0
- package/dist/esm/diagram/packet.js +175 -0
- package/dist/esm/diagram/presentation.js +252 -0
- package/dist/esm/diagram/quadrant.js +297 -0
- package/dist/esm/diagram/railroad-parse.js +465 -0
- package/dist/esm/diagram/railroad.js +294 -0
- package/dist/esm/diagram/registry.js +296 -0
- package/dist/esm/diagram/relations.js +966 -0
- package/dist/esm/diagram/requirement.js +341 -0
- package/dist/esm/diagram/sequence.js +1228 -0
- package/dist/esm/diagram/shapes.js +366 -0
- package/dist/esm/diagram/source.js +538 -0
- package/dist/esm/diagram/support.js +153 -0
- package/dist/esm/diagram/swimlane-mermaid.js +140 -0
- package/dist/esm/diagram/swimlane.js +1186 -0
- package/dist/esm/diagram/time.js +748 -0
- package/dist/esm/diagram/timeline.js +376 -0
- package/dist/esm/diagram/tree-family-icon.js +22 -0
- package/dist/esm/diagram/treeview.js +293 -0
- package/dist/esm/diagram/usecase.js +852 -0
- package/dist/esm/diagram/venn.js +1038 -0
- package/dist/esm/diagram/wardley.js +838 -0
- package/dist/esm/index.d.ts +133 -0
- package/dist/esm/index.js +92 -0
- package/dist/esm/interaction.js +561 -0
- package/dist/esm/mermaid.d.ts +28 -0
- package/dist/esm/mermaid.js +70 -0
- package/dist/esm/node.d.ts +68 -0
- package/dist/esm/node.js +373 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/page-api.js +65 -0
- package/dist/esm/runtime.js +352 -0
- package/dist/esm/src/families.js +209 -0
- package/dist/page-api.js +1 -0
- package/dist/units/chart/basic.js +1 -0
- package/dist/units/chart/catalog.js +1 -0
- package/dist/units/chart/composition.js +1 -0
- package/dist/units/chart/context.js +1 -0
- package/dist/units/chart/distribution.js +1 -0
- package/dist/units/chart/fields.js +1 -0
- package/dist/units/chart/maps.js +1 -0
- package/dist/units/chart/mindmap.js +1 -0
- package/dist/units/chart/multivariate.js +1 -0
- package/dist/units/chart/registry.js +1 -0
- package/dist/units/chart/relations.js +1 -0
- package/dist/units/chart/spec.js +3 -0
- package/dist/units/chart/stat-compare.js +1 -0
- package/dist/units/chart/stat-trend.js +1 -0
- package/dist/units/chart/statistics.js +1 -0
- package/dist/units/chart/unit-variants.js +1 -0
- package/dist/units/chart/variants.js +1 -0
- package/dist/units/core/color.js +1 -0
- package/dist/units/core/context.js +1 -0
- package/dist/units/core/diagnostics.js +1 -0
- package/dist/units/core/format.js +1 -0
- package/dist/units/core/geometry.js +1 -0
- package/dist/units/core/icon-data.js +1 -0
- package/dist/units/core/icons.js +1 -0
- package/dist/units/core/scale.js +1 -0
- package/dist/units/core/scene.js +1 -0
- package/dist/units/core/svg.js +1 -0
- package/dist/units/core/text.js +2 -0
- package/dist/units/core/theme.js +1 -0
- package/dist/units/diagram/agentflow.js +12 -0
- package/dist/units/diagram/architecture.js +4 -0
- package/dist/units/diagram/block.js +2 -0
- package/dist/units/diagram/blocks.js +2 -0
- package/dist/units/diagram/c4.js +6 -0
- package/dist/units/diagram/class.js +5 -0
- package/dist/units/diagram/cynefin.js +4 -0
- package/dist/units/diagram/emphasis.js +1 -0
- package/dist/units/diagram/er.js +9 -0
- package/dist/units/diagram/eventmodeling.js +5 -0
- package/dist/units/diagram/gantt-date.js +1 -0
- package/dist/units/diagram/gantt.js +3 -0
- package/dist/units/diagram/gitgraph.js +2 -0
- package/dist/units/diagram/graph-layout.js +4 -0
- package/dist/units/diagram/graph-parse.js +9 -0
- package/dist/units/diagram/graph-route.js +1 -0
- package/dist/units/diagram/graph.js +1 -0
- package/dist/units/diagram/ishikawa.js +2 -0
- package/dist/units/diagram/journey.js +5 -0
- package/dist/units/diagram/kanban.js +2 -0
- package/dist/units/diagram/label.js +4 -0
- package/dist/units/diagram/layers.js +1 -0
- package/dist/units/diagram/map-label.js +1 -0
- package/dist/units/diagram/mindmap.js +2 -0
- package/dist/units/diagram/numeric-composition.js +1 -0
- package/dist/units/diagram/numeric-model.js +1 -0
- package/dist/units/diagram/numeric-parse.js +2 -0
- package/dist/units/diagram/numeric-radar.js +3 -0
- package/dist/units/diagram/numeric-sankey.js +2 -0
- package/dist/units/diagram/numeric-xy.js +2 -0
- package/dist/units/diagram/packet.js +2 -0
- package/dist/units/diagram/presentation.js +2 -0
- package/dist/units/diagram/quadrant.js +2 -0
- package/dist/units/diagram/railroad-parse.js +4 -0
- package/dist/units/diagram/railroad.js +1 -0
- package/dist/units/diagram/registry.js +2 -0
- package/dist/units/diagram/relations.js +3 -0
- package/dist/units/diagram/requirement.js +3 -0
- package/dist/units/diagram/sequence.js +2 -0
- package/dist/units/diagram/shapes.js +60 -0
- package/dist/units/diagram/source.js +20 -0
- package/dist/units/diagram/support.js +3 -0
- package/dist/units/diagram/swimlane-mermaid.js +3 -0
- package/dist/units/diagram/swimlane.js +2 -0
- package/dist/units/diagram/time.js +3 -0
- package/dist/units/diagram/timeline.js +2 -0
- package/dist/units/diagram/tree-family-icon.js +1 -0
- package/dist/units/diagram/treeview.js +2 -0
- package/dist/units/diagram/usecase.js +3 -0
- package/dist/units/diagram/venn.js +2 -0
- package/dist/units/diagram/wardley.js +4 -0
- package/dist/units/interaction.js +1 -0
- package/dist/units/runtime.js +1 -0
- package/dist/units.json +712 -0
- package/package.json +30 -4
|
@@ -0,0 +1,1366 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { statusMarks } from "../core/theme.js";
|
|
3
|
+
import { DIRECTIONS, labelText, warning } from "./support.js";
|
|
4
|
+
import { shapeName } from "./shapes.js";
|
|
5
|
+
import { presentation } from "./presentation.js";
|
|
6
|
+
import { normalizeLabel as normalizeLabel$1 } from "./label.js";
|
|
7
|
+
import { resolveIconRef } from "../core/icons.js";
|
|
8
|
+
// src/diagram/graph-parse.js
|
|
9
|
+
var graph_parse_exports = /* @__PURE__ */ __exportAll({
|
|
10
|
+
parseFlow: () => parseFlow,
|
|
11
|
+
parseGraph: () => parseGraph
|
|
12
|
+
});
|
|
13
|
+
var LIMITS = {
|
|
14
|
+
nodes: 30,
|
|
15
|
+
edges: 50,
|
|
16
|
+
subgraphDepth: 3,
|
|
17
|
+
stateDepth: 3
|
|
18
|
+
};
|
|
19
|
+
var MERMAID_LIMITS = {
|
|
20
|
+
...LIMITS,
|
|
21
|
+
nodes: 300,
|
|
22
|
+
edges: 420
|
|
23
|
+
};
|
|
24
|
+
var CLASSES = Object.keys(statusMarks);
|
|
25
|
+
var stripQuotes = (raw) => {
|
|
26
|
+
let text = String(raw ?? "").trim();
|
|
27
|
+
if (text.length >= 2 && (text[0] === "\"" && text.endsWith("\"") || text[0] === "'" && text.endsWith("'"))) text = text.slice(1, -1);
|
|
28
|
+
return text.replace(/\\"/g, "\"").replace(/\\'/g, "'");
|
|
29
|
+
};
|
|
30
|
+
function normalizeLabel(raw) {
|
|
31
|
+
return labelText(raw).trim();
|
|
32
|
+
}
|
|
33
|
+
function splitStatements(text, flow = false) {
|
|
34
|
+
const parts = [];
|
|
35
|
+
let depth = 0;
|
|
36
|
+
let quote = false;
|
|
37
|
+
let pipe = false;
|
|
38
|
+
let start = 0;
|
|
39
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
40
|
+
const ch = text[i];
|
|
41
|
+
if (ch === "\"" && text[i - 1] !== "\\") {
|
|
42
|
+
quote = !quote;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (quote) continue;
|
|
46
|
+
if (flow && ch === "|" && depth === 0) pipe = !pipe;
|
|
47
|
+
if (pipe) continue;
|
|
48
|
+
if ("[({".includes(ch) || flow && ch === ">" && /[^\s\-=]/.test(text[i - 1] ?? "") && text.indexOf("]", i) !== -1) depth += 1;
|
|
49
|
+
else if ("]})".includes(ch)) depth = Math.max(0, depth - 1);
|
|
50
|
+
else if (ch === ";" && depth === 0) {
|
|
51
|
+
parts.push(text.slice(start, i));
|
|
52
|
+
start = i + 1;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
parts.push(text.slice(start));
|
|
56
|
+
return parts;
|
|
57
|
+
}
|
|
58
|
+
function parseDirection(token) {
|
|
59
|
+
const upper = String(token || "").toUpperCase();
|
|
60
|
+
if (upper === "TD") return { direction: "TB" };
|
|
61
|
+
if (DIRECTIONS.includes(upper)) return { direction: upper };
|
|
62
|
+
return { error: `不支持的方向:${token}` };
|
|
63
|
+
}
|
|
64
|
+
function findClose(text, start, close) {
|
|
65
|
+
let quote = null;
|
|
66
|
+
for (let i = start; i < text.length; i += 1) {
|
|
67
|
+
const ch = text[i];
|
|
68
|
+
if (quote) {
|
|
69
|
+
if (ch === quote && text[i - 1] !== "\\") quote = null;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (ch === "\"" || ch === "'" && (i === start || /[\s:]/.test(text[i - 1]))) {
|
|
73
|
+
quote = ch;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (text.startsWith(close, i)) return i;
|
|
77
|
+
}
|
|
78
|
+
return -1;
|
|
79
|
+
}
|
|
80
|
+
function parseShapeAt(text, index, normalizeLabel) {
|
|
81
|
+
const read = (openLen, close, shape, lean = false) => {
|
|
82
|
+
const end = findClose(text, index + openLen, close);
|
|
83
|
+
if (end < 0) return {
|
|
84
|
+
error: "unterminated",
|
|
85
|
+
end: text.length
|
|
86
|
+
};
|
|
87
|
+
let raw = text.slice(index + openLen, end);
|
|
88
|
+
if (lean) {
|
|
89
|
+
shape = {
|
|
90
|
+
"//": "lean-r",
|
|
91
|
+
"\\\\": "lean-l",
|
|
92
|
+
"/\\": "trap-b",
|
|
93
|
+
"\\/": "trap-t"
|
|
94
|
+
}[raw[0] + raw.at(-1)];
|
|
95
|
+
if (!shape) return {
|
|
96
|
+
error: "unterminated",
|
|
97
|
+
end: end + 1
|
|
98
|
+
};
|
|
99
|
+
raw = raw.slice(1, -1);
|
|
100
|
+
}
|
|
101
|
+
const props = shape === "rect" && /^\|[^|]*\|/.exec(raw);
|
|
102
|
+
if (props) raw = raw.slice(props[0].length);
|
|
103
|
+
return {
|
|
104
|
+
shape,
|
|
105
|
+
props: Boolean(props),
|
|
106
|
+
label: normalizeLabel(stripQuotes(raw)),
|
|
107
|
+
end: end + close.length
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
const ch = text[index];
|
|
111
|
+
if (ch === "[") {
|
|
112
|
+
if (text[index + 1] === "[") return read(2, "]]", "subroutine");
|
|
113
|
+
if (text[index + 1] === "(") return read(2, ")]", "cylinder");
|
|
114
|
+
if (text[index + 1] === "/" || text[index + 1] === "\\") return read(1, "]", "rect", true);
|
|
115
|
+
return read(1, "]", "rect");
|
|
116
|
+
}
|
|
117
|
+
if (ch === "(") {
|
|
118
|
+
if (text.startsWith("(((", index)) return read(3, ")))", "dbl-circ");
|
|
119
|
+
if (text.startsWith("(-", index)) return read(2, "-)", "circle");
|
|
120
|
+
if (text[index + 1] === "(") return read(2, "))", "circle");
|
|
121
|
+
if (text[index + 1] === "[") return read(2, "])", "stadium");
|
|
122
|
+
return read(1, ")", "round");
|
|
123
|
+
}
|
|
124
|
+
if (ch === "{") {
|
|
125
|
+
if (text[index + 1] === "{") return read(2, "}}", "hexagon");
|
|
126
|
+
return read(1, "}", "diamond");
|
|
127
|
+
}
|
|
128
|
+
if (ch === ">") return read(1, "]", "odd");
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
function parseFlowNodeToken(text, start, normalizeLabel) {
|
|
132
|
+
let i = start;
|
|
133
|
+
while (i < text.length && /\s/.test(text[i])) i += 1;
|
|
134
|
+
if (i >= text.length) return null;
|
|
135
|
+
if (text.startsWith("@{", i)) return {
|
|
136
|
+
error: "shape",
|
|
137
|
+
end: i + 2
|
|
138
|
+
};
|
|
139
|
+
if (text[i] === "&") return null;
|
|
140
|
+
const idStart = i;
|
|
141
|
+
while (i < text.length) {
|
|
142
|
+
const ch = text[i];
|
|
143
|
+
if (/\s/.test(ch)) break;
|
|
144
|
+
if ("[](){}|;<>@".includes(ch) || text.startsWith(":::", i)) break;
|
|
145
|
+
if (/^(?:--|==|-\.|~~~)/.test(text.slice(i)) || i > idStart && /^[ox](?:--|==|-\.)/.test(text.slice(i))) break;
|
|
146
|
+
i += 1;
|
|
147
|
+
}
|
|
148
|
+
const id = text.slice(idStart, i);
|
|
149
|
+
if (!id) return null;
|
|
150
|
+
if (text.startsWith("@{", i)) {
|
|
151
|
+
const close = findClose(text, i + 2, "}");
|
|
152
|
+
if (close < 0) return {
|
|
153
|
+
id,
|
|
154
|
+
error: "unterminated",
|
|
155
|
+
end: text.length
|
|
156
|
+
};
|
|
157
|
+
const metadata = Object.create(null);
|
|
158
|
+
const payload = text.slice(i + 2, close);
|
|
159
|
+
const fields = /\s*([\w-]+)\s*:\s*(\|[^\n]*\n[\s\S]*?(?=,\s*[\w-]+\s*:|$)|"(?:\\.|[^"\\])*"|'(?:''|[^'])*'|[^,]*?)\s*(?:,|$)/gy;
|
|
160
|
+
while (fields.lastIndex < payload.length && payload.slice(fields.lastIndex).trim()) {
|
|
161
|
+
const field = fields.exec(payload);
|
|
162
|
+
if (!field) return {
|
|
163
|
+
id,
|
|
164
|
+
error: "metadata",
|
|
165
|
+
end: close + 1
|
|
166
|
+
};
|
|
167
|
+
const raw = field[2];
|
|
168
|
+
metadata[field[1]] = /^\|\s*\n/.test(raw) ? raw.replace(/^\|[^\n]*\n/, "").split("\n").map((line) => line.trim()).join("\n") : stripQuotes(raw).replace(/''/g, "'");
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
id,
|
|
172
|
+
metadata,
|
|
173
|
+
end: close + 1
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
let shape = null;
|
|
177
|
+
let label = null;
|
|
178
|
+
let error = null;
|
|
179
|
+
const shapePart = parseShapeAt(text, i, normalizeLabel);
|
|
180
|
+
if (shapePart) {
|
|
181
|
+
if (shapePart.error) error = shapePart.error;
|
|
182
|
+
else {
|
|
183
|
+
shape = shapePart.shape;
|
|
184
|
+
label = shapePart.label;
|
|
185
|
+
}
|
|
186
|
+
i = shapePart.end;
|
|
187
|
+
}
|
|
188
|
+
let cls = null;
|
|
189
|
+
if (text.startsWith(":::", i)) {
|
|
190
|
+
i += 3;
|
|
191
|
+
const classStart = i;
|
|
192
|
+
while (i < text.length && !/[\s&;]/.test(text[i])) i += 1;
|
|
193
|
+
cls = text.slice(classStart, i);
|
|
194
|
+
if (!cls) error = "class-empty";
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
id,
|
|
198
|
+
shape,
|
|
199
|
+
label,
|
|
200
|
+
cls,
|
|
201
|
+
props: shapePart?.props,
|
|
202
|
+
error,
|
|
203
|
+
end: i
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function parseFlowGroup(text, start, normalizeLabel) {
|
|
207
|
+
const tokens = [];
|
|
208
|
+
let i = start;
|
|
209
|
+
for (;;) {
|
|
210
|
+
while (i < text.length && /\s/.test(text[i])) i += 1;
|
|
211
|
+
if (i >= text.length) break;
|
|
212
|
+
const token = parseFlowNodeToken(text, i, normalizeLabel);
|
|
213
|
+
if (!token) return null;
|
|
214
|
+
tokens.push(token);
|
|
215
|
+
i = token.end;
|
|
216
|
+
while (i < text.length && /\s/.test(text[i])) i += 1;
|
|
217
|
+
if (text[i] === "&") {
|
|
218
|
+
i += 1;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
return tokens.length ? {
|
|
224
|
+
tokens,
|
|
225
|
+
end: i
|
|
226
|
+
} : null;
|
|
227
|
+
}
|
|
228
|
+
var marks = {
|
|
229
|
+
"<": "point",
|
|
230
|
+
">": "point",
|
|
231
|
+
o: "circle",
|
|
232
|
+
x: "cross"
|
|
233
|
+
};
|
|
234
|
+
var linkToken = /^([<ox]?)(-{2,}|={2,}|-?\.+-|~{3,})([>ox]?)/;
|
|
235
|
+
var labelToken = /"(?:\\.|[^"\\])*"|[^|]*/y;
|
|
236
|
+
function parseOperator(text, start, normalizeLabel) {
|
|
237
|
+
const identifier = /^[^\s"\[\](){}|@]+@(?=[<xo.\-=~])/.exec(text.slice(start));
|
|
238
|
+
const id = identifier?.[0].slice(0, -1) ?? null;
|
|
239
|
+
const offset = start + (identifier?.[0].length ?? 0);
|
|
240
|
+
const s = text.slice(offset);
|
|
241
|
+
const match = linkToken.exec(s);
|
|
242
|
+
const opener = /^([<ox]?)(--|==|-\.)(?![-=.>])/.exec(s);
|
|
243
|
+
if (!match && !opener) return null;
|
|
244
|
+
let [, prefix, body, suffix = ""] = match || opener;
|
|
245
|
+
let rawLabel = null, end = offset + (match || opener)[0].length;
|
|
246
|
+
const style = body[0] === "~" ? "invisible" : body.includes(".") ? "dashed" : body[0] === "=" ? "bold" : "solid";
|
|
247
|
+
if (opener && !s.slice(opener[0].length).trimStart().startsWith("|") && (!suffix || /[ox]/.test(suffix))) {
|
|
248
|
+
const tail = s.slice(opener[0].length);
|
|
249
|
+
const closing = style === "dashed" ? /\.+-[>ox]?/ : style === "bold" ? /={2,}[>ox]?/ : /-{2,}[>ox]?/;
|
|
250
|
+
const label = /^\s*("(?:\\.|[^"\\])*"|[^"[\]{}|]+?)(?=\s*(?:--|==|\.+-))/.exec(tail);
|
|
251
|
+
const close = label && new RegExp(`^\\s*(${closing.source})`).exec(tail.slice(label[0].length));
|
|
252
|
+
if (close && label[1].trim()) {
|
|
253
|
+
rawLabel = label[1];
|
|
254
|
+
prefix = opener[1];
|
|
255
|
+
[, , body, suffix] = linkToken.exec(close[1]);
|
|
256
|
+
if (prefix && marks[prefix] !== marks[suffix]) return null;
|
|
257
|
+
end = offset + opener[0].length + label[0].length + close[0].length;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (!match && rawLabel == null) return null;
|
|
261
|
+
const pipeStart = end + /^\s*/.exec(text.slice(end))[0].length;
|
|
262
|
+
if (text[pipeStart] === "|" && rawLabel == null) {
|
|
263
|
+
labelToken.lastIndex = pipeStart + 1;
|
|
264
|
+
const label = labelToken.exec(text);
|
|
265
|
+
if (text[labelToken.lastIndex] !== "|") return null;
|
|
266
|
+
rawLabel = label[0];
|
|
267
|
+
end = labelToken.lastIndex + 1;
|
|
268
|
+
}
|
|
269
|
+
const endMark = style === "invisible" ? "none" : marks[suffix] || "none";
|
|
270
|
+
const startMark = marks[prefix] === endMark ? endMark : "none";
|
|
271
|
+
const len = style === "dashed" ? body.split(".").length - 1 : body.length - 1 - (endMark === "none" ? 1 : 0) + (prefix && startMark === "none" ? 1 : 0);
|
|
272
|
+
return {
|
|
273
|
+
id,
|
|
274
|
+
style,
|
|
275
|
+
startMark,
|
|
276
|
+
endMark,
|
|
277
|
+
startArrow: startMark !== "none",
|
|
278
|
+
endArrow: endMark !== "none",
|
|
279
|
+
extra: Math.max(0, Math.min(10, len) - 1),
|
|
280
|
+
label: rawLabel == null ? null : normalizeLabel(stripQuotes(rawLabel)),
|
|
281
|
+
end
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
function parseSubgraphDecl(rest, anonymousId, normalizeLabel) {
|
|
285
|
+
const text = rest.trim();
|
|
286
|
+
let m = /^([^\s[\]]+)\s*\[([\s\S]*)\]\s*$/.exec(text);
|
|
287
|
+
if (m) return {
|
|
288
|
+
id: m[1],
|
|
289
|
+
title: normalizeLabel(stripQuotes(m[2])) || m[1]
|
|
290
|
+
};
|
|
291
|
+
if (/^(["'])/.test(text) && /["']$/.test(text)) return {
|
|
292
|
+
id: anonymousId,
|
|
293
|
+
title: normalizeLabel(stripQuotes(text))
|
|
294
|
+
};
|
|
295
|
+
return {
|
|
296
|
+
id: text && !/\s/.test(text) ? text : anonymousId,
|
|
297
|
+
title: normalizeLabel(text)
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
function parseFlowchartBody(statements, errors, direction = "TB", lang, look) {
|
|
301
|
+
const limits = look ? MERMAID_LIMITS : LIMITS;
|
|
302
|
+
const cleanLabel = look ? (value) => String(value).trim() : normalizeLabel;
|
|
303
|
+
const model = {
|
|
304
|
+
type: "flowchart",
|
|
305
|
+
direction: "TB",
|
|
306
|
+
nodes: [],
|
|
307
|
+
edges: [],
|
|
308
|
+
subgraphs: []
|
|
309
|
+
};
|
|
310
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
311
|
+
const subgraphs = /* @__PURE__ */ new Map();
|
|
312
|
+
const stack = [];
|
|
313
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
314
|
+
const knownFrames = /* @__PURE__ */ new Set();
|
|
315
|
+
for (const st of statements) {
|
|
316
|
+
const m = /^subgraph\b\s*(.*)$/.exec(st.text);
|
|
317
|
+
if (!m) continue;
|
|
318
|
+
const decl = parseSubgraphDecl(m[1], `subGraph${declarations.size}`, cleanLabel);
|
|
319
|
+
declarations.set(st, decl);
|
|
320
|
+
knownFrames.add(decl.id);
|
|
321
|
+
}
|
|
322
|
+
const frameMetadata = /* @__PURE__ */ new Map();
|
|
323
|
+
let seq = 0;
|
|
324
|
+
let edgeSeq = 0;
|
|
325
|
+
let nodeLimit = false;
|
|
326
|
+
let edgeLimit = false;
|
|
327
|
+
const currentParent = () => stack.length ? stack[stack.length - 1].id : null;
|
|
328
|
+
const addError = (line, message, fix) => errors.push({
|
|
329
|
+
line,
|
|
330
|
+
message,
|
|
331
|
+
fix
|
|
332
|
+
});
|
|
333
|
+
const checkClass = (name, line) => {
|
|
334
|
+
if (!CLASSES.includes(name)) {
|
|
335
|
+
addError(line, `未知分类:${name}`, `可用分类:${CLASSES.join("、")}`);
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
return name;
|
|
339
|
+
};
|
|
340
|
+
const warned = /* @__PURE__ */ new Set();
|
|
341
|
+
const resolveToken = (token, line) => {
|
|
342
|
+
if (token.error === "shape" || token.error === "metadata") {
|
|
343
|
+
addError(line, "无法解析形状属性", "使用 A@{ shape: rect, label: \"文字\" }");
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
if (token.metadata && knownFrames.has(token.id) && "view" in token.metadata) {
|
|
347
|
+
frameMetadata.set(token.id, token.metadata.view);
|
|
348
|
+
return {
|
|
349
|
+
id: token.id,
|
|
350
|
+
frame: true
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
if (token.metadata) {
|
|
354
|
+
const { shape, label, ...other } = token.metadata;
|
|
355
|
+
if (shape !== void 0) {
|
|
356
|
+
token.shape = shapeName(shape);
|
|
357
|
+
if (!token.shape) {
|
|
358
|
+
addError(line, `No such shape: ${shape}`, "使用 Mermaid 的小写形状名称");
|
|
359
|
+
return null;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (label !== void 0) token.label = cleanLabel(label);
|
|
363
|
+
if (look && "icon" in other) {
|
|
364
|
+
token.iconRef = other.icon;
|
|
365
|
+
if (!token.shape) token.shape = "icon";
|
|
366
|
+
token.iconPosition = other.pos;
|
|
367
|
+
token.iconHeight = Number(other.h) > 0 ? Math.min(256, Number(other.h)) : 48;
|
|
368
|
+
token.iconForm = other.form;
|
|
369
|
+
}
|
|
370
|
+
if (("img" in other || !look && "icon" in other) && !warned.has("asset")) {
|
|
371
|
+
warned.add("asset");
|
|
372
|
+
errors.push(warning("IGNORED_ASSET", lang, line, "忽略图标或远程图片", "Ignored icon or remote image"));
|
|
373
|
+
}
|
|
374
|
+
const keys = Object.keys(other).filter((key) => ![
|
|
375
|
+
"img",
|
|
376
|
+
"icon",
|
|
377
|
+
"form",
|
|
378
|
+
"pos",
|
|
379
|
+
"w",
|
|
380
|
+
"h",
|
|
381
|
+
"constraint"
|
|
382
|
+
].includes(key));
|
|
383
|
+
for (const key of keys) if (!warned.has(key)) {
|
|
384
|
+
warned.add(key);
|
|
385
|
+
errors.push(warning("IGNORED_CONFIG", lang, line, `忽略属性 ${key}`, `Ignored property ${key}`));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (token.error === "unterminated") {
|
|
389
|
+
addError(line, `节点 ${token.id} 的形状没有结束的括号`, "补上缺少的 ]、) 或 }");
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
if (look && token.cls) look.addClasses(token.id, token.cls.split(","), line);
|
|
393
|
+
if (token.props && !warned.has("node-props")) {
|
|
394
|
+
warned.add("node-props");
|
|
395
|
+
errors.push(warning("IGNORED_CONFIG", lang, line, "忽略方框内的属性", "Ignored bracketed node properties"));
|
|
396
|
+
}
|
|
397
|
+
if (token.error === "class-empty") addError(line, `节点 ${token.id} 的 ::: 后面缺少分类名`, `可用分类:${CLASSES.join("、")}`);
|
|
398
|
+
if (knownFrames.has(token.id) && !token.shape) return {
|
|
399
|
+
id: token.id,
|
|
400
|
+
frame: true
|
|
401
|
+
};
|
|
402
|
+
let node = nodes.get(token.id);
|
|
403
|
+
if (!node) {
|
|
404
|
+
node = {
|
|
405
|
+
id: token.id,
|
|
406
|
+
label: token.id,
|
|
407
|
+
shape: "rect",
|
|
408
|
+
class: null,
|
|
409
|
+
parent: currentParent(),
|
|
410
|
+
seq: seq++,
|
|
411
|
+
line
|
|
412
|
+
};
|
|
413
|
+
nodes.set(token.id, node);
|
|
414
|
+
model.nodes.push(node);
|
|
415
|
+
if (model.nodes.length === limits.nodes + 1 && !nodeLimit) {
|
|
416
|
+
nodeLimit = true;
|
|
417
|
+
addError(line, `节点 ${model.nodes.length} 个,超过上限 ${limits.nodes}`, "拆成总图和局部图");
|
|
418
|
+
}
|
|
419
|
+
} else if (node.parent == null && currentParent() != null) node.parent = currentParent();
|
|
420
|
+
if (token.shape) node.shape = token.shape;
|
|
421
|
+
if (token.iconRef) {
|
|
422
|
+
node.iconRef = token.iconRef;
|
|
423
|
+
node.iconPosition = token.iconPosition;
|
|
424
|
+
node.iconHeight = token.iconHeight;
|
|
425
|
+
node.iconForm = token.iconForm;
|
|
426
|
+
if (token.label == null && !node.explicitLabel) node.label = "";
|
|
427
|
+
}
|
|
428
|
+
if (token.label != null) {
|
|
429
|
+
node.label = token.label;
|
|
430
|
+
node.explicitLabel = true;
|
|
431
|
+
}
|
|
432
|
+
if (!look && token.cls) {
|
|
433
|
+
const cls = checkClass(token.cls, line);
|
|
434
|
+
if (cls) node.class = cls;
|
|
435
|
+
}
|
|
436
|
+
return {
|
|
437
|
+
id: node.id,
|
|
438
|
+
node
|
|
439
|
+
};
|
|
440
|
+
};
|
|
441
|
+
const edgeIds = /* @__PURE__ */ new Map();
|
|
442
|
+
const edgeMetadata = [];
|
|
443
|
+
const addEdge = (from, to, op, line, requestedId) => {
|
|
444
|
+
let id = requestedId || null;
|
|
445
|
+
if (id && edgeIds.has(id)) {
|
|
446
|
+
let serial = 0;
|
|
447
|
+
do
|
|
448
|
+
id = `L_${from.id}_${to.id}_${serial++}`;
|
|
449
|
+
while (edgeIds.has(id));
|
|
450
|
+
}
|
|
451
|
+
const edge = {
|
|
452
|
+
id,
|
|
453
|
+
seq: edgeSeq++,
|
|
454
|
+
from: from.id,
|
|
455
|
+
to: to.id,
|
|
456
|
+
fromFrame: Boolean(from.frame),
|
|
457
|
+
toFrame: Boolean(to.frame),
|
|
458
|
+
label: op.label,
|
|
459
|
+
style: op.style,
|
|
460
|
+
startArrow: op.startArrow,
|
|
461
|
+
endArrow: op.endArrow,
|
|
462
|
+
startMark: op.startMark,
|
|
463
|
+
endMark: op.endMark,
|
|
464
|
+
extra: op.extra,
|
|
465
|
+
line
|
|
466
|
+
};
|
|
467
|
+
if (id) edgeIds.set(id, edge);
|
|
468
|
+
model.edges.push(edge);
|
|
469
|
+
if (model.edges.length === limits.edges + 1 && !edgeLimit) {
|
|
470
|
+
edgeLimit = true;
|
|
471
|
+
addError(line, `连线 ${model.edges.length} 条,超过上限 ${limits.edges}`, "拆成总图和局部图");
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
let skipDepth = 0;
|
|
475
|
+
let depthLimit = false;
|
|
476
|
+
for (const st of statements.slice(1)) {
|
|
477
|
+
const text = st.text;
|
|
478
|
+
const line = st.line;
|
|
479
|
+
const decl = declarations.get(st);
|
|
480
|
+
if (skipDepth) {
|
|
481
|
+
if (decl) skipDepth += 1;
|
|
482
|
+
else if (text === "end") skipDepth -= 1;
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
if (decl) {
|
|
486
|
+
if (stack.length + 1 > LIMITS.subgraphDepth) {
|
|
487
|
+
if (!depthLimit) addError(line, `子图嵌套 ${stack.length + 1} 层,超过上限 ${LIMITS.subgraphDepth} 层`, "拆成总图和局部图");
|
|
488
|
+
depthLimit = true;
|
|
489
|
+
skipDepth = 1;
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
if (subgraphs.has(decl.id)) {
|
|
493
|
+
stack.push(subgraphs.get(decl.id));
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
const sg = {
|
|
497
|
+
id: decl.id,
|
|
498
|
+
title: decl.title,
|
|
499
|
+
parent: currentParent(),
|
|
500
|
+
depth: stack.length + 1,
|
|
501
|
+
seq: seq++,
|
|
502
|
+
line
|
|
503
|
+
};
|
|
504
|
+
subgraphs.set(sg.id, sg);
|
|
505
|
+
model.subgraphs.push(sg);
|
|
506
|
+
stack.push(sg);
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
if (text === "end") {
|
|
510
|
+
if (!stack.length) {
|
|
511
|
+
addError(line, "end 没有对应的 subgraph", "删除多余的 end");
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
stack.pop();
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
if (/^direction\b/i.test(text)) {
|
|
518
|
+
const token = text.replace(/^direction\b/i, "").trim().split(/\s/)[0];
|
|
519
|
+
const d = parseDirection(token);
|
|
520
|
+
if (d.error) addError(line, d.error, "方向用 TB、TD、BT、LR、RL");
|
|
521
|
+
else if (stack.length) stack.at(-1).dir = d.direction;
|
|
522
|
+
else direction = d.direction;
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
if (look?.accept(text, line)) continue;
|
|
526
|
+
if (/^(classDef|style|linkStyle)\b/i.test(text)) {
|
|
527
|
+
addError(line, `不支持 ${text.split(/\s+/)[0]}`, "颜色由主题决定,删掉这一行;节点分类用 :::new 等");
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
if (/^(click|call)\b/i.test(text)) {
|
|
531
|
+
addError(line, `不支持 ${text.split(/\s+/)[0]}`, "删掉交互语句");
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (/^class\s+/i.test(text)) {
|
|
535
|
+
const m = /^class\s+([^\s]+)\s+([A-Za-z_]\w*)\s*$/i.exec(text);
|
|
536
|
+
if (!m) {
|
|
537
|
+
addError(line, "class 写法不对", "写成 class A,B ok");
|
|
538
|
+
continue;
|
|
539
|
+
}
|
|
540
|
+
const cls = checkClass(m[2], line);
|
|
541
|
+
for (const id of m[1].split(",").map((x) => x.trim()).filter(Boolean)) {
|
|
542
|
+
const node = nodes.get(id);
|
|
543
|
+
if (node) {
|
|
544
|
+
if (cls) node.class = cls;
|
|
545
|
+
} else if (subgraphs.has(id)) {
|
|
546
|
+
if (cls) subgraphs.get(id).class = cls;
|
|
547
|
+
} else {
|
|
548
|
+
const created = resolveToken({ id }, line);
|
|
549
|
+
if (created?.node && cls) created.node.class = cls;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
if (/^(classDiagram|stateDiagram|erDiagram|flowchart|graph)\b/i.test(text)) {
|
|
555
|
+
addError(line, `无法解析的语句:${text}`, "检查 Mermaid 语法;图类型只能声明一次");
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
let group = parseFlowGroup(text, 0, cleanLabel);
|
|
559
|
+
if (!group) {
|
|
560
|
+
addError(line, `无法解析的语句:${text}`, "检查节点 id、形状括号与连线写法");
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
let cursor = group.end;
|
|
564
|
+
const [meta] = group.tokens;
|
|
565
|
+
if (cursor === text.length && group.tokens.length === 1 && meta.metadata && (edgeIds.has(meta.id) || [
|
|
566
|
+
"animate",
|
|
567
|
+
"animation",
|
|
568
|
+
"curve",
|
|
569
|
+
"step"
|
|
570
|
+
].some((key) => key in meta.metadata))) {
|
|
571
|
+
edgeMetadata.push({
|
|
572
|
+
...meta,
|
|
573
|
+
line
|
|
574
|
+
});
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
let left = group.tokens.map((t) => resolveToken(t, line)).filter(Boolean);
|
|
578
|
+
while (cursor < text.length) {
|
|
579
|
+
while (cursor < text.length && /\s/.test(text[cursor])) cursor += 1;
|
|
580
|
+
if (cursor >= text.length) break;
|
|
581
|
+
const op = parseOperator(text, cursor, cleanLabel);
|
|
582
|
+
if (!op) {
|
|
583
|
+
addError(line, `无法解析的连线:${text.slice(cursor)}`, "连线写成 -->、-.->、==>、--- 等;标签用 |文字| 或 -- 文字 -->");
|
|
584
|
+
break;
|
|
585
|
+
}
|
|
586
|
+
cursor = op.end;
|
|
587
|
+
const next = parseFlowGroup(text, cursor, cleanLabel);
|
|
588
|
+
if (!next) {
|
|
589
|
+
addError(line, "连线后面缺少节点", "在连线运算符后写节点 id");
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
cursor = next.end;
|
|
593
|
+
const right = next.tokens.map((t) => resolveToken(t, line)).filter(Boolean);
|
|
594
|
+
for (const a of left) for (const b of right) addEdge(a, b, op, line, a === left.at(-1) && b === right[0] ? op.id : null);
|
|
595
|
+
left = right;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
for (const { id, metadata, line } of edgeMetadata) {
|
|
599
|
+
const edge = edgeIds.get(id);
|
|
600
|
+
if (!edge) {
|
|
601
|
+
errors.push(warning("DIAGRAM_HINT", lang, line, `未找到连线 ${id}`, `Unknown edge ${id}`));
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
const { animate, animation, step, curve } = metadata;
|
|
605
|
+
edge.anim = {
|
|
606
|
+
animate: animate == null ? Boolean(animation) : animate === "true",
|
|
607
|
+
animation,
|
|
608
|
+
step: Number(step) || null,
|
|
609
|
+
curve
|
|
610
|
+
};
|
|
611
|
+
if ((edge.anim.animate || step || curve) && !warned.has("edge-motion")) {
|
|
612
|
+
warned.add("edge-motion");
|
|
613
|
+
errors.push(warning("IGNORED_CONFIG", lang, line, "连线采用静态正交绘制", "Edges use static orthogonal routes"));
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
if (stack.length && !depthLimit) addError(statements[statements.length - 1].line, "subgraph 没有结束的 end", "补上 end");
|
|
617
|
+
for (const [id, view] of frameMetadata) {
|
|
618
|
+
const frame = subgraphs.get(id);
|
|
619
|
+
if (frame && ["collapsed", "expanded"].includes(view)) frame.view = view;
|
|
620
|
+
}
|
|
621
|
+
for (const node of model.nodes) if (subgraphs.has(node.id)) node.conflict = true;
|
|
622
|
+
model.direction = direction;
|
|
623
|
+
model.nodes.sort((a, b) => a.seq - b.seq);
|
|
624
|
+
model.subgraphs.sort((a, b) => a.seq - b.seq);
|
|
625
|
+
return model;
|
|
626
|
+
}
|
|
627
|
+
function parseFlowchart(statements, errors, lang, look) {
|
|
628
|
+
const { text: head, line } = statements[0];
|
|
629
|
+
if (/^flowchart-elk\b/.test(head)) errors.push(warning("IGNORED_CONFIG", lang, line, "使用 KChart 布局代替 ELK", "Using KChart layout instead of ELK"));
|
|
630
|
+
let direction = "TB";
|
|
631
|
+
const header = /^(?:flowchart|graph)(?:-elk)?\b(?:\s+(.*))?$/.exec(head);
|
|
632
|
+
if (header?.[1]) {
|
|
633
|
+
const token = header[1];
|
|
634
|
+
const symbols = {
|
|
635
|
+
"<": "RL",
|
|
636
|
+
">": "LR",
|
|
637
|
+
"^": "BT",
|
|
638
|
+
v: "TB",
|
|
639
|
+
BR: "TB"
|
|
640
|
+
};
|
|
641
|
+
const d = Object.hasOwn(symbols, token) ? { direction: symbols[token] } : token === token.toUpperCase() ? parseDirection(token) : null;
|
|
642
|
+
if (d?.direction) direction = d.direction;
|
|
643
|
+
else statements = [
|
|
644
|
+
statements[0],
|
|
645
|
+
{
|
|
646
|
+
text: token,
|
|
647
|
+
line: statements[0].line
|
|
648
|
+
},
|
|
649
|
+
...statements.slice(1)
|
|
650
|
+
];
|
|
651
|
+
}
|
|
652
|
+
return parseFlowchartBody(statements, errors, direction, lang, look);
|
|
653
|
+
}
|
|
654
|
+
function parseStateDecl(rest, normalizeLabel) {
|
|
655
|
+
let text = rest.trim();
|
|
656
|
+
let kind = "state";
|
|
657
|
+
let m = /<<(choice|fork|join)>>\s*$/i.exec(text);
|
|
658
|
+
if (m) {
|
|
659
|
+
kind = m[1].toLowerCase();
|
|
660
|
+
text = text.slice(0, m.index).trim();
|
|
661
|
+
}
|
|
662
|
+
m = /\[\[(choice|fork|join)\]\]\s*$/i.exec(text);
|
|
663
|
+
if (m) {
|
|
664
|
+
kind = m[1].toLowerCase();
|
|
665
|
+
text = text.slice(0, m.index).trim();
|
|
666
|
+
}
|
|
667
|
+
m = /^"([\s\S]*)"\s+as\s+([^\s:]+)(?:\s*:\s*(.*))?$/i.exec(text);
|
|
668
|
+
if (m) return {
|
|
669
|
+
id: m[2],
|
|
670
|
+
label: normalizeLabel(m[1]),
|
|
671
|
+
kind,
|
|
672
|
+
desc: m[3] == null ? null : normalizeLabel(m[3])
|
|
673
|
+
};
|
|
674
|
+
return {
|
|
675
|
+
id: text,
|
|
676
|
+
label: null,
|
|
677
|
+
kind
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
function parseState(statements, errors, lang, look) {
|
|
681
|
+
const limits = look ? MERMAID_LIMITS : LIMITS;
|
|
682
|
+
const cleanLabel = look ? (value) => String(value).trim() : normalizeLabel;
|
|
683
|
+
const head = statements[0].text;
|
|
684
|
+
const model = {
|
|
685
|
+
type: "state",
|
|
686
|
+
direction: "TB",
|
|
687
|
+
nodes: [],
|
|
688
|
+
edges: [],
|
|
689
|
+
subgraphs: []
|
|
690
|
+
};
|
|
691
|
+
let direction = "TB";
|
|
692
|
+
const header = /^stateDiagram(?:-v2)?\b\s*([A-Za-z]*)/i.exec(head);
|
|
693
|
+
if (header && header[1]) {
|
|
694
|
+
const d = parseDirection(header[1]);
|
|
695
|
+
if (d.error) errors.push({
|
|
696
|
+
line: statements[0].line,
|
|
697
|
+
message: d.error,
|
|
698
|
+
fix: "方向用 TB、TD、BT、LR、RL"
|
|
699
|
+
});
|
|
700
|
+
else direction = d.direction;
|
|
701
|
+
}
|
|
702
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
703
|
+
const subgraphs = /* @__PURE__ */ new Map();
|
|
704
|
+
const stack = [];
|
|
705
|
+
const knownComposite = /* @__PURE__ */ new Set();
|
|
706
|
+
const compositeMeta = /* @__PURE__ */ new Map();
|
|
707
|
+
const metaFor = (id) => {
|
|
708
|
+
if (!compositeMeta.has(id)) compositeMeta.set(id, {
|
|
709
|
+
label: null,
|
|
710
|
+
desc: [],
|
|
711
|
+
class: null
|
|
712
|
+
});
|
|
713
|
+
return compositeMeta.get(id);
|
|
714
|
+
};
|
|
715
|
+
for (const st of statements) {
|
|
716
|
+
const m = /^state\s+(.+?)\s*\{\s*$/i.exec(st.text);
|
|
717
|
+
if (!m) continue;
|
|
718
|
+
const decl = parseStateDecl(m[1], cleanLabel);
|
|
719
|
+
if (decl.id) knownComposite.add(decl.id);
|
|
720
|
+
}
|
|
721
|
+
let seq = 0;
|
|
722
|
+
let edgeSeq = 0;
|
|
723
|
+
let nodeLimit = false;
|
|
724
|
+
let edgeLimit = false;
|
|
725
|
+
const activeRegions = /* @__PURE__ */ new Map();
|
|
726
|
+
const notes = [];
|
|
727
|
+
const currentParent = () => {
|
|
728
|
+
const id = stack.at(-1)?.id ?? null;
|
|
729
|
+
return activeRegions.get(id)?.id ?? id;
|
|
730
|
+
};
|
|
731
|
+
const addError = (line, message, fix) => errors.push({
|
|
732
|
+
line,
|
|
733
|
+
message,
|
|
734
|
+
fix
|
|
735
|
+
});
|
|
736
|
+
const checkClass = (name, line) => {
|
|
737
|
+
if (!CLASSES.includes(name)) {
|
|
738
|
+
addError(line, `未知分类:${name}`, `可用分类:${CLASSES.join("、")}`);
|
|
739
|
+
return null;
|
|
740
|
+
}
|
|
741
|
+
return name;
|
|
742
|
+
};
|
|
743
|
+
const countStates = () => model.nodes.filter((n) => !n.pseudo).length;
|
|
744
|
+
const ensureState = (id, line, { label = null, kind = "state" } = {}) => {
|
|
745
|
+
if (knownComposite.has(id) || subgraphs.has(id)) return null;
|
|
746
|
+
let node = nodes.get(id);
|
|
747
|
+
if (!node) {
|
|
748
|
+
node = {
|
|
749
|
+
id,
|
|
750
|
+
label: label ?? id,
|
|
751
|
+
kind: kind === "state" ? "state" : kind,
|
|
752
|
+
desc: [],
|
|
753
|
+
class: null,
|
|
754
|
+
parent: currentParent(),
|
|
755
|
+
seq: seq++,
|
|
756
|
+
line
|
|
757
|
+
};
|
|
758
|
+
nodes.set(id, node);
|
|
759
|
+
model.nodes.push(node);
|
|
760
|
+
if (countStates() === limits.nodes + 1 && !nodeLimit) {
|
|
761
|
+
nodeLimit = true;
|
|
762
|
+
addError(line, `状态 ${countStates()} 个,超过上限 ${limits.nodes}`, "拆成总图和局部图");
|
|
763
|
+
}
|
|
764
|
+
} else {
|
|
765
|
+
if (label != null) node.label = label;
|
|
766
|
+
if (kind !== "state") node.kind = kind;
|
|
767
|
+
if (node.parent == null && currentParent() != null) node.parent = currentParent();
|
|
768
|
+
}
|
|
769
|
+
return node;
|
|
770
|
+
};
|
|
771
|
+
const ensurePseudo = (kind, line) => {
|
|
772
|
+
const scope = currentParent();
|
|
773
|
+
const id = `@@${kind}:${scope ?? "@root"}`;
|
|
774
|
+
let node = nodes.get(id);
|
|
775
|
+
if (!node) {
|
|
776
|
+
node = {
|
|
777
|
+
id,
|
|
778
|
+
label: "",
|
|
779
|
+
kind,
|
|
780
|
+
desc: [],
|
|
781
|
+
class: null,
|
|
782
|
+
parent: scope,
|
|
783
|
+
seq: seq++,
|
|
784
|
+
line,
|
|
785
|
+
pseudo: true
|
|
786
|
+
};
|
|
787
|
+
nodes.set(id, node);
|
|
788
|
+
model.nodes.push(node);
|
|
789
|
+
}
|
|
790
|
+
return node;
|
|
791
|
+
};
|
|
792
|
+
const addEdge = (from, to, label, line, note = false) => {
|
|
793
|
+
model.edges.push({
|
|
794
|
+
seq: edgeSeq++,
|
|
795
|
+
from,
|
|
796
|
+
to,
|
|
797
|
+
fromFrame: false,
|
|
798
|
+
toFrame: false,
|
|
799
|
+
label: label == null ? null : cleanLabel(stripQuotes(label)),
|
|
800
|
+
style: note ? "dashed" : "solid",
|
|
801
|
+
startArrow: false,
|
|
802
|
+
endArrow: !note,
|
|
803
|
+
endMark: note ? "none" : "barb",
|
|
804
|
+
extra: 0,
|
|
805
|
+
line
|
|
806
|
+
});
|
|
807
|
+
if (model.edges.length === limits.edges + 1 && !edgeLimit) {
|
|
808
|
+
edgeLimit = true;
|
|
809
|
+
addError(line, `连线 ${model.edges.length} 条,超过上限 ${limits.edges}`, "拆成总图和局部图");
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
const ignored = /* @__PURE__ */ new Set();
|
|
813
|
+
let skipDepth = 0, depthLimit = false;
|
|
814
|
+
for (const st of statements.slice(1)) {
|
|
815
|
+
const text = st.text;
|
|
816
|
+
const line = st.line;
|
|
817
|
+
if (skipDepth) {
|
|
818
|
+
if (/^state\s+.+\{\s*$/i.test(text)) skipDepth += 1;
|
|
819
|
+
else if (text === "}") skipDepth -= 1;
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
if (/^direction\b/i.test(text)) {
|
|
823
|
+
const d = parseDirection(text.replace(/^direction\b/i, "").trim());
|
|
824
|
+
if (d.error) addError(line, d.error, "方向用 TB、TD、BT、LR、RL");
|
|
825
|
+
else if (stack.length) (activeRegions.get(stack.at(-1).id) ?? stack.at(-1)).dir = d.direction;
|
|
826
|
+
else direction = d.direction;
|
|
827
|
+
continue;
|
|
828
|
+
}
|
|
829
|
+
if (/^\}\s*$/.test(text)) {
|
|
830
|
+
if (!stack.length) {
|
|
831
|
+
addError(line, "} 没有对应的 state {", "删除多余的 }");
|
|
832
|
+
continue;
|
|
833
|
+
}
|
|
834
|
+
stack.pop();
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
if (text === "--") {
|
|
838
|
+
const frame = stack.at(-1);
|
|
839
|
+
if (!frame) {
|
|
840
|
+
addError(line, "-- 只能放在复合状态中", "将分区放入 state A { … }");
|
|
841
|
+
continue;
|
|
842
|
+
}
|
|
843
|
+
const addRegion = () => {
|
|
844
|
+
const region = {
|
|
845
|
+
id: `@@region:${seq}`,
|
|
846
|
+
title: "",
|
|
847
|
+
parent: frame.id,
|
|
848
|
+
depth: frame.depth + 1,
|
|
849
|
+
seq: seq++,
|
|
850
|
+
line,
|
|
851
|
+
kind: "divider"
|
|
852
|
+
};
|
|
853
|
+
model.subgraphs.push(region);
|
|
854
|
+
activeRegions.set(frame.id, region);
|
|
855
|
+
return region;
|
|
856
|
+
};
|
|
857
|
+
if (!activeRegions.has(frame.id)) {
|
|
858
|
+
const first = addRegion();
|
|
859
|
+
for (const item of [...model.nodes, ...model.subgraphs]) if (item !== first && item.parent === frame.id) item.parent = first.id;
|
|
860
|
+
}
|
|
861
|
+
addRegion();
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
if (/^note\b/i.test(text)) {
|
|
865
|
+
const note = /^note\s+(left|right)\s+of\s+([^\s:]+)(?:\s*:\s*([\s\S]*)|\n([\s\S]*))$/i.exec(text);
|
|
866
|
+
const floating = /^note\s+"([\s\S]*)"\s+as\s+(\S+)$/i.exec(text);
|
|
867
|
+
if (!note && !floating) {
|
|
868
|
+
addError(line, "无法解析状态图 note", "使用 note left of A: 说明,或用 end note 结束多行说明");
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
871
|
+
const id = floating ? floating[2] : `@@note:${seq}`;
|
|
872
|
+
if (note) ensureState(note[2], line);
|
|
873
|
+
const node = ensureState(id, line, {
|
|
874
|
+
kind: "note",
|
|
875
|
+
label: cleanLabel(note ? note[3] ?? note[4] : floating[1])
|
|
876
|
+
});
|
|
877
|
+
if (node && note) {
|
|
878
|
+
notes.push({
|
|
879
|
+
node,
|
|
880
|
+
target: note[2]
|
|
881
|
+
});
|
|
882
|
+
if (note[1].toLowerCase() === "left") addEdge(id, note[2], null, line, true);
|
|
883
|
+
else addEdge(note[2], id, null, line, true);
|
|
884
|
+
}
|
|
885
|
+
continue;
|
|
886
|
+
}
|
|
887
|
+
if (/^(hide\s+empty\s+description|scale\s+\d+(?:\.\d+)?\s+width)$/i.exec(text)) {
|
|
888
|
+
const key = text.split(/\s/)[0].toLowerCase();
|
|
889
|
+
if (!ignored.has(key)) {
|
|
890
|
+
ignored.add(key);
|
|
891
|
+
errors.push(warning("IGNORED_CONFIG", lang, line, `忽略状态图 ${key} 语句`, `Ignored state diagram ${key} statement`));
|
|
892
|
+
}
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
if (look?.accept(text, line)) continue;
|
|
896
|
+
if (/^(classDef|style|linkStyle)\b/i.test(text)) {
|
|
897
|
+
addError(line, `不支持 ${text.split(/\s+/)[0]}`, "颜色由主题决定,删掉这一行;节点分类用 :::warn 等");
|
|
898
|
+
continue;
|
|
899
|
+
}
|
|
900
|
+
if (/^(click|call)\b/i.test(text)) {
|
|
901
|
+
addError(line, `不支持 ${text.split(/\s+/)[0]}`, "删掉交互语句");
|
|
902
|
+
continue;
|
|
903
|
+
}
|
|
904
|
+
if (/^class\s+/i.test(text)) {
|
|
905
|
+
const m = /^class\s+([^\s]+)\s+([A-Za-z_]\w*)\s*$/i.exec(text);
|
|
906
|
+
if (!m) {
|
|
907
|
+
addError(line, "class 写法不对", "写成 class A danger");
|
|
908
|
+
continue;
|
|
909
|
+
}
|
|
910
|
+
const cls = checkClass(m[2], line);
|
|
911
|
+
for (const id of m[1].split(",").map((x) => x.trim()).filter(Boolean)) {
|
|
912
|
+
if (subgraphs.has(id)) {
|
|
913
|
+
if (cls) subgraphs.get(id).class = cls;
|
|
914
|
+
continue;
|
|
915
|
+
}
|
|
916
|
+
if (knownComposite.has(id)) {
|
|
917
|
+
if (cls) metaFor(id).class = cls;
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
const node = nodes.get(id) || ensureState(id, line);
|
|
921
|
+
if (node && cls) node.class = cls;
|
|
922
|
+
}
|
|
923
|
+
continue;
|
|
924
|
+
}
|
|
925
|
+
if (/^state\b/i.test(text)) {
|
|
926
|
+
const block = /^state\s+(.+?)\s*\{\s*$/i.exec(text);
|
|
927
|
+
if (block) {
|
|
928
|
+
const decl = parseStateDecl(block[1], cleanLabel);
|
|
929
|
+
if (!decl.id) {
|
|
930
|
+
addError(line, "state { 缺少 id", "写成 state A {");
|
|
931
|
+
continue;
|
|
932
|
+
}
|
|
933
|
+
if (subgraphs.has(decl.id)) {
|
|
934
|
+
addError(line, `复合状态 id 重复:${decl.id}`, "换一个 id");
|
|
935
|
+
continue;
|
|
936
|
+
}
|
|
937
|
+
if (stack.length + 1 > LIMITS.stateDepth) {
|
|
938
|
+
if (!depthLimit) addError(line, `复合状态嵌套 ${stack.length + 1} 层,超过上限 ${LIMITS.stateDepth} 层`, "拆成平级状态或拆成两张图");
|
|
939
|
+
depthLimit = true;
|
|
940
|
+
skipDepth = 1;
|
|
941
|
+
continue;
|
|
942
|
+
}
|
|
943
|
+
const placeholder = nodes.get(decl.id);
|
|
944
|
+
const meta = metaFor(decl.id);
|
|
945
|
+
const display = decl.label || meta.label;
|
|
946
|
+
let title = display || meta.desc[0];
|
|
947
|
+
if (!title && placeholder && placeholder.label !== placeholder.id) title = placeholder.label;
|
|
948
|
+
if (placeholder) {
|
|
949
|
+
nodes.delete(decl.id);
|
|
950
|
+
const at = model.nodes.indexOf(placeholder);
|
|
951
|
+
if (at >= 0) model.nodes.splice(at, 1);
|
|
952
|
+
}
|
|
953
|
+
const sg = {
|
|
954
|
+
id: decl.id,
|
|
955
|
+
title: title || decl.id,
|
|
956
|
+
parent: currentParent(),
|
|
957
|
+
depth: stack.length + 1,
|
|
958
|
+
seq: seq++,
|
|
959
|
+
line,
|
|
960
|
+
kind: "composite",
|
|
961
|
+
desc: [...decl.desc == null ? [] : [decl.desc], ...meta.desc.slice(display ? 0 : 1)],
|
|
962
|
+
class: meta.class
|
|
963
|
+
};
|
|
964
|
+
subgraphs.set(sg.id, sg);
|
|
965
|
+
model.subgraphs.push(sg);
|
|
966
|
+
stack.push(sg);
|
|
967
|
+
continue;
|
|
968
|
+
}
|
|
969
|
+
const decl = parseStateDecl(text.replace(/^state\b/i, ""), cleanLabel);
|
|
970
|
+
if (!decl.id) {
|
|
971
|
+
addError(line, "state 缺少 id", "写成 state A 或 state \"名称\" as A");
|
|
972
|
+
continue;
|
|
973
|
+
}
|
|
974
|
+
if (knownComposite.has(decl.id)) {
|
|
975
|
+
if (decl.label) {
|
|
976
|
+
metaFor(decl.id).label = decl.label;
|
|
977
|
+
if (subgraphs.has(decl.id)) subgraphs.get(decl.id).title = decl.label;
|
|
978
|
+
}
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
const node = ensureState(decl.id, line, {
|
|
982
|
+
label: decl.label,
|
|
983
|
+
kind: decl.kind
|
|
984
|
+
});
|
|
985
|
+
if (node && decl.desc != null) node.desc.push(decl.desc);
|
|
986
|
+
continue;
|
|
987
|
+
}
|
|
988
|
+
const arrow = /^(.+?)\s*-->\s*(.+)$/.exec(text);
|
|
989
|
+
if (arrow) {
|
|
990
|
+
const parseEnd = (raw) => {
|
|
991
|
+
let value = raw.trim();
|
|
992
|
+
let cls = null;
|
|
993
|
+
const at = value.indexOf(":::");
|
|
994
|
+
if (at >= 0) {
|
|
995
|
+
cls = value.slice(at + 3).trim();
|
|
996
|
+
value = value.slice(0, at).trim();
|
|
997
|
+
}
|
|
998
|
+
return {
|
|
999
|
+
id: value,
|
|
1000
|
+
cls
|
|
1001
|
+
};
|
|
1002
|
+
};
|
|
1003
|
+
const leftTok = parseEnd(arrow[1]);
|
|
1004
|
+
const colon = /(?<!:):(?!:)/.exec(arrow[2])?.index ?? -1;
|
|
1005
|
+
const rightTok = parseEnd(colon < 0 ? arrow[2] : arrow[2].slice(0, colon));
|
|
1006
|
+
const label = colon < 0 ? null : arrow[2].slice(colon + 1).trim();
|
|
1007
|
+
if (!leftTok.id || !rightTok.id) {
|
|
1008
|
+
addError(line, "状态转移缺少起点或终点", "写成 A --> B : 说明");
|
|
1009
|
+
continue;
|
|
1010
|
+
}
|
|
1011
|
+
if (look) {
|
|
1012
|
+
for (const token of [leftTok, rightTok]) if (token.cls) look.addClasses(token.id, token.cls.split(","), line);
|
|
1013
|
+
}
|
|
1014
|
+
const leftClass = !look && leftTok.cls ? checkClass(leftTok.cls, line) : null;
|
|
1015
|
+
const rightClass = !look && rightTok.cls ? checkClass(rightTok.cls, line) : null;
|
|
1016
|
+
let from;
|
|
1017
|
+
let to;
|
|
1018
|
+
if (leftTok.id === "[*]") from = ensurePseudo("start", line).id;
|
|
1019
|
+
else {
|
|
1020
|
+
const node = ensureState(leftTok.id, line);
|
|
1021
|
+
if (node && leftClass) node.class = leftClass;
|
|
1022
|
+
from = leftTok.id;
|
|
1023
|
+
}
|
|
1024
|
+
if (rightTok.id === "[*]") to = ensurePseudo("end", line).id;
|
|
1025
|
+
else {
|
|
1026
|
+
const node = ensureState(rightTok.id, line);
|
|
1027
|
+
if (node && rightClass) node.class = rightClass;
|
|
1028
|
+
to = rightTok.id;
|
|
1029
|
+
}
|
|
1030
|
+
addEdge(from, to, label, line);
|
|
1031
|
+
continue;
|
|
1032
|
+
}
|
|
1033
|
+
const desc = /^([^\s:]+)\s*:\s*(.+)$/.exec(text);
|
|
1034
|
+
if (desc) {
|
|
1035
|
+
const id = desc[1];
|
|
1036
|
+
const value = cleanLabel(desc[2]);
|
|
1037
|
+
if (subgraphs.has(id)) subgraphs.get(id).desc.push(value);
|
|
1038
|
+
else if (knownComposite.has(id)) metaFor(id).desc.push(value);
|
|
1039
|
+
else {
|
|
1040
|
+
const node = nodes.get(id) || ensureState(id, line);
|
|
1041
|
+
if (node) node.desc.push(value);
|
|
1042
|
+
}
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
1045
|
+
if (/^(stateDiagram|flowchart|graph|erDiagram)\b/i.test(text)) {
|
|
1046
|
+
addError(line, "图类型只能声明一次", "删掉重复的声明");
|
|
1047
|
+
continue;
|
|
1048
|
+
}
|
|
1049
|
+
const styled = look && /^([^\s:{}]+):::(\S+)$/.exec(text);
|
|
1050
|
+
if (styled) {
|
|
1051
|
+
ensureState(styled[1], line);
|
|
1052
|
+
look.addClasses(styled[1], styled[2].split(","), line);
|
|
1053
|
+
} else if (/^[^\s:{}]+$/.test(text)) ensureState(text, line);
|
|
1054
|
+
else addError(line, `无法解析的语句:${text}`, "检查状态名、转移箭头与 : 说明的写法");
|
|
1055
|
+
}
|
|
1056
|
+
for (const { node, target } of notes) node.parent = (nodes.get(target) ?? subgraphs.get(target))?.parent ?? null;
|
|
1057
|
+
const occupied = new Set([...model.nodes, ...model.subgraphs].map((item) => item.parent));
|
|
1058
|
+
model.subgraphs = model.subgraphs.filter((frame) => frame.kind !== "divider" || occupied.has(frame.id));
|
|
1059
|
+
for (const node of model.nodes) if (node.kind === "state" && node.desc.length === 1 && node.label === node.id) node.label = node.desc.pop();
|
|
1060
|
+
if (stack.length && !depthLimit) addError(statements[statements.length - 1].line, "复合状态没有结束的 }", "补上 }");
|
|
1061
|
+
for (const edge of model.edges) {
|
|
1062
|
+
edge.fromFrame = subgraphs.has(edge.from) && !nodes.has(edge.from);
|
|
1063
|
+
edge.toFrame = subgraphs.has(edge.to) && !nodes.has(edge.to);
|
|
1064
|
+
}
|
|
1065
|
+
model.direction = direction;
|
|
1066
|
+
model.nodes.sort((a, b) => a.seq - b.seq);
|
|
1067
|
+
model.subgraphs.sort((a, b) => a.seq - b.seq);
|
|
1068
|
+
return model;
|
|
1069
|
+
}
|
|
1070
|
+
function parseEr(statements, errors) {
|
|
1071
|
+
const head = statements[0].text;
|
|
1072
|
+
const model = {
|
|
1073
|
+
type: "er",
|
|
1074
|
+
direction: "TB",
|
|
1075
|
+
nodes: [],
|
|
1076
|
+
edges: [],
|
|
1077
|
+
subgraphs: []
|
|
1078
|
+
};
|
|
1079
|
+
let direction = "TB";
|
|
1080
|
+
const header = /^erDiagram\b\s*([A-Za-z]*)/i.exec(head);
|
|
1081
|
+
if (header && header[1]) {
|
|
1082
|
+
const d = parseDirection(header[1]);
|
|
1083
|
+
if (d.error) errors.push({
|
|
1084
|
+
line: statements[0].line,
|
|
1085
|
+
message: d.error,
|
|
1086
|
+
fix: "方向用 TB、TD、BT、LR、RL"
|
|
1087
|
+
});
|
|
1088
|
+
else direction = d.direction;
|
|
1089
|
+
}
|
|
1090
|
+
const entities = /* @__PURE__ */ new Map();
|
|
1091
|
+
let seq = 0;
|
|
1092
|
+
let edgeSeq = 0;
|
|
1093
|
+
let nodeLimit = false;
|
|
1094
|
+
let edgeLimit = false;
|
|
1095
|
+
const addError = (line, message, fix) => errors.push({
|
|
1096
|
+
line,
|
|
1097
|
+
message,
|
|
1098
|
+
fix
|
|
1099
|
+
});
|
|
1100
|
+
const ensureEntity = (id, line, alias = null) => {
|
|
1101
|
+
let entity = entities.get(id);
|
|
1102
|
+
if (!entity) {
|
|
1103
|
+
entity = {
|
|
1104
|
+
id,
|
|
1105
|
+
label: alias || id,
|
|
1106
|
+
kind: "er",
|
|
1107
|
+
shape: "er",
|
|
1108
|
+
fields: [],
|
|
1109
|
+
class: null,
|
|
1110
|
+
parent: null,
|
|
1111
|
+
seq: seq++,
|
|
1112
|
+
line
|
|
1113
|
+
};
|
|
1114
|
+
entities.set(id, entity);
|
|
1115
|
+
model.nodes.push(entity);
|
|
1116
|
+
if (model.nodes.length === LIMITS.nodes + 1 && !nodeLimit) {
|
|
1117
|
+
nodeLimit = true;
|
|
1118
|
+
addError(line, `实体 ${model.nodes.length} 个,超过上限 ${LIMITS.nodes}`, "拆成总图和局部图");
|
|
1119
|
+
}
|
|
1120
|
+
} else if (alias) entity.label = alias;
|
|
1121
|
+
return entity;
|
|
1122
|
+
};
|
|
1123
|
+
const addField = (entity, text, line) => {
|
|
1124
|
+
let rest = text.trim();
|
|
1125
|
+
if (!rest) return;
|
|
1126
|
+
let comment = null;
|
|
1127
|
+
const cm = /"([^"]*)"\s*$/.exec(rest);
|
|
1128
|
+
if (cm) {
|
|
1129
|
+
comment = cm[1];
|
|
1130
|
+
rest = rest.slice(0, cm.index).trim();
|
|
1131
|
+
}
|
|
1132
|
+
const parts = rest.split(/\s+/);
|
|
1133
|
+
if (parts.length < 2) {
|
|
1134
|
+
addError(line, `实体 ${entity.id} 的属性写法不对:${text}`, "写成 type name PK|FK|UK \"注释\"");
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
const keys = parts.slice(2).join(" ").split(",").map((x) => x.trim()).filter(Boolean);
|
|
1138
|
+
for (const key of keys) if (![
|
|
1139
|
+
"PK",
|
|
1140
|
+
"FK",
|
|
1141
|
+
"UK"
|
|
1142
|
+
].includes(key)) addError(line, `属性键只能是 PK、FK、UK,收到:${key}`, "删掉未知的键标记,或改成 PK/FK/UK");
|
|
1143
|
+
entity.fields.push({
|
|
1144
|
+
type: parts[0],
|
|
1145
|
+
name: parts[1],
|
|
1146
|
+
keys: keys.filter((key) => [
|
|
1147
|
+
"PK",
|
|
1148
|
+
"FK",
|
|
1149
|
+
"UK"
|
|
1150
|
+
].includes(key)),
|
|
1151
|
+
comment
|
|
1152
|
+
});
|
|
1153
|
+
};
|
|
1154
|
+
const addRelation = (from, to, startCard, endCard, style, label, line) => {
|
|
1155
|
+
ensureEntity(from, line);
|
|
1156
|
+
ensureEntity(to, line);
|
|
1157
|
+
model.edges.push({
|
|
1158
|
+
seq: edgeSeq++,
|
|
1159
|
+
from,
|
|
1160
|
+
to,
|
|
1161
|
+
fromFrame: false,
|
|
1162
|
+
toFrame: false,
|
|
1163
|
+
label: label == null ? null : normalizeLabel(stripQuotes(label)),
|
|
1164
|
+
startCard,
|
|
1165
|
+
endCard,
|
|
1166
|
+
style: style === ".." ? "dashed" : "solid",
|
|
1167
|
+
startArrow: false,
|
|
1168
|
+
endArrow: false,
|
|
1169
|
+
extra: 0,
|
|
1170
|
+
line
|
|
1171
|
+
});
|
|
1172
|
+
if (model.edges.length === LIMITS.edges + 1 && !edgeLimit) {
|
|
1173
|
+
edgeLimit = true;
|
|
1174
|
+
addError(line, `关系 ${model.edges.length} 条,超过上限 ${LIMITS.edges}`, "拆成总图和局部图");
|
|
1175
|
+
}
|
|
1176
|
+
};
|
|
1177
|
+
for (let i = 1; i < statements.length; i += 1) {
|
|
1178
|
+
const st = statements[i];
|
|
1179
|
+
const text = st.text;
|
|
1180
|
+
const line = st.line;
|
|
1181
|
+
if (/^direction\b/i.test(text)) {
|
|
1182
|
+
const d = parseDirection(text.replace(/^direction\b/i, "").trim());
|
|
1183
|
+
if (d.error) addError(line, d.error, "方向用 TB、TD、BT、LR、RL");
|
|
1184
|
+
else direction = d.direction;
|
|
1185
|
+
continue;
|
|
1186
|
+
}
|
|
1187
|
+
if (/^(classDef|style|linkStyle)\b/i.test(text)) {
|
|
1188
|
+
addError(line, `ER 图不支持 ${text.split(/\s+/)[0]}`, "颜色由主题决定,删掉这一行");
|
|
1189
|
+
continue;
|
|
1190
|
+
}
|
|
1191
|
+
const block = /^([^\s{}[\]]+)\s*(?:\["([^"]*)"\])?\s*\{\s*$/.exec(text);
|
|
1192
|
+
if (block) {
|
|
1193
|
+
const entity = ensureEntity(block[1], line, block[2] || null);
|
|
1194
|
+
let closed = false;
|
|
1195
|
+
i += 1;
|
|
1196
|
+
for (; i < statements.length; i += 1) {
|
|
1197
|
+
const row = statements[i];
|
|
1198
|
+
if (/^\}\s*$/.test(row.text)) {
|
|
1199
|
+
closed = true;
|
|
1200
|
+
break;
|
|
1201
|
+
}
|
|
1202
|
+
addField(entity, row.text, row.line);
|
|
1203
|
+
}
|
|
1204
|
+
if (!closed) addError(line, `实体 ${entity.id} 的属性块没有结束的 }`, "补一个 }");
|
|
1205
|
+
continue;
|
|
1206
|
+
}
|
|
1207
|
+
const aliasOnly = /^([^\s{}[\]]+)\s*\["([^"]*)"\]\s*$/.exec(text);
|
|
1208
|
+
if (aliasOnly) {
|
|
1209
|
+
ensureEntity(aliasOnly[1], line, aliasOnly[2]);
|
|
1210
|
+
continue;
|
|
1211
|
+
}
|
|
1212
|
+
const rel = /^(\S+)\s+(\|\||\|o|\}o|\}\|)\s*(--|\.\.)\s*(\|\||o\||o\{|\|\{)\s+(\S+)\s*(?::\s*(.*))?$/.exec(text);
|
|
1213
|
+
if (rel) {
|
|
1214
|
+
addRelation(rel[1], rel[5], rel[2], rel[4], rel[3], rel[6], line);
|
|
1215
|
+
continue;
|
|
1216
|
+
}
|
|
1217
|
+
if (/^\}\s*$/.test(text)) {
|
|
1218
|
+
addError(line, "没有对应的实体 {", "删除多余的 }");
|
|
1219
|
+
continue;
|
|
1220
|
+
}
|
|
1221
|
+
if (/^(erDiagram|flowchart|graph|stateDiagram)\b/i.test(text)) {
|
|
1222
|
+
addError(line, "图类型只能声明一次", "删掉重复的声明");
|
|
1223
|
+
continue;
|
|
1224
|
+
}
|
|
1225
|
+
addError(line, `无法解析的语句:${text}`, "实体写成 ENTITY { … };关系写成 A ||--o{ B : 标签");
|
|
1226
|
+
}
|
|
1227
|
+
model.direction = direction;
|
|
1228
|
+
model.nodes.sort((a, b) => a.seq - b.seq);
|
|
1229
|
+
return model;
|
|
1230
|
+
}
|
|
1231
|
+
/**
|
|
1232
|
+
* 供泳道图复用的流程语法解析。调用方传入已剔除泳道壳层的流程语句,
|
|
1233
|
+
* 每条 { text, line };line 必须是原源码行号,这样报错仍指向用户看到的那一行。
|
|
1234
|
+
* direction 只记录在模型上,不参与此处的解析。
|
|
1235
|
+
*/
|
|
1236
|
+
function parseFlow(statements, { direction = "TB" } = {}) {
|
|
1237
|
+
const errors = [];
|
|
1238
|
+
const safeDirection = DIRECTIONS.includes(direction) ? direction : "TB";
|
|
1239
|
+
const model = parseFlowchartBody([{
|
|
1240
|
+
text: "flowchart",
|
|
1241
|
+
line: 0
|
|
1242
|
+
}, ...statements], errors, safeDirection);
|
|
1243
|
+
return {
|
|
1244
|
+
ok: errors.length === 0,
|
|
1245
|
+
errors,
|
|
1246
|
+
model
|
|
1247
|
+
};
|
|
1248
|
+
}
|
|
1249
|
+
function statementsOf(body, firstLine, kind) {
|
|
1250
|
+
const state = kind?.startsWith("statediagram"), flow = kind === "flowchart";
|
|
1251
|
+
const rows = String(body ?? "").replace(/\r\n?/g, "\n").split("\n");
|
|
1252
|
+
const statements = [];
|
|
1253
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
1254
|
+
let row = rows[index];
|
|
1255
|
+
const line = firstLine + index;
|
|
1256
|
+
if (flow) for (const token of row.matchAll(/"(?:\\.|[^"\\])*"|'(?:''|[^'])*'|@\{/g)) {
|
|
1257
|
+
if (token[0] !== "@{" || findClose(row, token.index + 2, "}") >= 0) continue;
|
|
1258
|
+
const rest = rows.slice(index).join("\n");
|
|
1259
|
+
const end = findClose(rest, token.index + 2, "}");
|
|
1260
|
+
if (end >= 0) {
|
|
1261
|
+
index += rest.slice(0, end).split("\n").length - 1;
|
|
1262
|
+
row = rest.slice(0, end + 1) + rows[index].slice(end - rest.lastIndexOf("\n", end));
|
|
1263
|
+
}
|
|
1264
|
+
break;
|
|
1265
|
+
}
|
|
1266
|
+
if (state && /^\s*note\s+(left|right)\s+of\s+[^\s:]+\s*$/i.test(row)) {
|
|
1267
|
+
const end = rows.findIndex((value, i) => i > index && /^\s*end\s+note\s*$/i.test(value));
|
|
1268
|
+
if (end !== -1) {
|
|
1269
|
+
statements.push({
|
|
1270
|
+
text: [row.trim(), ...rows.slice(index + 1, end).map((value) => value.trim())].join("\n"),
|
|
1271
|
+
line
|
|
1272
|
+
});
|
|
1273
|
+
index = end;
|
|
1274
|
+
continue;
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
for (const piece of splitStatements(row, flow)) {
|
|
1278
|
+
const text = piece.trim();
|
|
1279
|
+
if (state && /^state\s+.+?\{\s*\}\s*$/i.test(text)) statements.push({
|
|
1280
|
+
text: text.slice(0, -1).trim(),
|
|
1281
|
+
line
|
|
1282
|
+
}, {
|
|
1283
|
+
text: "}",
|
|
1284
|
+
line
|
|
1285
|
+
});
|
|
1286
|
+
else if (text && !/^\s*%%/.test(text) && !/^acc(Title|Descr)\s*:/i.test(text)) statements.push({
|
|
1287
|
+
text,
|
|
1288
|
+
line
|
|
1289
|
+
});
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
return statements;
|
|
1293
|
+
}
|
|
1294
|
+
function parseGraph(body, info = {}) {
|
|
1295
|
+
const kind = String(info.kind || "").toLowerCase();
|
|
1296
|
+
const header = String(info.header || "");
|
|
1297
|
+
const headerLine = Number.isFinite(info.headerLine) ? info.headerLine : Math.max(1, (info.firstLine || 2) - 1);
|
|
1298
|
+
const firstLine = Number.isFinite(info.firstLine) ? info.firstLine : headerLine + 1;
|
|
1299
|
+
const statements = [...statementsOf(header, headerLine), ...statementsOf(body, firstLine, kind)];
|
|
1300
|
+
const errors = [];
|
|
1301
|
+
const look = info.syntax === "mermaid" && kind !== "erdiagram" ? presentation(info) : null;
|
|
1302
|
+
if (!statements.length) return {
|
|
1303
|
+
model: null,
|
|
1304
|
+
errors: [{
|
|
1305
|
+
line: headerLine,
|
|
1306
|
+
column: 1,
|
|
1307
|
+
message: "图源码为空",
|
|
1308
|
+
fix: "写 flowchart、stateDiagram-v2 或 erDiagram 开头的图"
|
|
1309
|
+
}]
|
|
1310
|
+
};
|
|
1311
|
+
const first = statements[0].text;
|
|
1312
|
+
let model;
|
|
1313
|
+
if (kind === "flowchart") model = parseFlowchart(statements, errors, info.lang, look);
|
|
1314
|
+
else if (kind === "statediagram-v2" || kind === "statediagram") model = parseState(statements, errors, info.lang, look);
|
|
1315
|
+
else if (kind === "erdiagram") model = parseEr(statements, errors);
|
|
1316
|
+
else errors.push({
|
|
1317
|
+
line: statements[0].line,
|
|
1318
|
+
column: 1,
|
|
1319
|
+
message: "不支持的图类型:" + first.split(/\s+/)[0],
|
|
1320
|
+
fix: "使用 flowchart、stateDiagram-v2 或 erDiagram 图型"
|
|
1321
|
+
});
|
|
1322
|
+
if (model && look) {
|
|
1323
|
+
model.mermaid = true;
|
|
1324
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1325
|
+
const warn = (diagnostic) => {
|
|
1326
|
+
const key = `${diagnostic.code}:${diagnostic.message}`;
|
|
1327
|
+
if (!seen.has(key)) {
|
|
1328
|
+
seen.add(key);
|
|
1329
|
+
errors.push(diagnostic);
|
|
1330
|
+
}
|
|
1331
|
+
};
|
|
1332
|
+
const normalize = (value, line, item) => {
|
|
1333
|
+
const result = normalizeLabel$1(value, {
|
|
1334
|
+
lang: info.lang,
|
|
1335
|
+
line
|
|
1336
|
+
});
|
|
1337
|
+
result.diagnostics.forEach(warn);
|
|
1338
|
+
if (item && result.icon) item.icon ??= result.icon;
|
|
1339
|
+
return result.text;
|
|
1340
|
+
};
|
|
1341
|
+
for (const item of [...model.nodes, ...model.subgraphs]) {
|
|
1342
|
+
item.class = look.statusOf(item.id);
|
|
1343
|
+
item.link = look.linkOf(item.id);
|
|
1344
|
+
if (item.iconRef) {
|
|
1345
|
+
item.icon = resolveIconRef(item.iconRef);
|
|
1346
|
+
if (!item.icon) warn(warning("IGNORED_ASSET", info.lang, item.line, `未知图标 ${item.iconRef}`, `Unknown icon ${item.iconRef}`));
|
|
1347
|
+
}
|
|
1348
|
+
const key = Object.hasOwn(item, "title") ? "title" : "label";
|
|
1349
|
+
item[key] = normalize(item[key], item.line, item);
|
|
1350
|
+
if (item.desc) item.desc = item.desc.map((value) => normalize(value, item.line));
|
|
1351
|
+
}
|
|
1352
|
+
for (const edge of model.edges) {
|
|
1353
|
+
edge.status = look.edgeStatusOf(edge.seq) ?? (edge.id ? look.statusOf(edge.id) : null);
|
|
1354
|
+
if (edge.label != null) edge.label = normalize(edge.label, edge.line, edge);
|
|
1355
|
+
}
|
|
1356
|
+
errors.push(...look.diagnostics());
|
|
1357
|
+
}
|
|
1358
|
+
return {
|
|
1359
|
+
model: model || null,
|
|
1360
|
+
errors: errors.map((item) => ({
|
|
1361
|
+
column: 1,
|
|
1362
|
+
...item
|
|
1363
|
+
}))
|
|
1364
|
+
};
|
|
1365
|
+
}
|
|
1366
|
+
export { graph_parse_exports, parseFlow, parseGraph };
|