@pairdraw/mermaid 0.0.0-stage → 0.16.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/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/chunks/detect.js +171 -0
- package/dist/chunks/detect.js.map +1 -0
- package/dist/chunks/flowchart/builder.js +538 -0
- package/dist/chunks/flowchart/builder.js.map +1 -0
- package/dist/chunks/flowchart/parse.js +3763 -0
- package/dist/chunks/flowchart/parse.js.map +1 -0
- package/dist/index.d.ts +155 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/package.json +42 -4
- package/src/detect.ts +14 -0
- package/src/flowchart/builder.ts +418 -0
- package/src/flowchart/model.ts +72 -0
- package/src/flowchart/parse.ts +19 -0
- package/src/flowchart/shapes.ts +105 -0
- package/src/index.ts +13 -0
- package/src/label.ts +37 -0
- package/src/source.ts +51 -0
- package/src/upstream/MERMAID-LICENSE.txt +21 -0
- package/src/upstream/comments.ts +13 -0
- package/src/upstream/flow.d.ts +8 -0
- package/src/upstream/flow.jison +635 -0
- package/src/upstream/flow.js +1099 -0
- package/src/upstream/preprocess.ts +62 -0
- package/src/upstream/regexes.ts +199 -0
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
import { decodeEntities } from "../detect.js";
|
|
2
|
+
import * as v from "valibot";
|
|
3
|
+
import { parse } from "yaml";
|
|
4
|
+
import { decodeHTML } from "entities";
|
|
5
|
+
import { parseFragment } from "parse5";
|
|
6
|
+
//#region src/label.ts
|
|
7
|
+
/** The text HTML shows: its text nodes, and a line break for each `<br>`. */
|
|
8
|
+
function shownText(nodes) {
|
|
9
|
+
return nodes.map((node) => {
|
|
10
|
+
if (node.nodeName === "#text" && "value" in node) return node.value;
|
|
11
|
+
if (node.nodeName === "br") return "\n";
|
|
12
|
+
return "childNodes" in node ? shownText(node.childNodes) : "";
|
|
13
|
+
}).join("");
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A label's text as plain text and its format. Mermaid draws a label as HTML, so its tags only
|
|
17
|
+
* style the text, `<br>` breaks the line, and entities stand for characters. Markdown, written
|
|
18
|
+
* in backticks, keeps its marks for whoever draws it.
|
|
19
|
+
*/
|
|
20
|
+
function readLabel(label) {
|
|
21
|
+
let text = decodeEntities(label.text.trim());
|
|
22
|
+
if (text.length >= 2 && text.startsWith("\"") && text.endsWith("\"")) text = text.slice(1, -1);
|
|
23
|
+
if (label.type === "markdown") return {
|
|
24
|
+
text: decodeHTML(text),
|
|
25
|
+
format: "markdown"
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
text: shownText(parseFragment(text).childNodes),
|
|
29
|
+
format: "text"
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/flowchart/shapes.ts
|
|
34
|
+
const SHAPE_BY_NAME = new Map(Object.entries({
|
|
35
|
+
rect: [
|
|
36
|
+
"proc",
|
|
37
|
+
"process",
|
|
38
|
+
"rectangle",
|
|
39
|
+
"squareRect"
|
|
40
|
+
],
|
|
41
|
+
rounded: ["event", "roundedRect"],
|
|
42
|
+
stadium: ["terminal", "pill"],
|
|
43
|
+
"fr-rect": [
|
|
44
|
+
"subprocess",
|
|
45
|
+
"subproc",
|
|
46
|
+
"framed-rectangle",
|
|
47
|
+
"subroutine"
|
|
48
|
+
],
|
|
49
|
+
cyl: [
|
|
50
|
+
"db",
|
|
51
|
+
"database",
|
|
52
|
+
"cylinder"
|
|
53
|
+
],
|
|
54
|
+
datastore: ["data-store"],
|
|
55
|
+
folder: ["directory"],
|
|
56
|
+
bucket: [],
|
|
57
|
+
console: [],
|
|
58
|
+
browser: [],
|
|
59
|
+
person: [],
|
|
60
|
+
circle: ["circ"],
|
|
61
|
+
bang: [],
|
|
62
|
+
cloud: [],
|
|
63
|
+
diam: [
|
|
64
|
+
"decision",
|
|
65
|
+
"diamond",
|
|
66
|
+
"question"
|
|
67
|
+
],
|
|
68
|
+
hex: ["hexagon", "prepare"],
|
|
69
|
+
"lean-r": [
|
|
70
|
+
"lean-right",
|
|
71
|
+
"in-out",
|
|
72
|
+
"lean_right"
|
|
73
|
+
],
|
|
74
|
+
"lean-l": [
|
|
75
|
+
"lean-left",
|
|
76
|
+
"out-in",
|
|
77
|
+
"lean_left"
|
|
78
|
+
],
|
|
79
|
+
"trap-b": [
|
|
80
|
+
"priority",
|
|
81
|
+
"trapezoid-bottom",
|
|
82
|
+
"trapezoid"
|
|
83
|
+
],
|
|
84
|
+
"trap-t": [
|
|
85
|
+
"manual",
|
|
86
|
+
"trapezoid-top",
|
|
87
|
+
"inv-trapezoid",
|
|
88
|
+
"inv_trapezoid"
|
|
89
|
+
],
|
|
90
|
+
"dbl-circ": ["double-circle", "doublecircle"],
|
|
91
|
+
text: [],
|
|
92
|
+
"notch-rect": ["card", "notched-rectangle"],
|
|
93
|
+
"lin-rect": [
|
|
94
|
+
"lined-rectangle",
|
|
95
|
+
"lined-process",
|
|
96
|
+
"lin-proc",
|
|
97
|
+
"shaded-process"
|
|
98
|
+
],
|
|
99
|
+
"sm-circ": [
|
|
100
|
+
"start",
|
|
101
|
+
"small-circle",
|
|
102
|
+
"stateStart"
|
|
103
|
+
],
|
|
104
|
+
"fr-circ": [
|
|
105
|
+
"stop",
|
|
106
|
+
"framed-circle",
|
|
107
|
+
"stateEnd"
|
|
108
|
+
],
|
|
109
|
+
fork: ["join", "forkJoin"],
|
|
110
|
+
hourglass: ["collate"],
|
|
111
|
+
brace: ["comment", "brace-l"],
|
|
112
|
+
"brace-r": [],
|
|
113
|
+
braces: [],
|
|
114
|
+
bolt: ["com-link", "lightning-bolt"],
|
|
115
|
+
doc: ["document"],
|
|
116
|
+
delay: ["half-rounded-rectangle"],
|
|
117
|
+
"h-cyl": ["das", "horizontal-cylinder"],
|
|
118
|
+
"lin-cyl": ["disk", "lined-cylinder"],
|
|
119
|
+
"curv-trap": ["curved-trapezoid", "display"],
|
|
120
|
+
"div-rect": [
|
|
121
|
+
"div-proc",
|
|
122
|
+
"divided-rectangle",
|
|
123
|
+
"divided-process"
|
|
124
|
+
],
|
|
125
|
+
tri: ["extract", "triangle"],
|
|
126
|
+
"win-pane": ["internal-storage", "window-pane"],
|
|
127
|
+
"f-circ": ["junction", "filled-circle"],
|
|
128
|
+
"notch-pent": ["loop-limit", "notched-pentagon"],
|
|
129
|
+
"flip-tri": ["manual-file", "flipped-triangle"],
|
|
130
|
+
"sl-rect": ["manual-input", "sloped-rectangle"],
|
|
131
|
+
docs: [
|
|
132
|
+
"documents",
|
|
133
|
+
"st-doc",
|
|
134
|
+
"stacked-document"
|
|
135
|
+
],
|
|
136
|
+
"st-rect": [
|
|
137
|
+
"procs",
|
|
138
|
+
"processes",
|
|
139
|
+
"stacked-rectangle"
|
|
140
|
+
],
|
|
141
|
+
"bow-rect": ["stored-data", "bow-tie-rectangle"],
|
|
142
|
+
"cross-circ": ["summary", "crossed-circle"],
|
|
143
|
+
"tag-doc": ["tagged-document"],
|
|
144
|
+
"tag-rect": [
|
|
145
|
+
"tagged-rectangle",
|
|
146
|
+
"tag-proc",
|
|
147
|
+
"tagged-process"
|
|
148
|
+
],
|
|
149
|
+
flag: ["paper-tape"],
|
|
150
|
+
odd: ["rect_left_inv_arrow"],
|
|
151
|
+
"lin-doc": ["lined-document"]
|
|
152
|
+
}).flatMap(([shape, aliases]) => [shape, ...aliases].map((name) => [name, shape])));
|
|
153
|
+
/** The shape a name from `@{ shape: … }` stands for, or null for one Mermaid does not know. */
|
|
154
|
+
function shapeNamed(name) {
|
|
155
|
+
return SHAPE_BY_NAME.get(name) ?? null;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The shape each bracket form stands for, by the vertex type Mermaid's grammar gives it: `[ ]` a
|
|
159
|
+
* rectangle, `( )` a rounded one, `([ ])` a stadium, and so on.
|
|
160
|
+
*/
|
|
161
|
+
const BRACKET_SHAPES = {
|
|
162
|
+
square: "rect",
|
|
163
|
+
rect: "rect",
|
|
164
|
+
round: "rounded",
|
|
165
|
+
ellipse: "ellipse",
|
|
166
|
+
stadium: "stadium",
|
|
167
|
+
subroutine: "fr-rect",
|
|
168
|
+
cylinder: "cyl",
|
|
169
|
+
circle: "circle",
|
|
170
|
+
doublecircle: "dbl-circ",
|
|
171
|
+
diamond: "diam",
|
|
172
|
+
hexagon: "hex",
|
|
173
|
+
odd: "odd",
|
|
174
|
+
trapezoid: "trap-b",
|
|
175
|
+
inv_trapezoid: "trap-t",
|
|
176
|
+
lean_right: "lean-r",
|
|
177
|
+
lean_left: "lean-l"
|
|
178
|
+
};
|
|
179
|
+
function bracketShape(vertexType) {
|
|
180
|
+
return BRACKET_SHAPES[vertexType] ?? "rect";
|
|
181
|
+
}
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region src/flowchart/builder.ts
|
|
184
|
+
const STROKES = {
|
|
185
|
+
normal: "normal",
|
|
186
|
+
thick: "thick",
|
|
187
|
+
dotted: "dotted",
|
|
188
|
+
invisible: "invisible"
|
|
189
|
+
};
|
|
190
|
+
const ARROWHEADS = {
|
|
191
|
+
arrow_point: "arrow",
|
|
192
|
+
arrow_circle: "circle",
|
|
193
|
+
arrow_cross: "cross",
|
|
194
|
+
arrow_open: "none"
|
|
195
|
+
};
|
|
196
|
+
/** The arrowhead a link's first character opens it with, as in `<-->` or `o--o`. */
|
|
197
|
+
const START_HEADS = {
|
|
198
|
+
"<": "arrow_point",
|
|
199
|
+
x: "arrow_cross",
|
|
200
|
+
o: "arrow_circle"
|
|
201
|
+
};
|
|
202
|
+
/** The arrowhead a link's last character closes it with, and the one that opens it as well. */
|
|
203
|
+
const END_HEADS = /* @__PURE__ */ new Map([
|
|
204
|
+
["x", {
|
|
205
|
+
type: "arrow_cross",
|
|
206
|
+
opening: "x"
|
|
207
|
+
}],
|
|
208
|
+
[">", {
|
|
209
|
+
type: "arrow_point",
|
|
210
|
+
opening: "<"
|
|
211
|
+
}],
|
|
212
|
+
["o", {
|
|
213
|
+
type: "arrow_circle",
|
|
214
|
+
opening: "o"
|
|
215
|
+
}]
|
|
216
|
+
]);
|
|
217
|
+
/** Mermaid's longest edge: more dashes than this count as this many. */
|
|
218
|
+
const MAX_EDGE_LENGTH = 10;
|
|
219
|
+
function direction(text) {
|
|
220
|
+
const dir = text.trim();
|
|
221
|
+
if (dir.includes("<")) return "RL";
|
|
222
|
+
if (dir.includes("^")) return "BT";
|
|
223
|
+
if (dir.includes(">")) return "LR";
|
|
224
|
+
if (dir.includes("v")) return "TB";
|
|
225
|
+
if (dir === "BT" || dir === "LR" || dir === "RL") return dir;
|
|
226
|
+
return "TB";
|
|
227
|
+
}
|
|
228
|
+
function isDirectionStatement(item) {
|
|
229
|
+
return typeof item === "object" && item !== null && "stmt" in item && item.stmt === "dir";
|
|
230
|
+
}
|
|
231
|
+
/** What `@{ … }` data sets that a node keeps; other keys, such as `icon`, are left out. */
|
|
232
|
+
const shapeDataSchema = v.looseObject({
|
|
233
|
+
shape: v.optional(v.string()),
|
|
234
|
+
label: v.optional(v.pipe(v.union([v.string(), v.number()]), v.toString())),
|
|
235
|
+
labelType: v.optional(v.string())
|
|
236
|
+
});
|
|
237
|
+
/** `@{ … }` data, which is YAML, written as a flow mapping when it fits on one line. */
|
|
238
|
+
function shapeData(text) {
|
|
239
|
+
const data = parse(text.includes("\n") ? `${text}\n` : `{\n${text}\n}`);
|
|
240
|
+
const parsed = v.safeParse(shapeDataSchema, data);
|
|
241
|
+
return parsed.success ? parsed.output : {};
|
|
242
|
+
}
|
|
243
|
+
function arrowheads(type) {
|
|
244
|
+
if (type.startsWith("double_")) {
|
|
245
|
+
const head = ARROWHEADS[type.slice(7)] ?? "arrow";
|
|
246
|
+
return {
|
|
247
|
+
startArrow: head,
|
|
248
|
+
endArrow: head
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
startArrow: "none",
|
|
253
|
+
endArrow: ARROWHEADS[type] ?? "arrow"
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* The `yy` object Mermaid's flowchart grammar calls as it parses, building a `Flowchart`. It
|
|
258
|
+
* keeps the meaning Mermaid's `FlowDB` gives each call in the pinned release (MIT, Copyright (c)
|
|
259
|
+
* 2014 - 2022 Knut Sveidqvist), without what only Mermaid's renderer needs: DOM ids, click
|
|
260
|
+
* callbacks, sanitizing for the page, and edge animation.
|
|
261
|
+
*/
|
|
262
|
+
var FlowchartBuilder = class {
|
|
263
|
+
dir = "TB";
|
|
264
|
+
nodes = /* @__PURE__ */ new Map();
|
|
265
|
+
edges = [];
|
|
266
|
+
subgraphs = [];
|
|
267
|
+
classDefs = /* @__PURE__ */ new Map();
|
|
268
|
+
defaultEdgeStyle = [];
|
|
269
|
+
subgraphCount = 0;
|
|
270
|
+
firstGraphSeen = false;
|
|
271
|
+
accessibleTitle;
|
|
272
|
+
accessibleDescription;
|
|
273
|
+
lex = {
|
|
274
|
+
/** True for the first `graph`/`flowchart` keyword only; a later one is text. */
|
|
275
|
+
firstGraph: () => {
|
|
276
|
+
if (this.firstGraphSeen) return false;
|
|
277
|
+
this.firstGraphSeen = true;
|
|
278
|
+
return true;
|
|
279
|
+
} };
|
|
280
|
+
/**
|
|
281
|
+
* The calls the grammar makes, as the plain object it copies them from: JISON copies `yy`'s
|
|
282
|
+
* enumerable properties into its own state, which leaves out a class's methods.
|
|
283
|
+
*/
|
|
284
|
+
grammarCalls() {
|
|
285
|
+
return {
|
|
286
|
+
lex: this.lex,
|
|
287
|
+
setDirection: this.setDirection.bind(this),
|
|
288
|
+
setAccTitle: this.setAccTitle.bind(this),
|
|
289
|
+
setAccDescription: this.setAccDescription.bind(this),
|
|
290
|
+
addVertex: this.addVertex.bind(this),
|
|
291
|
+
destructLink: this.destructLink.bind(this),
|
|
292
|
+
addLink: this.addLink.bind(this),
|
|
293
|
+
updateLinkInterpolate: this.updateLinkInterpolate.bind(this),
|
|
294
|
+
updateLink: this.updateLink.bind(this),
|
|
295
|
+
addClass: this.addClass.bind(this),
|
|
296
|
+
setClass: this.setClass.bind(this),
|
|
297
|
+
setLink: this.setLink.bind(this),
|
|
298
|
+
setTooltip: this.setTooltip.bind(this),
|
|
299
|
+
setClickEvent: this.setClickEvent.bind(this),
|
|
300
|
+
addSubGraph: this.addSubGraph.bind(this)
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
result(title) {
|
|
304
|
+
const subgraphIds = new Set(this.subgraphs.map((subgraph) => subgraph.id));
|
|
305
|
+
for (const subgraph of this.subgraphs) {
|
|
306
|
+
const node = this.nodes.get(subgraph.id);
|
|
307
|
+
if (!node) continue;
|
|
308
|
+
subgraph.styles.push(...node.styles);
|
|
309
|
+
subgraph.classes.push(...node.classes);
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
direction: this.dir,
|
|
313
|
+
nodes: [...this.nodes.values()].filter((node) => !subgraphIds.has(node.id)),
|
|
314
|
+
edges: this.edges.map((edge) => ({
|
|
315
|
+
...edge,
|
|
316
|
+
styles: [...this.defaultEdgeStyle, ...edge.styles]
|
|
317
|
+
})),
|
|
318
|
+
subgraphs: this.subgraphs,
|
|
319
|
+
classDefs: this.classDefs,
|
|
320
|
+
title,
|
|
321
|
+
accessibleTitle: this.accessibleTitle,
|
|
322
|
+
accessibleDescription: this.accessibleDescription
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
setDirection(dir) {
|
|
326
|
+
this.dir = direction(dir);
|
|
327
|
+
}
|
|
328
|
+
setAccTitle(text) {
|
|
329
|
+
this.accessibleTitle = text;
|
|
330
|
+
}
|
|
331
|
+
setAccDescription(text) {
|
|
332
|
+
this.accessibleDescription = text;
|
|
333
|
+
}
|
|
334
|
+
addVertex(id, text, type, styles, classes, _dir, _props, metadata) {
|
|
335
|
+
if (!id || id.trim().length === 0) return;
|
|
336
|
+
const data = metadata === void 0 ? void 0 : shapeData(metadata);
|
|
337
|
+
if (this.edges.some((edge) => edge.id === id) || data && this.isSubgraph(id)) return;
|
|
338
|
+
let node = this.nodes.get(id);
|
|
339
|
+
if (!node) {
|
|
340
|
+
node = {
|
|
341
|
+
id,
|
|
342
|
+
label: {
|
|
343
|
+
text: id,
|
|
344
|
+
format: "text"
|
|
345
|
+
},
|
|
346
|
+
shape: "rect",
|
|
347
|
+
classes: [],
|
|
348
|
+
styles: []
|
|
349
|
+
};
|
|
350
|
+
this.nodes.set(id, node);
|
|
351
|
+
}
|
|
352
|
+
if (text) node.label = readLabel(text);
|
|
353
|
+
if (type !== void 0) node.shape = bracketShape(type);
|
|
354
|
+
if (styles) node.styles.push(...styles);
|
|
355
|
+
if (classes) node.classes.push(...classes);
|
|
356
|
+
if (!data) return;
|
|
357
|
+
if (typeof data.shape === "string") {
|
|
358
|
+
const shape = data.shape === data.shape.toLowerCase() ? shapeNamed(data.shape) : null;
|
|
359
|
+
if (!shape) throw new Error(`No such shape: ${data.shape}.`);
|
|
360
|
+
node.shape = shape;
|
|
361
|
+
}
|
|
362
|
+
if (typeof data.label === "string") node.label = readLabel({
|
|
363
|
+
text: data.label,
|
|
364
|
+
type: data.labelType === "text" || data.labelType === "string" ? "text" : "markdown"
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
destructLink(end, start) {
|
|
368
|
+
const info = this.endOfLink(end);
|
|
369
|
+
if (!start) return info;
|
|
370
|
+
const begin = this.startOfLink(start);
|
|
371
|
+
if (begin.stroke !== info.stroke) return {
|
|
372
|
+
type: "INVALID",
|
|
373
|
+
stroke: "INVALID"
|
|
374
|
+
};
|
|
375
|
+
if (begin.type === "arrow_open") return {
|
|
376
|
+
...info,
|
|
377
|
+
type: info.type
|
|
378
|
+
};
|
|
379
|
+
if (begin.type !== info.type) return {
|
|
380
|
+
type: "INVALID",
|
|
381
|
+
stroke: "INVALID"
|
|
382
|
+
};
|
|
383
|
+
return {
|
|
384
|
+
...info,
|
|
385
|
+
type: `double_${begin.type}`
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
addLink(starts, ends, link) {
|
|
389
|
+
const id = link.id?.replace("@", "");
|
|
390
|
+
for (const from of starts) for (const to of ends) {
|
|
391
|
+
const named = from === starts.at(-1) && to === ends[0];
|
|
392
|
+
this.addEdge(from, to, link, named ? id : void 0);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
updateLinkInterpolate() {}
|
|
396
|
+
updateLink(positions, styles) {
|
|
397
|
+
for (const position of positions) {
|
|
398
|
+
if (position === "default") {
|
|
399
|
+
this.defaultEdgeStyle = styles;
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
const edge = this.edges.at(position);
|
|
403
|
+
if (!edge || position < 0) throw new Error(`The index ${position} for linkStyle is out of bounds. Valid indices for linkStyle are between 0 and ${this.edges.length - 1}.`);
|
|
404
|
+
edge.styles = styles;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
addClass(ids, styles) {
|
|
408
|
+
const declarations = styles.join(",").split(/(?<!\\)[,;]/).map((declaration) => declaration.replaceAll("\\,", ","));
|
|
409
|
+
for (const id of ids.split(",")) {
|
|
410
|
+
const existing = this.classDefs.get(id) ?? [];
|
|
411
|
+
this.classDefs.set(id, [...existing, ...declarations]);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
setClass(ids, className) {
|
|
415
|
+
for (const id of ids.split(",")) {
|
|
416
|
+
const subgraph = this.subgraphs.find((candidate) => candidate.id === id);
|
|
417
|
+
if (subgraph) subgraph.classes.push(className);
|
|
418
|
+
else this.nodes.get(id)?.classes.push(className);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
setLink(ids, href, target) {
|
|
422
|
+
for (const id of ids.split(",")) {
|
|
423
|
+
const node = this.nodes.get(id);
|
|
424
|
+
if (node) node.link = target ? {
|
|
425
|
+
href,
|
|
426
|
+
target
|
|
427
|
+
} : { href };
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
setTooltip(ids, tooltip) {
|
|
431
|
+
if (tooltip === void 0) return;
|
|
432
|
+
for (const id of ids.split(",")) {
|
|
433
|
+
const node = this.nodes.get(id);
|
|
434
|
+
if (node) node.tooltip = tooltip;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
setClickEvent() {}
|
|
438
|
+
addSubGraph(idText, list, titleText) {
|
|
439
|
+
let id = idText?.text.trim();
|
|
440
|
+
if (idText && idText === titleText && /\s/.test(idText.text)) id = void 0;
|
|
441
|
+
let dir;
|
|
442
|
+
const members = [];
|
|
443
|
+
for (const item of list.flat()) {
|
|
444
|
+
if (isDirectionStatement(item)) {
|
|
445
|
+
dir = direction(item.value);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (typeof item !== "string" || item.trim() === "" || members.includes(item)) continue;
|
|
449
|
+
members.push(item);
|
|
450
|
+
}
|
|
451
|
+
const subgraphId = id ?? `subGraph${this.subgraphCount}`;
|
|
452
|
+
this.subgraphCount++;
|
|
453
|
+
const own = members.filter((member) => member !== subgraphId && !this.subgraphs.some((subgraph) => subgraph.members.includes(member)));
|
|
454
|
+
const existing = this.subgraphs.find((subgraph) => subgraph.id === subgraphId);
|
|
455
|
+
if (existing) {
|
|
456
|
+
existing.members.push(...own);
|
|
457
|
+
return subgraphId;
|
|
458
|
+
}
|
|
459
|
+
this.subgraphs.push({
|
|
460
|
+
id: subgraphId,
|
|
461
|
+
title: titleText ? readLabel(titleText) : {
|
|
462
|
+
text: "",
|
|
463
|
+
format: "text"
|
|
464
|
+
},
|
|
465
|
+
members: own,
|
|
466
|
+
direction: dir,
|
|
467
|
+
classes: [],
|
|
468
|
+
styles: []
|
|
469
|
+
});
|
|
470
|
+
return subgraphId;
|
|
471
|
+
}
|
|
472
|
+
isSubgraph(id) {
|
|
473
|
+
return this.subgraphs.some((subgraph) => subgraph.id === id);
|
|
474
|
+
}
|
|
475
|
+
addEdge(from, to, link, id) {
|
|
476
|
+
const label = link.text && readLabel(link.text);
|
|
477
|
+
const edgeId = id && !this.edges.some((edge) => edge.id === id) ? id : `L_${from}_${to}_${this.edges.filter((edge) => edge.from === from && edge.to === to).length}`;
|
|
478
|
+
this.edges.push({
|
|
479
|
+
id: edgeId,
|
|
480
|
+
from,
|
|
481
|
+
to,
|
|
482
|
+
...label && label.text !== "" ? { label } : {},
|
|
483
|
+
...arrowheads(link.type),
|
|
484
|
+
stroke: STROKES[link.stroke] ?? "normal",
|
|
485
|
+
length: Math.min(link.length ?? 1, MAX_EDGE_LENGTH),
|
|
486
|
+
styles: []
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
startOfLink(text) {
|
|
490
|
+
let rest = text.trim();
|
|
491
|
+
let type = "arrow_open";
|
|
492
|
+
const head = rest[0];
|
|
493
|
+
const opening = head && START_HEADS[head];
|
|
494
|
+
if (opening) {
|
|
495
|
+
type = opening;
|
|
496
|
+
rest = rest.slice(1);
|
|
497
|
+
}
|
|
498
|
+
let stroke = "normal";
|
|
499
|
+
if (rest.includes("=")) stroke = "thick";
|
|
500
|
+
if (rest.includes(".")) stroke = "dotted";
|
|
501
|
+
return {
|
|
502
|
+
type,
|
|
503
|
+
stroke
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
endOfLink(text) {
|
|
507
|
+
const link = text.trim();
|
|
508
|
+
let line = link.slice(0, -1);
|
|
509
|
+
let type = "arrow_open";
|
|
510
|
+
const tail = link.slice(-1);
|
|
511
|
+
const head = END_HEADS.get(tail);
|
|
512
|
+
if (head) {
|
|
513
|
+
type = head.type;
|
|
514
|
+
if (link.startsWith(head.opening)) {
|
|
515
|
+
type = `double_${type}`;
|
|
516
|
+
line = line.slice(1);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
let stroke = "normal";
|
|
520
|
+
let length = line.length - 1;
|
|
521
|
+
if (line.startsWith("=")) stroke = "thick";
|
|
522
|
+
if (line.startsWith("~")) stroke = "invisible";
|
|
523
|
+
const dots = line.split(".").length - 1;
|
|
524
|
+
if (dots) {
|
|
525
|
+
stroke = "dotted";
|
|
526
|
+
length = dots;
|
|
527
|
+
}
|
|
528
|
+
return {
|
|
529
|
+
type,
|
|
530
|
+
stroke,
|
|
531
|
+
length
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
//#endregion
|
|
536
|
+
export { FlowchartBuilder };
|
|
537
|
+
|
|
538
|
+
//# sourceMappingURL=builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.js","names":["parseYAML"],"sources":["../../../src/label.ts","../../../src/flowchart/shapes.ts","../../../src/flowchart/builder.ts"],"sourcesContent":["import { decodeHTML } from 'entities'\nimport { parseFragment, type DefaultTreeAdapterTypes } from 'parse5'\n\nimport type { FlowLabel } from './flowchart/model'\nimport { decodeEntities } from './upstream/preprocess'\n\n/** A label as Mermaid's grammar hands it over: its text, and how that text is to be read. */\nexport interface GrammarText {\n text: string\n type: string\n}\n\ntype HTMLNode = DefaultTreeAdapterTypes.ChildNode\n\n/** The text HTML shows: its text nodes, and a line break for each `<br>`. */\nfunction shownText(nodes: HTMLNode[]): string {\n return nodes\n .map((node) => {\n if (node.nodeName === '#text' && 'value' in node) return node.value\n if (node.nodeName === 'br') return '\\n'\n return 'childNodes' in node ? shownText(node.childNodes) : ''\n })\n .join('')\n}\n\n/**\n * A label's text as plain text and its format. Mermaid draws a label as HTML, so its tags only\n * style the text, `<br>` breaks the line, and entities stand for characters. Markdown, written\n * in backticks, keeps its marks for whoever draws it.\n */\nexport function readLabel(label: GrammarText): FlowLabel {\n let text = decodeEntities(label.text.trim())\n // Mermaid drops the quotes around a label written in them.\n if (text.length >= 2 && text.startsWith('\"') && text.endsWith('\"')) text = text.slice(1, -1)\n if (label.type === 'markdown') return { text: decodeHTML(text), format: 'markdown' }\n return { text: shownText(parseFragment(text).childNodes), format: 'text' }\n}\n","/**\n * Mermaid's flowchart shapes by their short names, with the other names `@{ shape: … }` accepts\n * for each, from `rendering-util/rendering-elements/shapes.ts` in the pinned Mermaid release\n * (MIT, Copyright (c) 2014 - 2022 Knut Sveidqvist). `bun run generate:mermaid-sources --check`\n * reports when a newer release changes that file.\n */\nconst SHAPE_NAMES = {\n rect: ['proc', 'process', 'rectangle', 'squareRect'],\n rounded: ['event', 'roundedRect'],\n stadium: ['terminal', 'pill'],\n 'fr-rect': ['subprocess', 'subproc', 'framed-rectangle', 'subroutine'],\n cyl: ['db', 'database', 'cylinder'],\n datastore: ['data-store'],\n folder: ['directory'],\n bucket: [],\n console: [],\n browser: [],\n person: [],\n circle: ['circ'],\n bang: [],\n cloud: [],\n diam: ['decision', 'diamond', 'question'],\n hex: ['hexagon', 'prepare'],\n 'lean-r': ['lean-right', 'in-out', 'lean_right'],\n 'lean-l': ['lean-left', 'out-in', 'lean_left'],\n 'trap-b': ['priority', 'trapezoid-bottom', 'trapezoid'],\n 'trap-t': ['manual', 'trapezoid-top', 'inv-trapezoid', 'inv_trapezoid'],\n 'dbl-circ': ['double-circle', 'doublecircle'],\n text: [],\n 'notch-rect': ['card', 'notched-rectangle'],\n 'lin-rect': ['lined-rectangle', 'lined-process', 'lin-proc', 'shaded-process'],\n 'sm-circ': ['start', 'small-circle', 'stateStart'],\n 'fr-circ': ['stop', 'framed-circle', 'stateEnd'],\n fork: ['join', 'forkJoin'],\n hourglass: ['collate'],\n brace: ['comment', 'brace-l'],\n 'brace-r': [],\n braces: [],\n bolt: ['com-link', 'lightning-bolt'],\n doc: ['document'],\n delay: ['half-rounded-rectangle'],\n 'h-cyl': ['das', 'horizontal-cylinder'],\n 'lin-cyl': ['disk', 'lined-cylinder'],\n 'curv-trap': ['curved-trapezoid', 'display'],\n 'div-rect': ['div-proc', 'divided-rectangle', 'divided-process'],\n tri: ['extract', 'triangle'],\n 'win-pane': ['internal-storage', 'window-pane'],\n 'f-circ': ['junction', 'filled-circle'],\n 'notch-pent': ['loop-limit', 'notched-pentagon'],\n 'flip-tri': ['manual-file', 'flipped-triangle'],\n 'sl-rect': ['manual-input', 'sloped-rectangle'],\n docs: ['documents', 'st-doc', 'stacked-document'],\n 'st-rect': ['procs', 'processes', 'stacked-rectangle'],\n 'bow-rect': ['stored-data', 'bow-tie-rectangle'],\n 'cross-circ': ['summary', 'crossed-circle'],\n 'tag-doc': ['tagged-document'],\n 'tag-rect': ['tagged-rectangle', 'tag-proc', 'tagged-process'],\n flag: ['paper-tape'],\n odd: ['rect_left_inv_arrow'],\n 'lin-doc': ['lined-document']\n} as const satisfies Record<string, readonly string[]>\n\n/**\n * A flowchart node's shape, by Mermaid's short name for it, or `ellipse` for `A(-text-)`, which\n * Mermaid's grammar reads but its renderer refuses (mermaid-js/mermaid#5976).\n */\nexport type FlowShape = keyof typeof SHAPE_NAMES | 'ellipse'\n\nconst SHAPE_BY_NAME = new Map<string, FlowShape>(\n Object.entries(SHAPE_NAMES).flatMap(([shape, aliases]) =>\n [shape, ...aliases].map((name) => [name, shape as FlowShape] as const)\n )\n)\n\n/** The shape a name from `@{ shape: … }` stands for, or null for one Mermaid does not know. */\nexport function shapeNamed(name: string): FlowShape | null {\n return SHAPE_BY_NAME.get(name) ?? null\n}\n\n/**\n * The shape each bracket form stands for, by the vertex type Mermaid's grammar gives it: `[ ]` a\n * rectangle, `( )` a rounded one, `([ ])` a stadium, and so on.\n */\nconst BRACKET_SHAPES: Record<string, FlowShape> = {\n square: 'rect',\n rect: 'rect',\n round: 'rounded',\n ellipse: 'ellipse',\n stadium: 'stadium',\n subroutine: 'fr-rect',\n cylinder: 'cyl',\n circle: 'circle',\n doublecircle: 'dbl-circ',\n diamond: 'diam',\n hexagon: 'hex',\n odd: 'odd',\n trapezoid: 'trap-b',\n inv_trapezoid: 'trap-t',\n lean_right: 'lean-r',\n lean_left: 'lean-l'\n}\n\nexport function bracketShape(vertexType: string): FlowShape {\n return BRACKET_SHAPES[vertexType] ?? 'rect'\n}\n","import * as v from 'valibot'\nimport { parse as parseYAML } from 'yaml'\n\nimport { readLabel, type GrammarText } from '../label'\nimport type {\n FlowArrowhead,\n FlowDirection,\n FlowEdge,\n FlowNode,\n FlowStroke,\n FlowSubgraph,\n Flowchart\n} from './model'\nimport { bracketShape, shapeNamed } from './shapes'\n\n/** An edge as the grammar describes it before its ends are known. */\ninterface GrammarLink {\n type: string\n stroke: string\n length?: number\n text?: GrammarText\n id?: string\n}\n\n/** A `direction` statement inside a subgraph, as the grammar lists it among the members. */\ninterface DirectionStatement {\n stmt: 'dir'\n value: string\n}\n\nconst STROKES: Record<string, FlowStroke> = {\n normal: 'normal',\n thick: 'thick',\n dotted: 'dotted',\n invisible: 'invisible'\n}\n\nconst ARROWHEADS: Record<string, FlowArrowhead> = {\n arrow_point: 'arrow',\n arrow_circle: 'circle',\n arrow_cross: 'cross',\n arrow_open: 'none'\n}\n\n/** The arrowhead a link's first character opens it with, as in `<-->` or `o--o`. */\nconst START_HEADS: Record<string, string> = {\n '<': 'arrow_point',\n x: 'arrow_cross',\n o: 'arrow_circle'\n}\n\n/** The arrowhead a link's last character closes it with, and the one that opens it as well. */\nconst END_HEADS = new Map([\n ['x', { type: 'arrow_cross', opening: 'x' }],\n ['>', { type: 'arrow_point', opening: '<' }],\n ['o', { type: 'arrow_circle', opening: 'o' }]\n])\n\n/** Mermaid's longest edge: more dashes than this count as this many. */\nconst MAX_EDGE_LENGTH = 10\n\nfunction direction(text: string): FlowDirection {\n const dir = text.trim()\n if (dir.includes('<')) return 'RL'\n if (dir.includes('^')) return 'BT'\n if (dir.includes('>')) return 'LR'\n if (dir.includes('v')) return 'TB'\n if (dir === 'BT' || dir === 'LR' || dir === 'RL') return dir\n return 'TB'\n}\n\nfunction isDirectionStatement(item: unknown): item is DirectionStatement {\n return typeof item === 'object' && item !== null && 'stmt' in item && item.stmt === 'dir'\n}\n\n/** What `@{ … }` data sets that a node keeps; other keys, such as `icon`, are left out. */\nconst shapeDataSchema = v.looseObject({\n shape: v.optional(v.string()),\n label: v.optional(v.pipe(v.union([v.string(), v.number()]), v.toString())),\n labelType: v.optional(v.string())\n})\n\ntype ShapeData = v.InferOutput<typeof shapeDataSchema>\n\n/** `@{ … }` data, which is YAML, written as a flow mapping when it fits on one line. */\nfunction shapeData(text: string): ShapeData {\n const data: unknown = parseYAML(text.includes('\\n') ? `${text}\\n` : `{\\n${text}\\n}`)\n const parsed = v.safeParse(shapeDataSchema, data)\n return parsed.success ? parsed.output : {}\n}\n\nfunction arrowheads(type: string): { startArrow: FlowArrowhead; endArrow: FlowArrowhead } {\n if (type.startsWith('double_')) {\n const head = ARROWHEADS[type.slice('double_'.length)] ?? 'arrow'\n return { startArrow: head, endArrow: head }\n }\n return { startArrow: 'none', endArrow: ARROWHEADS[type] ?? 'arrow' }\n}\n\n/**\n * The `yy` object Mermaid's flowchart grammar calls as it parses, building a `Flowchart`. It\n * keeps the meaning Mermaid's `FlowDB` gives each call in the pinned release (MIT, Copyright (c)\n * 2014 - 2022 Knut Sveidqvist), without what only Mermaid's renderer needs: DOM ids, click\n * callbacks, sanitizing for the page, and edge animation.\n */\nexport class FlowchartBuilder {\n private dir: FlowDirection = 'TB'\n private readonly nodes = new Map<string, FlowNode>()\n private readonly edges: FlowEdge[] = []\n private readonly subgraphs: FlowSubgraph[] = []\n private readonly classDefs = new Map<string, string[]>()\n private defaultEdgeStyle: string[] = []\n private subgraphCount = 0\n private firstGraphSeen = false\n private accessibleTitle?: string\n private accessibleDescription?: string\n\n readonly lex = {\n /** True for the first `graph`/`flowchart` keyword only; a later one is text. */\n firstGraph: () => {\n if (this.firstGraphSeen) return false\n this.firstGraphSeen = true\n return true\n }\n }\n\n /**\n * The calls the grammar makes, as the plain object it copies them from: JISON copies `yy`'s\n * enumerable properties into its own state, which leaves out a class's methods.\n */\n grammarCalls() {\n return {\n lex: this.lex,\n setDirection: this.setDirection.bind(this),\n setAccTitle: this.setAccTitle.bind(this),\n setAccDescription: this.setAccDescription.bind(this),\n addVertex: this.addVertex.bind(this),\n destructLink: this.destructLink.bind(this),\n addLink: this.addLink.bind(this),\n updateLinkInterpolate: this.updateLinkInterpolate.bind(this),\n updateLink: this.updateLink.bind(this),\n addClass: this.addClass.bind(this),\n setClass: this.setClass.bind(this),\n setLink: this.setLink.bind(this),\n setTooltip: this.setTooltip.bind(this),\n setClickEvent: this.setClickEvent.bind(this),\n addSubGraph: this.addSubGraph.bind(this)\n }\n }\n\n result(title?: string): Flowchart {\n // `style` and `class` statements naming a subgraph style the subgraph, though the grammar\n // reports them as nodes.\n const subgraphIds = new Set(this.subgraphs.map((subgraph) => subgraph.id))\n for (const subgraph of this.subgraphs) {\n const node = this.nodes.get(subgraph.id)\n if (!node) continue\n subgraph.styles.push(...node.styles)\n subgraph.classes.push(...node.classes)\n }\n return {\n direction: this.dir,\n nodes: [...this.nodes.values()].filter((node) => !subgraphIds.has(node.id)),\n edges: this.edges.map((edge) => ({\n ...edge,\n styles: [...this.defaultEdgeStyle, ...edge.styles]\n })),\n subgraphs: this.subgraphs,\n classDefs: this.classDefs,\n title,\n accessibleTitle: this.accessibleTitle,\n accessibleDescription: this.accessibleDescription\n }\n }\n\n setDirection(dir: string): void {\n this.dir = direction(dir)\n }\n\n setAccTitle(text: string): void {\n this.accessibleTitle = text\n }\n\n setAccDescription(text: string): void {\n this.accessibleDescription = text\n }\n\n addVertex(\n id: string,\n text?: GrammarText,\n type?: string,\n styles?: string[],\n classes?: string[],\n _dir?: string,\n _props?: Record<string, unknown>,\n metadata?: string\n ): void {\n if (!id || id.trim().length === 0) return\n const data = metadata === undefined ? undefined : shapeData(metadata)\n // An edge's id, or `@{ … }` after a subgraph's, names that edge or subgraph, not a node;\n // the data it carries is animation and the like, which nothing here draws.\n if (this.edges.some((edge) => edge.id === id) || (data && this.isSubgraph(id))) return\n let node = this.nodes.get(id)\n if (!node) {\n node = {\n id,\n label: { text: id, format: 'text' },\n shape: 'rect',\n classes: [],\n styles: []\n }\n this.nodes.set(id, node)\n }\n if (text) node.label = readLabel(text)\n if (type !== undefined) node.shape = bracketShape(type)\n if (styles) node.styles.push(...styles)\n if (classes) node.classes.push(...classes)\n if (!data) return\n if (typeof data.shape === 'string') {\n const shape = data.shape === data.shape.toLowerCase() ? shapeNamed(data.shape) : null\n if (!shape) throw new Error(`No such shape: ${data.shape}.`)\n node.shape = shape\n }\n if (typeof data.label === 'string') {\n node.label = readLabel({\n text: data.label,\n type: data.labelType === 'text' || data.labelType === 'string' ? 'text' : 'markdown'\n })\n }\n }\n\n destructLink(end: string, start?: string): GrammarLink {\n const info = this.endOfLink(end)\n if (!start) return info\n const begin = this.startOfLink(start)\n if (begin.stroke !== info.stroke) return { type: 'INVALID', stroke: 'INVALID' }\n if (begin.type === 'arrow_open') return { ...info, type: info.type }\n if (begin.type !== info.type) return { type: 'INVALID', stroke: 'INVALID' }\n return { ...info, type: `double_${begin.type}` }\n }\n\n addLink(starts: string[], ends: string[], link: GrammarLink): void {\n const id = link.id?.replace('@', '')\n for (const from of starts) {\n for (const to of ends) {\n // In `A e1@--> B & C`, the id names the edge from the last start to the first end.\n const named = from === starts.at(-1) && to === ends[0]\n this.addEdge(from, to, link, named ? id : undefined)\n }\n }\n }\n\n updateLinkInterpolate(): void {\n // Curve styles are Mermaid's renderer's; connectors take their own line type.\n }\n\n updateLink(positions: ('default' | number)[], styles: string[]): void {\n for (const position of positions) {\n if (position === 'default') {\n this.defaultEdgeStyle = styles\n continue\n }\n const edge = this.edges.at(position)\n if (!edge || position < 0) {\n throw new Error(\n `The index ${position} for linkStyle is out of bounds. Valid indices for linkStyle are between 0 and ${this.edges.length - 1}.`\n )\n }\n edge.styles = styles\n }\n }\n\n addClass(ids: string, styles: string[]): void {\n // Commas and semicolons separate declarations; an escaped comma, as in `font-family:a\\,b`,\n // is part of one.\n const declarations = styles\n .join(',')\n .split(/(?<!\\\\)[,;]/)\n .map((declaration) => declaration.replaceAll('\\\\,', ','))\n for (const id of ids.split(',')) {\n const existing = this.classDefs.get(id) ?? []\n this.classDefs.set(id, [...existing, ...declarations])\n }\n }\n\n setClass(ids: string, className: string): void {\n for (const id of ids.split(',')) {\n const subgraph = this.subgraphs.find((candidate) => candidate.id === id)\n if (subgraph) subgraph.classes.push(className)\n else this.nodes.get(id)?.classes.push(className)\n }\n }\n\n setLink(ids: string, href: string, target?: string): void {\n for (const id of ids.split(',')) {\n const node = this.nodes.get(id)\n if (node) node.link = target ? { href, target } : { href }\n }\n }\n\n setTooltip(ids: string, tooltip?: string): void {\n if (tooltip === undefined) return\n for (const id of ids.split(',')) {\n const node = this.nodes.get(id)\n if (node) node.tooltip = tooltip\n }\n }\n\n setClickEvent(): void {\n // Callbacks run scripts on Mermaid's page; a design layer has none to run.\n }\n\n addSubGraph(\n idText: GrammarText | undefined,\n list: unknown[],\n titleText: GrammarText | undefined\n ): string {\n // `subgraph Some title` gives the title as the id too; an id with spaces is no id.\n let id = idText?.text.trim()\n if (idText && idText === titleText && /\\s/.test(idText.text)) id = undefined\n let dir: FlowDirection | undefined\n const members: string[] = []\n for (const item of list.flat()) {\n if (isDirectionStatement(item)) {\n dir = direction(item.value)\n continue\n }\n if (typeof item !== 'string' || item.trim() === '' || members.includes(item)) continue\n members.push(item)\n }\n const subgraphId = id ?? `subGraph${this.subgraphCount}`\n this.subgraphCount++\n // A node belongs to the subgraph that closes first, which is the innermost one, and a\n // subgraph never holds itself.\n const own = members.filter(\n (member) =>\n member !== subgraphId &&\n !this.subgraphs.some((subgraph) => subgraph.members.includes(member))\n )\n const existing = this.subgraphs.find((subgraph) => subgraph.id === subgraphId)\n if (existing) {\n // A repeated id adds to the first declaration, which keeps its title and direction.\n existing.members.push(...own)\n return subgraphId\n }\n this.subgraphs.push({\n id: subgraphId,\n title: titleText ? readLabel(titleText) : { text: '', format: 'text' },\n members: own,\n direction: dir,\n classes: [],\n styles: []\n })\n return subgraphId\n }\n\n private isSubgraph(id: string): boolean {\n return this.subgraphs.some((subgraph) => subgraph.id === id)\n }\n\n private addEdge(from: string, to: string, link: GrammarLink, id?: string): void {\n const label = link.text && readLabel(link.text)\n const edgeId =\n id && !this.edges.some((edge) => edge.id === id)\n ? id\n : `L_${from}_${to}_${this.edges.filter((edge) => edge.from === from && edge.to === to).length}`\n this.edges.push({\n id: edgeId,\n from,\n to,\n ...(label && label.text !== '' ? { label } : {}),\n ...arrowheads(link.type),\n stroke: STROKES[link.stroke] ?? 'normal',\n length: Math.min(link.length ?? 1, MAX_EDGE_LENGTH),\n styles: []\n })\n }\n\n private startOfLink(text: string): { type: string; stroke: string } {\n let rest = text.trim()\n let type = 'arrow_open'\n const head = rest[0]\n const opening = head && START_HEADS[head]\n if (opening) {\n type = opening\n rest = rest.slice(1)\n }\n let stroke = 'normal'\n if (rest.includes('=')) stroke = 'thick'\n if (rest.includes('.')) stroke = 'dotted'\n return { type, stroke }\n }\n\n private endOfLink(text: string): GrammarLink {\n const link = text.trim()\n let line = link.slice(0, -1)\n let type = 'arrow_open'\n const tail = link.slice(-1)\n const head = END_HEADS.get(tail)\n if (head) {\n type = head.type\n if (link.startsWith(head.opening)) {\n type = `double_${type}`\n line = line.slice(1)\n }\n }\n let stroke = 'normal'\n let length = line.length - 1\n if (line.startsWith('=')) stroke = 'thick'\n if (line.startsWith('~')) stroke = 'invisible'\n const dots = line.split('.').length - 1\n if (dots) {\n stroke = 'dotted'\n length = dots\n }\n return { type, stroke, length }\n }\n}\n"],"mappings":";;;;;;;AAeA,SAAS,UAAU,OAA2B;CAC5C,OAAO,MACJ,KAAK,SAAS;EACb,IAAI,KAAK,aAAa,WAAW,WAAW,MAAM,OAAO,KAAK;EAC9D,IAAI,KAAK,aAAa,MAAM,OAAO;EACnC,OAAO,gBAAgB,OAAO,UAAU,KAAK,UAAU,IAAI;CAC7D,CAAC,CAAC,CACD,KAAK,EAAE;AACZ;;;;;;AAOA,SAAgB,UAAU,OAA+B;CACvD,IAAI,OAAO,eAAe,MAAM,KAAK,KAAK,CAAC;CAE3C,IAAI,KAAK,UAAU,KAAK,KAAK,WAAW,IAAG,KAAK,KAAK,SAAS,IAAG,GAAG,OAAO,KAAK,MAAM,GAAG,EAAE;CAC3F,IAAI,MAAM,SAAS,YAAY,OAAO;EAAE,MAAM,WAAW,IAAI;EAAG,QAAQ;CAAW;CACnF,O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