@md2nativedocx/core 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/diagrams/agentflow/parser.d.ts +20 -0
- package/dist/diagrams/agentflow/parser.js +155 -0
- package/dist/diagrams/agentflow/parser.js.map +1 -0
- package/dist/diagrams/architecture-diagram/icons.d.ts +24 -0
- package/dist/diagrams/architecture-diagram/icons.js +148 -0
- package/dist/diagrams/architecture-diagram/icons.js.map +1 -0
- package/dist/diagrams/architecture-diagram/layout.d.ts +71 -0
- package/dist/diagrams/architecture-diagram/layout.js +388 -0
- package/dist/diagrams/architecture-diagram/layout.js.map +1 -0
- package/dist/diagrams/architecture-diagram/parser.d.ts +14 -19
- package/dist/diagrams/architecture-diagram/parser.js +316 -113
- package/dist/diagrams/architecture-diagram/parser.js.map +1 -1
- package/dist/diagrams/architecture-diagram/translator.d.ts +26 -36
- package/dist/diagrams/architecture-diagram/translator.js +234 -147
- package/dist/diagrams/architecture-diagram/translator.js.map +1 -1
- package/dist/diagrams/architecture-diagram/types.d.ts +26 -16
- package/dist/diagrams/architecture-diagram/types.js +4 -14
- package/dist/diagrams/architecture-diagram/types.js.map +1 -1
- package/dist/diagrams/c4/parser.d.ts +11 -19
- package/dist/diagrams/c4/parser.js +242 -90
- package/dist/diagrams/c4/parser.js.map +1 -1
- package/dist/diagrams/c4/translator.d.ts +23 -43
- package/dist/diagrams/c4/translator.js +333 -204
- package/dist/diagrams/c4/translator.js.map +1 -1
- package/dist/diagrams/c4/types.d.ts +78 -45
- package/dist/diagrams/c4/types.js +17 -38
- package/dist/diagrams/c4/types.js.map +1 -1
- package/dist/diagrams/class-diagram/parser.d.ts +10 -11
- package/dist/diagrams/class-diagram/parser.js +322 -74
- package/dist/diagrams/class-diagram/parser.js.map +1 -1
- package/dist/diagrams/class-diagram/translator.d.ts +10 -10
- package/dist/diagrams/class-diagram/translator.js +305 -33
- package/dist/diagrams/class-diagram/translator.js.map +1 -1
- package/dist/diagrams/class-diagram/types.d.ts +61 -4
- package/dist/diagrams/er-diagram/parser.d.ts +22 -19
- package/dist/diagrams/er-diagram/parser.js +412 -89
- package/dist/diagrams/er-diagram/parser.js.map +1 -1
- package/dist/diagrams/er-diagram/translator.d.ts +8 -0
- package/dist/diagrams/er-diagram/translator.js +170 -38
- package/dist/diagrams/er-diagram/translator.js.map +1 -1
- package/dist/diagrams/er-diagram/types.d.ts +32 -5
- package/dist/diagrams/er-diagram/types.js +2 -5
- package/dist/diagrams/er-diagram/types.js.map +1 -1
- package/dist/diagrams/flowchart-source.d.ts +106 -0
- package/dist/diagrams/flowchart-source.js +272 -0
- package/dist/diagrams/flowchart-source.js.map +1 -0
- package/dist/diagrams/gantt/date-utils.d.ts +55 -33
- package/dist/diagrams/gantt/date-utils.js +147 -58
- package/dist/diagrams/gantt/date-utils.js.map +1 -1
- package/dist/diagrams/gantt/parser.d.ts +31 -22
- package/dist/diagrams/gantt/parser.js +259 -141
- package/dist/diagrams/gantt/parser.js.map +1 -1
- package/dist/diagrams/gantt/time-axis.d.ts +44 -0
- package/dist/diagrams/gantt/time-axis.js +158 -0
- package/dist/diagrams/gantt/time-axis.js.map +1 -0
- package/dist/diagrams/gantt/translator.d.ts +8 -5
- package/dist/diagrams/gantt/translator.js +72 -60
- package/dist/diagrams/gantt/translator.js.map +1 -1
- package/dist/diagrams/gantt/types.d.ts +25 -4
- package/dist/diagrams/gantt/types.js +4 -3
- package/dist/diagrams/gantt/types.js.map +1 -1
- package/dist/diagrams/mindmap/parser.d.ts +18 -14
- package/dist/diagrams/mindmap/parser.js +169 -69
- package/dist/diagrams/mindmap/parser.js.map +1 -1
- package/dist/diagrams/mindmap/translator.js +90 -35
- package/dist/diagrams/mindmap/translator.js.map +1 -1
- package/dist/diagrams/mindmap/types.d.ts +18 -0
- package/dist/diagrams/packet/parser.js +1 -1
- package/dist/diagrams/packet/parser.js.map +1 -1
- package/dist/diagrams/pie/translator.js +5 -4
- package/dist/diagrams/pie/translator.js.map +1 -1
- package/dist/diagrams/plantuml/class-parser.d.ts +2 -1
- package/dist/diagrams/plantuml/class-parser.js +19 -11
- package/dist/diagrams/plantuml/class-parser.js.map +1 -1
- package/dist/diagrams/plantuml/sequence-parser.d.ts +3 -2
- package/dist/diagrams/plantuml/sequence-parser.js +28 -12
- package/dist/diagrams/plantuml/sequence-parser.js.map +1 -1
- package/dist/diagrams/plantuml/state-parser.d.ts +5 -3
- package/dist/diagrams/plantuml/state-parser.js +141 -19
- package/dist/diagrams/plantuml/state-parser.js.map +1 -1
- package/dist/diagrams/quadrant/translator.js +5 -4
- package/dist/diagrams/quadrant/translator.js.map +1 -1
- package/dist/diagrams/railroad/layout.d.ts +89 -0
- package/dist/diagrams/railroad/layout.js +235 -0
- package/dist/diagrams/railroad/layout.js.map +1 -0
- package/dist/diagrams/railroad/parser.d.ts +37 -0
- package/dist/diagrams/railroad/parser.js +605 -0
- package/dist/diagrams/railroad/parser.js.map +1 -0
- package/dist/diagrams/railroad/translator.d.ts +11 -0
- package/dist/diagrams/railroad/translator.js +101 -0
- package/dist/diagrams/railroad/translator.js.map +1 -0
- package/dist/diagrams/railroad/types.d.ts +61 -0
- package/dist/diagrams/railroad/types.js +7 -0
- package/dist/diagrams/railroad/types.js.map +1 -0
- package/dist/diagrams/requirement-diagram/parser.d.ts +26 -19
- package/dist/diagrams/requirement-diagram/parser.js +249 -70
- package/dist/diagrams/requirement-diagram/parser.js.map +1 -1
- package/dist/diagrams/requirement-diagram/translator.d.ts +16 -11
- package/dist/diagrams/requirement-diagram/translator.js +170 -50
- package/dist/diagrams/requirement-diagram/translator.js.map +1 -1
- package/dist/diagrams/requirement-diagram/types.d.ts +30 -9
- package/dist/diagrams/requirement-diagram/types.js +7 -5
- package/dist/diagrams/requirement-diagram/types.js.map +1 -1
- package/dist/diagrams/sequence/parser.d.ts +8 -7
- package/dist/diagrams/sequence/parser.js +129 -32
- package/dist/diagrams/sequence/parser.js.map +1 -1
- package/dist/diagrams/sequence/participant-glyph.d.ts +38 -0
- package/dist/diagrams/sequence/participant-glyph.js +97 -0
- package/dist/diagrams/sequence/participant-glyph.js.map +1 -0
- package/dist/diagrams/sequence/participant-kind.d.ts +7 -0
- package/dist/diagrams/sequence/participant-kind.js +19 -0
- package/dist/diagrams/sequence/participant-kind.js.map +1 -0
- package/dist/diagrams/sequence/translator.d.ts +7 -3
- package/dist/diagrams/sequence/translator.js +140 -28
- package/dist/diagrams/sequence/translator.js.map +1 -1
- package/dist/diagrams/sequence/types.d.ts +24 -1
- package/dist/diagrams/state-diagram/parser.d.ts +24 -24
- package/dist/diagrams/state-diagram/parser.js +259 -73
- package/dist/diagrams/state-diagram/parser.js.map +1 -1
- package/dist/diagrams/state-diagram/translator.d.ts +13 -1
- package/dist/diagrams/state-diagram/translator.js +554 -72
- package/dist/diagrams/state-diagram/translator.js.map +1 -1
- package/dist/diagrams/state-diagram/types.d.ts +57 -11
- package/dist/diagrams/state-diagram/types.js +5 -7
- package/dist/diagrams/state-diagram/types.js.map +1 -1
- package/dist/diagrams/tree-view/implicit-root.d.ts +11 -0
- package/dist/diagrams/tree-view/implicit-root.js +14 -0
- package/dist/diagrams/tree-view/implicit-root.js.map +1 -0
- package/dist/diagrams/tree-view/parser.d.ts +23 -8
- package/dist/diagrams/tree-view/parser.js +242 -98
- package/dist/diagrams/tree-view/parser.js.map +1 -1
- package/dist/diagrams/tree-view/translator.d.ts +6 -5
- package/dist/diagrams/tree-view/translator.js +40 -23
- package/dist/diagrams/tree-view/translator.js.map +1 -1
- package/dist/diagrams/tree-view/types.d.ts +22 -6
- package/dist/diagrams/tree-view/types.js +2 -2
- package/dist/diagrams/treemap/parser.d.ts +2 -2
- package/dist/diagrams/treemap/parser.js +5 -12
- package/dist/diagrams/treemap/parser.js.map +1 -1
- package/dist/diagrams/usecase/parser.d.ts +24 -0
- package/dist/diagrams/usecase/parser.js +232 -0
- package/dist/diagrams/usecase/parser.js.map +1 -0
- package/dist/diagrams/venn/translator.js +5 -4
- package/dist/diagrams/venn/translator.js.map +1 -1
- package/dist/diagrams/zenuml/parser.d.ts +2 -2
- package/dist/diagrams/zenuml/parser.js +14 -7
- package/dist/diagrams/zenuml/parser.js.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/layout/collapse-subgraphs.d.ts +18 -0
- package/dist/layout/collapse-subgraphs.js +80 -0
- package/dist/layout/collapse-subgraphs.js.map +1 -0
- package/dist/layout/layout.d.ts +11 -0
- package/dist/layout/layout.js +89 -17
- package/dist/layout/layout.js.map +1 -1
- package/dist/parser/css-named-colors.d.ts +7 -0
- package/dist/parser/css-named-colors.js +157 -0
- package/dist/parser/css-named-colors.js.map +1 -0
- package/dist/parser/css-style.d.ts +39 -0
- package/dist/parser/css-style.js +88 -0
- package/dist/parser/css-style.js.map +1 -0
- package/dist/parser/diagram-type.d.ts +12 -1
- package/dist/parser/diagram-type.js +4 -0
- package/dist/parser/diagram-type.js.map +1 -1
- package/dist/parser/entities.d.ts +5 -0
- package/dist/parser/entities.js +52 -0
- package/dist/parser/entities.js.map +1 -0
- package/dist/parser/frontmatter.d.ts +36 -0
- package/dist/parser/frontmatter.js +66 -0
- package/dist/parser/frontmatter.js.map +1 -0
- package/dist/parser/interactive.d.ts +13 -0
- package/dist/parser/interactive.js +17 -0
- package/dist/parser/interactive.js.map +1 -0
- package/dist/parser/label.d.ts +37 -0
- package/dist/parser/label.js +73 -0
- package/dist/parser/label.js.map +1 -0
- package/dist/parser/normalize-statements.d.ts +8 -0
- package/dist/parser/normalize-statements.js +17 -2
- package/dist/parser/normalize-statements.js.map +1 -1
- package/dist/parser/parser.d.ts +2 -1
- package/dist/parser/parser.js +345 -324
- package/dist/parser/parser.js.map +1 -1
- package/dist/render-diagram-odf.d.ts +2 -1
- package/dist/render-diagram-odf.js +39 -8
- package/dist/render-diagram-odf.js.map +1 -1
- package/dist/render-diagram.d.ts +11 -3
- package/dist/render-diagram.js +66 -23
- package/dist/render-diagram.js.map +1 -1
- package/dist/smartart/chain.js +1 -1
- package/dist/smartart/chain.js.map +1 -1
- package/dist/smartart/cycle.js +1 -1
- package/dist/smartart/cycle.js.map +1 -1
- package/dist/smartart/drawing.js +2 -10
- package/dist/smartart/drawing.js.map +1 -1
- package/dist/smartart/embed.d.ts +4 -3
- package/dist/smartart/embed.js +5 -4
- package/dist/smartart/embed.js.map +1 -1
- package/dist/smartart/from-graph.d.ts +13 -3
- package/dist/smartart/from-graph.js +39 -7
- package/dist/smartart/from-graph.js.map +1 -1
- package/dist/smartart/from-tree.d.ts +9 -5
- package/dist/smartart/from-tree.js +23 -11
- package/dist/smartart/from-tree.js.map +1 -1
- package/dist/smartart/text.d.ts +18 -4
- package/dist/smartart/text.js +32 -8
- package/dist/smartart/text.js.map +1 -1
- package/dist/smartart/tree-deep.js +1 -1
- package/dist/smartart/tree-deep.js.map +1 -1
- package/dist/smartart/tree.js +2 -2
- package/dist/smartart/tree.js.map +1 -1
- package/dist/translator/actor-figure.d.ts +45 -0
- package/dist/translator/actor-figure.js +46 -0
- package/dist/translator/actor-figure.js.map +1 -0
- package/dist/translator/boxes.d.ts +12 -1
- package/dist/translator/boxes.js +11 -6
- package/dist/translator/boxes.js.map +1 -1
- package/dist/translator/canvas.d.ts +28 -1
- package/dist/translator/canvas.js +83 -16
- package/dist/translator/canvas.js.map +1 -1
- package/dist/translator/custom-geometry.d.ts +28 -0
- package/dist/translator/custom-geometry.js +59 -0
- package/dist/translator/custom-geometry.js.map +1 -0
- package/dist/translator/elbow-connector.d.ts +52 -0
- package/dist/translator/elbow-connector.js +110 -0
- package/dist/translator/elbow-connector.js.map +1 -0
- package/dist/translator/graph-shapes.d.ts +40 -4
- package/dist/translator/graph-shapes.js +62 -10
- package/dist/translator/graph-shapes.js.map +1 -1
- package/dist/translator/label-runs.d.ts +14 -0
- package/dist/translator/label-runs.js +36 -0
- package/dist/translator/label-runs.js.map +1 -0
- package/dist/translator/odf-translator.js +56 -24
- package/dist/translator/odf-translator.js.map +1 -1
- package/dist/translator/ooxml-translator.d.ts +17 -11
- package/dist/translator/ooxml-translator.js +159 -146
- package/dist/translator/ooxml-translator.js.map +1 -1
- package/dist/translator/text-color.d.ts +9 -0
- package/dist/translator/text-color.js +17 -0
- package/dist/translator/text-color.js.map +1 -0
- package/dist/types.d.ts +27 -4
- package/package.json +1 -1
package/dist/parser/parser.js
CHANGED
|
@@ -3,12 +3,16 @@
|
|
|
3
3
|
*
|
|
4
4
|
* V1 scope (spec §5.1): `graph`/`flowchart` with `TD`/`LR` direction only.
|
|
5
5
|
* Supports node shapes from §6.1, edge types from §6.2, and `subgraph`
|
|
6
|
-
* containers. `classDef
|
|
6
|
+
* containers. `classDef` (including `default`), `class`/`:::` and `style` set a node's fill, stroke
|
|
7
|
+
* and text colour, layered as Mermaid layers them (see resolveNodeStyles); `linkStyle` an edge's.
|
|
7
8
|
*
|
|
8
9
|
* This module is a pure function from untrusted text to a typed AST. It never
|
|
9
10
|
* emits XML itself — escaping happens in the translator.
|
|
10
11
|
*/
|
|
11
12
|
import { splitLines, splitStatements } from './normalize-statements.js';
|
|
13
|
+
import { INTERACTIVE_DIRECTIVES_IGNORED, isInteractiveDirective } from './interactive.js';
|
|
14
|
+
import { parseLabel } from './label.js';
|
|
15
|
+
import { extractNodeStyle, parseCssStyleProps, parseHexColor } from './css-style.js';
|
|
12
16
|
/** Shape of a node based on its bracket syntax (spec §6.1). */
|
|
13
17
|
const SHAPE_BY_SYNTAX = [
|
|
14
18
|
// Longest/most specific patterns first, so e.g. `[[` matches subroutine
|
|
@@ -50,6 +54,7 @@ const SHAPE_BY_SYNTAX = [
|
|
|
50
54
|
* the two prior bugs in that same class).
|
|
51
55
|
*/
|
|
52
56
|
const SHAPE_ALIAS_MAP = {
|
|
57
|
+
actor: 'actor',
|
|
53
58
|
proc: 'rect',
|
|
54
59
|
process: 'rect',
|
|
55
60
|
rect: 'rect',
|
|
@@ -247,34 +252,48 @@ const EDGE_OPERATORS = [
|
|
|
247
252
|
* {@link EdgeType} within that family. Only matched when no full token from
|
|
248
253
|
* {@link EDGE_OPERATORS} already matched at the same position (checked first
|
|
249
254
|
* in {@link parseEdgeChain}), so a plain `A-->B` is never reinterpreted here.
|
|
250
|
-
*
|
|
251
|
-
*
|
|
255
|
+
*
|
|
256
|
+
* As in Mermaid's grammar, a closing marker may be longer than its shortest
|
|
257
|
+
* form (`A-- No ---->B` is an arrow, a longer one in Mermaid's layout): a run
|
|
258
|
+
* of at least two dashes (or `=`) then the end character, or dots then `-`.
|
|
259
|
+
* Each family's closings are tried in order, arrow forms first, so a line
|
|
260
|
+
* form never captures the dashes of an arrow and strands its `>`.
|
|
252
261
|
*/
|
|
253
262
|
const MID_LABEL_FAMILIES = [
|
|
254
263
|
{
|
|
255
264
|
open: '--',
|
|
256
265
|
closings: [
|
|
257
|
-
{
|
|
258
|
-
{
|
|
259
|
-
{
|
|
260
|
-
{
|
|
266
|
+
{ pattern: /-{2,}>/y, type: 'arrow' },
|
|
267
|
+
{ pattern: /-{2,}o/y, type: 'circle' },
|
|
268
|
+
{ pattern: /-{2,}x/y, type: 'cross' },
|
|
269
|
+
{ pattern: /-{3,}/y, type: 'line' },
|
|
261
270
|
],
|
|
262
271
|
},
|
|
263
272
|
{
|
|
264
273
|
open: '-.',
|
|
265
274
|
closings: [
|
|
266
|
-
{
|
|
267
|
-
{
|
|
275
|
+
{ pattern: /\.+->/y, type: 'dotted' },
|
|
276
|
+
{ pattern: /\.+-/y, type: 'dottedLine' },
|
|
268
277
|
],
|
|
269
278
|
},
|
|
270
279
|
{
|
|
271
280
|
open: '==',
|
|
272
281
|
closings: [
|
|
273
|
-
{
|
|
274
|
-
{
|
|
282
|
+
{ pattern: /={2,}>/y, type: 'thick' },
|
|
283
|
+
{ pattern: /={3,}/y, type: 'thickLine' },
|
|
275
284
|
],
|
|
276
285
|
},
|
|
277
286
|
];
|
|
287
|
+
/** Length of a closing marker of `family` starting at `at`, and its edge type; `null` if none starts there. */
|
|
288
|
+
function matchClosing(line, at, family) {
|
|
289
|
+
for (const closing of family.closings) {
|
|
290
|
+
closing.pattern.lastIndex = at;
|
|
291
|
+
const m = closing.pattern.exec(line);
|
|
292
|
+
if (m)
|
|
293
|
+
return { length: m[0].length, type: closing.type };
|
|
294
|
+
}
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
278
297
|
/**
|
|
279
298
|
* Node ids that collide with Object.prototype members. Dagre stores nodes in a
|
|
280
299
|
* plain object, so an id like `__proto__`, `constructor`, or `length` would
|
|
@@ -312,184 +331,44 @@ function stripQuotedLabel(text) {
|
|
|
312
331
|
}
|
|
313
332
|
return text;
|
|
314
333
|
}
|
|
315
|
-
/**
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
];
|
|
325
|
-
/**
|
|
326
|
-
* Parse the small set of inline-text conveniences Mermaid supports (found via
|
|
327
|
-
* `docs/markdown-mermaid-compliance-table.md` §5, logged in `TODO.md`) into
|
|
328
|
-
* both a flattened plain-text fallback (`text`, used for box-name attributes
|
|
329
|
-
* and `layout.ts`'s width/height estimation — neither needs to know about a
|
|
330
|
-
* bold/italic span) and a structured rich-text body (`runs`, `types.ts`'s
|
|
331
|
-
* `LabelToken[]` — what the translator actually renders):
|
|
332
|
-
* - Mermaid's entity codes (`#quot;`, `#9829;`, ...) are decoded to their
|
|
333
|
-
* character first — the numeric form via its Unicode code point, plus the
|
|
334
|
-
* handful of named entities Mermaid documents — regardless of the two
|
|
335
|
-
* cases below.
|
|
336
|
-
* - `<br/>` (and `<br>`, `<br />`, case-insensitive) becomes a real line
|
|
337
|
-
* break: a `{ break: true }` token in `runs` (`text` flattens it to a
|
|
338
|
-
* single space instead, same as before this function could produce real
|
|
339
|
-
* breaks — a fallback string has nowhere to put a second line).
|
|
340
|
-
* - A "Markdown string" (a label wrapped in backticks) has its delimiters
|
|
341
|
-
* stripped, and `**bold**`/`__bold__`/`*italic*`/`_italic_` spans become
|
|
342
|
-
* real `LabelRun.bold`/`.italic` tokens instead of literal asterisks or a
|
|
343
|
-
* stripped-to-plain-text approximation. Emphasis markers are only
|
|
344
|
-
* recognized inside a Markdown string — Mermaid's classic (non-backtick)
|
|
345
|
-
* label syntax never interprets them, so a literal `**` in an ordinary
|
|
346
|
-
* label stays literal, unchanged from before.
|
|
347
|
-
*/
|
|
348
|
-
function parseLabel(text) {
|
|
349
|
-
let decoded = text.replace(/#(\d+);/g, (_, code) => String.fromCodePoint(Number(code)));
|
|
350
|
-
for (const [name, char] of NAMED_ENTITIES) {
|
|
351
|
-
decoded = decoded.split(`#${name};`).join(char);
|
|
352
|
-
}
|
|
353
|
-
const isMarkdownString = decoded.length >= 2 && decoded.startsWith('`') && decoded.endsWith('`');
|
|
354
|
-
if (isMarkdownString)
|
|
355
|
-
decoded = decoded.slice(1, -1);
|
|
356
|
-
const segments = decoded.split(/<br\s*\/?>/gi);
|
|
357
|
-
const runs = [];
|
|
358
|
-
const plainSegments = [];
|
|
359
|
-
segments.forEach((segment, i) => {
|
|
360
|
-
if (i > 0)
|
|
361
|
-
runs.push({ break: true });
|
|
362
|
-
const segmentRuns = isMarkdownString
|
|
363
|
-
? parseEmphasis(segment)
|
|
364
|
-
: segment.length > 0
|
|
365
|
-
? [{ text: segment }]
|
|
366
|
-
: [];
|
|
367
|
-
runs.push(...segmentRuns);
|
|
368
|
-
plainSegments.push(segmentRuns.map((r) => r.text).join(''));
|
|
369
|
-
});
|
|
370
|
-
return { text: plainSegments.join(' '), runs };
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* Split a Markdown-string segment (no `<br/>` in it — {@link parseLabel}
|
|
374
|
-
* already split those out) into plain/bold/italic {@link LabelRun}s.
|
|
375
|
-
* Non-nested and non-overlapping — CommonMark's full emphasis-resolution
|
|
376
|
-
* algorithm is out of scope here (V1 tolerance, same spirit as the rest of
|
|
377
|
-
* this parser); `**`/`__` (bold) is tried before `*`/`_` (italic) at each
|
|
378
|
-
* position so a bold span is never misread as two italic markers.
|
|
379
|
-
*/
|
|
380
|
-
function parseEmphasis(text) {
|
|
381
|
-
const runs = [];
|
|
382
|
-
const re = /(\*\*|__)([\s\S]+?)\1|(\*|_)([\s\S]+?)\3/g;
|
|
383
|
-
let last = 0;
|
|
384
|
-
let m;
|
|
385
|
-
while ((m = re.exec(text)) !== null) {
|
|
386
|
-
if (m.index > last)
|
|
387
|
-
runs.push({ text: text.slice(last, m.index) });
|
|
388
|
-
if (m[1] !== undefined)
|
|
389
|
-
runs.push({ text: m[2], bold: true });
|
|
390
|
-
else
|
|
391
|
-
runs.push({ text: m[4], italic: true });
|
|
392
|
-
last = re.lastIndex;
|
|
393
|
-
}
|
|
394
|
-
if (last < text.length)
|
|
395
|
-
runs.push({ text: text.slice(last) });
|
|
396
|
-
return runs;
|
|
397
|
-
}
|
|
398
|
-
/**
|
|
399
|
-
* Parse a CSS-like `prop:value,prop:value` property list (`classDef`/`style`/
|
|
400
|
-
* `linkStyle`, spec §6.3) into a lowercase-keyed map. Order-independent by
|
|
401
|
-
* design — unlike the old `classDef` regex this replaces, which required
|
|
402
|
-
* `fill:` to be the first (and only) property, silently failing the whole
|
|
403
|
-
* line otherwise. Values that themselves contain a comma (e.g. a
|
|
404
|
-
* `stroke-dasharray` list) will fragment here, but none of the properties
|
|
405
|
-
* this parser actually reads (`fill`, `stroke`, `stroke-width`) are affected.
|
|
406
|
-
*/
|
|
407
|
-
function parseCssStyleProps(text) {
|
|
408
|
-
const props = new Map();
|
|
409
|
-
for (const part of text.split(',')) {
|
|
410
|
-
const i = part.indexOf(':');
|
|
411
|
-
if (i === -1)
|
|
412
|
-
continue;
|
|
413
|
-
const key = part.slice(0, i).trim().toLowerCase();
|
|
414
|
-
const value = part.slice(i + 1).trim();
|
|
415
|
-
if (key.length > 0 && value.length > 0)
|
|
416
|
-
props.set(key, value);
|
|
417
|
-
}
|
|
418
|
-
return props;
|
|
334
|
+
/** Record that `className` was assigned to `nodeId` (`class` or `:::`), in source order. */
|
|
335
|
+
function addNodeClass(nodeClasses, nodeId, className) {
|
|
336
|
+
if (!className)
|
|
337
|
+
return;
|
|
338
|
+
const list = nodeClasses.get(nodeId);
|
|
339
|
+
if (list)
|
|
340
|
+
list.push(className);
|
|
341
|
+
else
|
|
342
|
+
nodeClasses.set(nodeId, [className]);
|
|
419
343
|
}
|
|
420
344
|
/**
|
|
421
|
-
*
|
|
422
|
-
* the
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
* the property is just dropped rather than failing the whole statement.
|
|
345
|
+
* Give every node its final colours, layered as Mermaid 12.3 layers them (`getCompiledStyles`
|
|
346
|
+
* in its flowchart database, then the node's inline styles): `classDef default`, `classDef node`,
|
|
347
|
+
* each class assigned to the node in source order, and last the node's `style` statements. This
|
|
348
|
+
* runs once the whole source is read, so a `classDef` or a node may come after the statement that
|
|
349
|
+
* refers to it. Returns the names assigned but never defined, in order of first assignment.
|
|
427
350
|
*/
|
|
428
|
-
function
|
|
429
|
-
const
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
if (hex3) {
|
|
435
|
-
const [r, g, b] = hex3[1];
|
|
436
|
-
return `${r}${r}${g}${g}${b}${b}`.toUpperCase();
|
|
437
|
-
}
|
|
438
|
-
return null;
|
|
439
|
-
}
|
|
440
|
-
/** Extract `fill`/`stroke`/`color` from a parsed property map, dropping anything not
|
|
441
|
-
* a recognized hex color (see {@link parseHexColor}). */
|
|
442
|
-
function extractNodeStyle(props) {
|
|
443
|
-
const patch = {};
|
|
444
|
-
const fillValue = props.get('fill');
|
|
445
|
-
if (fillValue) {
|
|
446
|
-
const hex = parseHexColor(fillValue);
|
|
447
|
-
if (hex)
|
|
448
|
-
patch.fill = hex;
|
|
449
|
-
}
|
|
450
|
-
const strokeValue = props.get('stroke');
|
|
451
|
-
if (strokeValue) {
|
|
452
|
-
const hex = parseHexColor(strokeValue);
|
|
453
|
-
if (hex)
|
|
454
|
-
patch.stroke = hex;
|
|
351
|
+
function resolveNodeStyles(nodes, classDefs, nodeClasses, inlineStyles) {
|
|
352
|
+
const undefinedNames = new Set();
|
|
353
|
+
for (const names of nodeClasses.values()) {
|
|
354
|
+
for (const name of names)
|
|
355
|
+
if (!classDefs.has(name))
|
|
356
|
+
undefinedNames.add(name);
|
|
455
357
|
}
|
|
456
|
-
const
|
|
457
|
-
|
|
458
|
-
const
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
return patch;
|
|
463
|
-
}
|
|
464
|
-
/**
|
|
465
|
-
* Apply a `fill`/`stroke` patch to a node, or — if the node hasn't been
|
|
466
|
-
* declared yet (a class/style referencing a node defined later in the
|
|
467
|
-
* source) — remember it in `pendingStyles` for the end-of-parse sweep. Merges
|
|
468
|
-
* onto whatever patch (if any) is already pending, so `style A stroke:#000`
|
|
469
|
-
* followed later by `A:::crit` (a fill-only class) doesn't clobber the
|
|
470
|
-
* stroke the first statement set.
|
|
471
|
-
*/
|
|
472
|
-
function applyNodeStyle(nodes, pendingStyles, nodeId, patch) {
|
|
473
|
-
if (patch.fill === undefined && patch.stroke === undefined && patch.color === undefined)
|
|
474
|
-
return;
|
|
475
|
-
const existing = nodes.get(nodeId);
|
|
476
|
-
if (existing) {
|
|
358
|
+
for (const node of nodes.values()) {
|
|
359
|
+
const patch = {};
|
|
360
|
+
for (const name of ['default', 'node', ...(nodeClasses.get(node.id) ?? [])]) {
|
|
361
|
+
Object.assign(patch, classDefs.get(name));
|
|
362
|
+
}
|
|
363
|
+
Object.assign(patch, inlineStyles.get(node.id));
|
|
477
364
|
if (patch.fill !== undefined)
|
|
478
|
-
|
|
365
|
+
node.fill = patch.fill;
|
|
479
366
|
if (patch.stroke !== undefined)
|
|
480
|
-
|
|
367
|
+
node.stroke = patch.stroke;
|
|
481
368
|
if (patch.color !== undefined)
|
|
482
|
-
|
|
483
|
-
return;
|
|
369
|
+
node.textColor = patch.color;
|
|
484
370
|
}
|
|
485
|
-
|
|
486
|
-
if (patch.fill !== undefined)
|
|
487
|
-
pending.fill = patch.fill;
|
|
488
|
-
if (patch.stroke !== undefined)
|
|
489
|
-
pending.stroke = patch.stroke;
|
|
490
|
-
if (patch.color !== undefined)
|
|
491
|
-
pending.color = patch.color;
|
|
492
|
-
pendingStyles.set(nodeId, pending);
|
|
371
|
+
return [...undefinedNames];
|
|
493
372
|
}
|
|
494
373
|
/**
|
|
495
374
|
* Merge physical lines that a bracketed node label wraps across, e.g.
|
|
@@ -501,33 +380,79 @@ function applyNodeStyle(nodes, pendingStyles, nodeId, patch) {
|
|
|
501
380
|
* edge — disconnecting the diagram. Found via medium-realistic.mmd
|
|
502
381
|
* (2026-09-04). Lines that are already bracket-balanced (the common case)
|
|
503
382
|
* flush immediately and are unaffected.
|
|
383
|
+
*
|
|
384
|
+
* A bracket or quote that never closes (`A[Receive ticket --> B[Assign crew]`) is a syntax error in
|
|
385
|
+
* Mermaid. Joining to the end of the block would turn every following line into that node's label and
|
|
386
|
+
* draw a single box; instead the line that opened it is kept as a statement of its own (read normally if
|
|
387
|
+
* it can be, e.g. a stray `\"` inside a quoted label, otherwise warned about as unclosed), and the lines
|
|
388
|
+
* after it are read again.
|
|
504
389
|
*/
|
|
505
390
|
function joinBracketContinuations(lines) {
|
|
506
|
-
const
|
|
507
|
-
let
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
391
|
+
const statements = [];
|
|
392
|
+
let pending = lines;
|
|
393
|
+
while (pending.length > 0) {
|
|
394
|
+
let buffer = [];
|
|
395
|
+
let depth = 0;
|
|
396
|
+
// Carried across lines: a quoted label may span several (`a["`Line1` / `Line 2`"]`), and its closing
|
|
397
|
+
// quote must not be read as an opening one.
|
|
398
|
+
let inQuotes = false;
|
|
399
|
+
for (const line of pending) {
|
|
400
|
+
buffer.push(line.trim());
|
|
401
|
+
const scan = bracketDelta(line, inQuotes);
|
|
402
|
+
depth += scan.delta;
|
|
403
|
+
inQuotes = scan.inQuotes;
|
|
404
|
+
if (depth <= 0) {
|
|
405
|
+
statements.push(buffer.join(' '));
|
|
406
|
+
buffer = [];
|
|
407
|
+
depth = 0;
|
|
408
|
+
inQuotes = false;
|
|
409
|
+
}
|
|
516
410
|
}
|
|
411
|
+
if (buffer.length === 0)
|
|
412
|
+
break;
|
|
413
|
+
statements.push(buffer[0]);
|
|
414
|
+
pending = pending.slice(pending.length - buffer.length + 1);
|
|
517
415
|
}
|
|
518
|
-
|
|
519
|
-
|
|
416
|
+
return statements;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Remove edge ids (`A e1@--> B`, `A e1@==> B`) from a statement, adding each to `edgeIds`. Only outside
|
|
420
|
+
* quotes and brackets, and only where `@` is directly followed by an edge operator, so a node's own
|
|
421
|
+
* `@{ shape: … }` and label text are untouched.
|
|
422
|
+
*/
|
|
423
|
+
function stripEdgeIds(line, edgeIds) {
|
|
424
|
+
let out = '';
|
|
425
|
+
let depth = 0;
|
|
426
|
+
let inQuotes = false;
|
|
427
|
+
for (let i = 0; i < line.length; i++) {
|
|
428
|
+
const ch = line[i];
|
|
429
|
+
if (ch === '"')
|
|
430
|
+
inQuotes = !inQuotes;
|
|
431
|
+
else if (!inQuotes && (ch === '[' || ch === '(' || ch === '{'))
|
|
432
|
+
depth++;
|
|
433
|
+
else if (!inQuotes && (ch === ']' || ch === ')' || ch === '}') && depth > 0)
|
|
434
|
+
depth--;
|
|
435
|
+
else if (!inQuotes && depth === 0 && /\s/.test(ch)) {
|
|
436
|
+
const m = /^\s+([A-Za-z_][\w-]*)@(?=--|==|-\.|<-|<=|~~~|[ox](?:--|==|-\.))/.exec(line.slice(i));
|
|
437
|
+
if (m) {
|
|
438
|
+
edgeIds.add(m[1]);
|
|
439
|
+
out += ' ';
|
|
440
|
+
i += m[0].length - 1;
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
out += ch;
|
|
520
445
|
}
|
|
521
|
-
return
|
|
446
|
+
return out;
|
|
522
447
|
}
|
|
523
448
|
/**
|
|
524
449
|
* Net count of `[`/`(`/`{` minus `]`/`)`/`}` in a line, ignoring anything
|
|
525
450
|
* inside a double-quoted label (a literal bracket character in quoted text,
|
|
526
451
|
* e.g. `n["["]`, must not be mistaken for an unterminated statement).
|
|
527
452
|
*/
|
|
528
|
-
function bracketDelta(line) {
|
|
453
|
+
function bracketDelta(line, startInQuotes = false) {
|
|
529
454
|
let delta = 0;
|
|
530
|
-
let inQuotes =
|
|
455
|
+
let inQuotes = startInQuotes;
|
|
531
456
|
for (const ch of line) {
|
|
532
457
|
if (ch === '"') {
|
|
533
458
|
inQuotes = !inQuotes;
|
|
@@ -540,7 +465,7 @@ function bracketDelta(line) {
|
|
|
540
465
|
else if (ch === ']' || ch === ')' || ch === '}')
|
|
541
466
|
delta--;
|
|
542
467
|
}
|
|
543
|
-
return delta;
|
|
468
|
+
return { delta, inQuotes };
|
|
544
469
|
}
|
|
545
470
|
/**
|
|
546
471
|
* Parse a Mermaid flowchart block into an intermediate AST.
|
|
@@ -552,30 +477,45 @@ export function parseMermaid(text) {
|
|
|
552
477
|
const edges = [];
|
|
553
478
|
const subgraphs = [];
|
|
554
479
|
const subgraphStack = [];
|
|
555
|
-
// Node ids
|
|
556
|
-
|
|
557
|
-
const subgraphAttached = new Set();
|
|
480
|
+
// Node ids already kept by a subgraph that has closed — see closeSubgraph.
|
|
481
|
+
const claimedBySubgraph = new Set();
|
|
558
482
|
// class name -> style patch, from `classDef` (spec §6.3).
|
|
559
483
|
const classDefs = new Map();
|
|
560
|
-
// node id ->
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
// first in a diagram but nothing requires it to; resolved once the whole
|
|
565
|
-
// document (and therefore every `classDef`) is known, mirroring how
|
|
566
|
-
// `pendingStyles` above already handles the opposite ordering (a class
|
|
567
|
-
// applied to a node declared later in the source).
|
|
568
|
-
const pendingClassAssignments = [];
|
|
484
|
+
// node id -> classes assigned by `class`/`:::`, and -> merged `style` statements; both resolved
|
|
485
|
+
// against `classDefs` once the whole source is read (resolveNodeStyles).
|
|
486
|
+
const nodeClasses = new Map();
|
|
487
|
+
const inlineStyles = new Map();
|
|
569
488
|
// Deferred `linkStyle` statements (spec §6.3): applied after the full edge
|
|
570
489
|
// list is known, since an index can refer to an edge declared later in the
|
|
571
490
|
// source (the common case — `linkStyle` conventionally comes last).
|
|
572
491
|
const pendingLinkStyles = [];
|
|
573
492
|
let direction = 'TD';
|
|
574
|
-
const
|
|
493
|
+
const rawLines = joinBracketContinuations(splitLines(text)).flatMap((l) => splitStatements(l));
|
|
494
|
+
// Edge ids (`A e1@--> B`, Mermaid 11.6+): removed from the edge, kept so that statements about the
|
|
495
|
+
// edge (`e1@{ animate: true }`, `class e1 animate`) are not taken for nodes.
|
|
496
|
+
const edgeIds = new Set();
|
|
497
|
+
const lines = rawLines.map((l) => stripEdgeIds(l, edgeIds));
|
|
498
|
+
// Ids given `view: collapsed` (`S@{ view: collapsed }`), see linkSubgraphEndpoints.
|
|
499
|
+
const collapsedViews = new Set();
|
|
575
500
|
for (const rawLine of lines) {
|
|
576
501
|
const line = rawLine.trim();
|
|
577
502
|
if (line.length === 0)
|
|
578
503
|
continue;
|
|
504
|
+
// Edge metadata (`e1@{ animate: true, curve: linear }`): animation and per-edge curve have no
|
|
505
|
+
// counterpart in a static Word drawing.
|
|
506
|
+
const edgeMeta = /^([A-Za-z_][\w-]*)@\{(.*)\}$/.exec(line);
|
|
507
|
+
if (edgeMeta && edgeIds.has(edgeMeta[1]))
|
|
508
|
+
continue;
|
|
509
|
+
// `S@{ view: collapsed }` folds a subgraph in Mermaid. Whether `S` is a subgraph is only known at
|
|
510
|
+
// the end (it may be declared later), so it is noted here and the line goes on as a node statement.
|
|
511
|
+
if (edgeMeta && parseAtShapeProps(edgeMeta[2]).get('view')?.toLowerCase() === 'collapsed') {
|
|
512
|
+
collapsedViews.add(edgeMeta[1]);
|
|
513
|
+
}
|
|
514
|
+
if (isInteractiveDirective(line, ['click'])) {
|
|
515
|
+
if (!warnings.includes(INTERACTIVE_DIRECTIVES_IGNORED))
|
|
516
|
+
warnings.push(INTERACTIVE_DIRECTIVES_IGNORED);
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
579
519
|
// Header line: graph TD / flowchart LR/BT/RL / swimlane-beta LR. `TB` is
|
|
580
520
|
// Mermaid's own documented alias for `TD` (top-to-bottom either way) and
|
|
581
521
|
// maps straight through to it; the other three are distinct directions,
|
|
@@ -622,22 +562,21 @@ export function parseMermaid(text) {
|
|
|
622
562
|
const ids = classAssign[1]
|
|
623
563
|
.split(',')
|
|
624
564
|
.map((s) => s.trim())
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
applyNodeStyle(nodes, pendingStyles, id, patch);
|
|
630
|
-
}
|
|
631
|
-
else {
|
|
632
|
-
pendingClassAssignments.push({ nodeIds: ids, className });
|
|
633
|
-
}
|
|
565
|
+
// A class on an edge id (`class e1 animate`) styles an edge animation, not a node.
|
|
566
|
+
.filter((s) => s.length > 0 && !edgeIds.has(s));
|
|
567
|
+
for (const id of ids)
|
|
568
|
+
addNodeClass(nodeClasses, id, className);
|
|
634
569
|
continue;
|
|
635
570
|
}
|
|
636
571
|
// style A fill:#XXX,stroke:#XXX,... (spec §6.3) — direct styling, no
|
|
637
|
-
// classDef indirection. Same property parser as classDef/linkStyle.
|
|
572
|
+
// classDef indirection, drawn over every class. Same property parser as classDef/linkStyle.
|
|
638
573
|
const styleStmt = line.match(/^style\s+([A-Za-z0-9_-]+)\s+(.+)$/i);
|
|
639
574
|
if (styleStmt) {
|
|
640
|
-
|
|
575
|
+
const id = styleStmt[1];
|
|
576
|
+
inlineStyles.set(id, {
|
|
577
|
+
...inlineStyles.get(id),
|
|
578
|
+
...extractNodeStyle(parseCssStyleProps(styleStmt[2])),
|
|
579
|
+
});
|
|
641
580
|
continue;
|
|
642
581
|
}
|
|
643
582
|
// linkStyle 0,2 stroke:#XXX,stroke-width:Npx / linkStyle default ...
|
|
@@ -683,6 +622,7 @@ export function parseMermaid(text) {
|
|
|
683
622
|
if (/^end\s*$/i.test(line)) {
|
|
684
623
|
if (subgraphStack.length > 0) {
|
|
685
624
|
const closed = subgraphStack.pop();
|
|
625
|
+
closeSubgraph(closed, claimedBySubgraph);
|
|
686
626
|
const parent = subgraphStack[subgraphStack.length - 1];
|
|
687
627
|
if (parent && !parent.subgraphIds.includes(closed.id)) {
|
|
688
628
|
parent.subgraphIds.push(closed.id);
|
|
@@ -728,10 +668,10 @@ export function parseMermaid(text) {
|
|
|
728
668
|
warnings.push(`Node id "${edge.to}" is reserved and was ignored.`);
|
|
729
669
|
}
|
|
730
670
|
// Apply inline class styles (`A:::crit`) to the edge endpoints.
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
671
|
+
addNodeClass(nodeClasses, edge.from, edge.fromClass);
|
|
672
|
+
addNodeClass(nodeClasses, edge.to, edge.toClass);
|
|
673
|
+
mentionInCurrentSubgraph(subgraphStack, edge.from);
|
|
674
|
+
mentionInCurrentSubgraph(subgraphStack, edge.to);
|
|
735
675
|
}
|
|
736
676
|
continue;
|
|
737
677
|
}
|
|
@@ -741,40 +681,17 @@ export function parseMermaid(text) {
|
|
|
741
681
|
if (!registerNode(nodes, node.id, node.label, node.labelRuns, node.shape)) {
|
|
742
682
|
warnings.push(`Node id "${node.id}" is reserved and was ignored.`);
|
|
743
683
|
}
|
|
744
|
-
|
|
745
|
-
|
|
684
|
+
addNodeClass(nodeClasses, node.id, node.className);
|
|
685
|
+
mentionInCurrentSubgraph(subgraphStack, node.id);
|
|
746
686
|
continue;
|
|
747
687
|
}
|
|
748
688
|
// Unsupported construct — warn but keep going (V1 tolerance).
|
|
749
|
-
|
|
689
|
+
const reason = unsupportedLineReason(line);
|
|
690
|
+
warnings.push(reason ? `Unsupported line ignored (${reason}): ${line}` : `Unsupported line ignored: ${line}`);
|
|
750
691
|
}
|
|
751
|
-
//
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
// still warns, just later than before.
|
|
755
|
-
for (const { nodeIds, className } of pendingClassAssignments) {
|
|
756
|
-
const patch = classDefs.get(className);
|
|
757
|
-
if (!patch) {
|
|
758
|
-
warnings.push(`classDef "${className}" referenced but not defined.`);
|
|
759
|
-
continue;
|
|
760
|
-
}
|
|
761
|
-
for (const id of nodeIds) {
|
|
762
|
-
applyNodeStyle(nodes, pendingStyles, id, patch);
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
// Apply any pending class/style patches (including ones just resolved
|
|
766
|
-
// above) to nodes that were defined after the class/style statement that
|
|
767
|
-
// targets them.
|
|
768
|
-
for (const [id, patch] of pendingStyles) {
|
|
769
|
-
const existing = nodes.get(id);
|
|
770
|
-
if (existing) {
|
|
771
|
-
if (patch.fill !== undefined)
|
|
772
|
-
existing.fill = patch.fill;
|
|
773
|
-
if (patch.stroke !== undefined)
|
|
774
|
-
existing.stroke = patch.stroke;
|
|
775
|
-
if (patch.color !== undefined)
|
|
776
|
-
existing.textColor = patch.color;
|
|
777
|
-
}
|
|
692
|
+
// A class name still undefined once the whole document is read is a typo or an omission.
|
|
693
|
+
for (const name of resolveNodeStyles(nodes, classDefs, nodeClasses, inlineStyles)) {
|
|
694
|
+
warnings.push(`classDef "${name}" referenced but not defined.`);
|
|
778
695
|
}
|
|
779
696
|
// Apply deferred `linkStyle` statements now that the edge list is final —
|
|
780
697
|
// `default` covers every edge, otherwise each index picks one edge in
|
|
@@ -792,52 +709,111 @@ export function parseMermaid(text) {
|
|
|
792
709
|
edge.strokeWidth = patch.strokeWidth;
|
|
793
710
|
}
|
|
794
711
|
}
|
|
795
|
-
// A subgraph id must not also be a node: Mermaid allows edges between
|
|
796
|
-
// subgraphs (e.g. `U1 --> Y2`), and our parser would otherwise register the
|
|
797
|
-
// subgraph id as a plain node, which breaks Dagre (an id cannot be both a
|
|
798
|
-
// node and a cluster). Drop such nodes and warn. Edges referencing subgraphs
|
|
799
|
-
// as endpoints are dropped too (V1 does not support inter-subgraph edges).
|
|
800
|
-
const subgraphIdSet = new Set(subgraphs.map((s) => s.id));
|
|
801
|
-
for (const id of subgraphIdSet) {
|
|
802
|
-
if (nodes.has(id)) {
|
|
803
|
-
nodes.delete(id);
|
|
804
|
-
warnings.push(`Subgraph "${id}" was also used as a node; node ignored.`);
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
const edgesWithoutSubgraphs = edges.filter((e) => {
|
|
808
|
-
if (subgraphIdSet.has(e.from) || subgraphIdSet.has(e.to)) {
|
|
809
|
-
warnings.push(`Edge ${e.from} -> ${e.to} references a subgraph; ignored (V1).`);
|
|
810
|
-
return false;
|
|
811
|
-
}
|
|
812
|
-
return true;
|
|
813
|
-
});
|
|
814
712
|
if (subgraphStack.length > 0) {
|
|
815
713
|
warnings.push('Unclosed `subgraph` block.');
|
|
714
|
+
// Still drawn as if closed at the end, innermost first, as an `end` would have.
|
|
715
|
+
for (const open of [...subgraphStack].reverse())
|
|
716
|
+
closeSubgraph(open, claimedBySubgraph);
|
|
816
717
|
}
|
|
718
|
+
// Before the node list is taken: this removes the nodes registered under a subgraph's id.
|
|
719
|
+
const keptEdges = linkSubgraphEndpoints(nodes, edges, subgraphs, collapsedViews, warnings);
|
|
817
720
|
return {
|
|
818
721
|
ast: {
|
|
819
722
|
direction,
|
|
820
723
|
nodes: [...nodes.values()],
|
|
821
|
-
edges:
|
|
724
|
+
edges: keptEdges,
|
|
822
725
|
subgraphs,
|
|
823
726
|
},
|
|
824
727
|
warnings,
|
|
825
728
|
};
|
|
826
729
|
}
|
|
730
|
+
/**
|
|
731
|
+
* Settle the links whose end is a subgraph (`A --> S`, `S1 --> S2`), which Mermaid draws to the
|
|
732
|
+
* subgraph's border. The parser registered such an end as a plain node; it is removed here, so that an
|
|
733
|
+
* id is never both a node and a subgraph, and the link keeps the subgraph's id as its end (see
|
|
734
|
+
* {@link FlowEdge}). A subgraph's member lists drop the subgraph ids a link inside it mentioned.
|
|
735
|
+
*
|
|
736
|
+
* Warned and dropped: a link between a subgraph and something inside it (no border to stop at), and a
|
|
737
|
+
* link to a subgraph with no node in it (no box to draw). Warned and kept: a node declared with its own
|
|
738
|
+
* label or shape under a subgraph's id (the subgraph wins, as in Mermaid's drawing). A subgraph given
|
|
739
|
+
* `view: collapsed` is marked {@link Subgraph.collapsed}.
|
|
740
|
+
*/
|
|
741
|
+
function linkSubgraphEndpoints(nodes, edges, subgraphs, collapsedViews, warnings) {
|
|
742
|
+
const byId = new Map(subgraphs.map((s) => [s.id, s]));
|
|
743
|
+
for (const [id, sg] of byId) {
|
|
744
|
+
const node = nodes.get(id);
|
|
745
|
+
if (node) {
|
|
746
|
+
nodes.delete(id);
|
|
747
|
+
// `A --> S` registers `S` as a bare node: that is how a link to a subgraph is written.
|
|
748
|
+
if (node.label !== id || node.shape !== 'rect') {
|
|
749
|
+
warnings.push(`Subgraph "${id}" was also declared as a node; the node is not drawn.`);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
sg.nodeIds = sg.nodeIds.filter((member) => !byId.has(member));
|
|
753
|
+
if (collapsedViews.has(id))
|
|
754
|
+
sg.collapsed = true;
|
|
755
|
+
}
|
|
756
|
+
// Everything inside a subgraph, nested subgraphs included, memoized: a link is checked at both ends.
|
|
757
|
+
const contents = new Map();
|
|
758
|
+
const contentsOf = (sg, seen = new Set()) => {
|
|
759
|
+
const known = contents.get(sg.id);
|
|
760
|
+
if (known)
|
|
761
|
+
return known;
|
|
762
|
+
const all = new Set(sg.nodeIds);
|
|
763
|
+
seen.add(sg.id);
|
|
764
|
+
for (const childId of sg.subgraphIds) {
|
|
765
|
+
const child = byId.get(childId);
|
|
766
|
+
// `seen` guards against a cycle, which the parser cannot build but a caller-made AST could.
|
|
767
|
+
if (!child || seen.has(childId))
|
|
768
|
+
continue;
|
|
769
|
+
all.add(childId);
|
|
770
|
+
for (const id of contentsOf(child, seen))
|
|
771
|
+
all.add(id);
|
|
772
|
+
}
|
|
773
|
+
contents.set(sg.id, all);
|
|
774
|
+
return all;
|
|
775
|
+
};
|
|
776
|
+
const hasNode = (sg) => [...contentsOf(sg)].some((id) => !byId.has(id));
|
|
777
|
+
return edges.filter((e) => {
|
|
778
|
+
for (const [end, other] of [
|
|
779
|
+
[e.from, e.to],
|
|
780
|
+
[e.to, e.from],
|
|
781
|
+
]) {
|
|
782
|
+
const sg = byId.get(end);
|
|
783
|
+
if (!sg)
|
|
784
|
+
continue;
|
|
785
|
+
if (end === other || contentsOf(sg).has(other)) {
|
|
786
|
+
warnings.push(`Edge ${e.from} -> ${e.to} links subgraph "${end}" to something inside it; ignored.`);
|
|
787
|
+
return false;
|
|
788
|
+
}
|
|
789
|
+
if (!hasNode(sg)) {
|
|
790
|
+
warnings.push(`Edge ${e.from} -> ${e.to} links to subgraph "${end}", which holds no node; ignored.`);
|
|
791
|
+
return false;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
return true;
|
|
795
|
+
});
|
|
796
|
+
}
|
|
827
797
|
function parseSubgraphHeader(line) {
|
|
828
798
|
const rest = line.replace(/^subgraph\b/i, '').trim();
|
|
799
|
+
// A title is read like a node label (Markdown string, `#name;` entities, `<br>`): the reader sees its
|
|
800
|
+
// text, never the syntax around it.
|
|
801
|
+
const title = (raw) => parseLabel(raw).text;
|
|
829
802
|
// subgraph id["Title"] or subgraph id[Title] or subgraph "Title" or subgraph id
|
|
830
803
|
const withTitle = rest.match(/^([A-Za-z0-9_-]+)\s*\[\s*"([^"]*)"\s*\]/);
|
|
831
804
|
if (withTitle) {
|
|
832
|
-
return { id: withTitle[1], title: withTitle[2], nodeIds: [], subgraphIds: [] };
|
|
805
|
+
return { id: withTitle[1], title: title(withTitle[2]), nodeIds: [], subgraphIds: [] };
|
|
833
806
|
}
|
|
834
807
|
const withTitleNoQuotes = rest.match(/^([A-Za-z0-9_-]+)\s*\[\s*([^\]]*)\s*\]/);
|
|
835
808
|
if (withTitleNoQuotes) {
|
|
836
|
-
return { id: withTitleNoQuotes[1], title: withTitleNoQuotes[2].trim(), nodeIds: [], subgraphIds: [] };
|
|
809
|
+
return { id: withTitleNoQuotes[1], title: title(withTitleNoQuotes[2].trim()), nodeIds: [], subgraphIds: [] };
|
|
837
810
|
}
|
|
838
811
|
const quotedTitle = rest.match(/^"([^"]+)"\s*$/);
|
|
839
812
|
if (quotedTitle) {
|
|
840
|
-
|
|
813
|
+
// As in Mermaid, the id is the quoted text, without the backticks of a Markdown string.
|
|
814
|
+
const text = quotedTitle[1];
|
|
815
|
+
const id = text.length >= 2 && text.startsWith('`') && text.endsWith('`') ? text.slice(1, -1) : text;
|
|
816
|
+
return { id, title: title(text), nodeIds: [], subgraphIds: [] };
|
|
841
817
|
}
|
|
842
818
|
// Bare id (or id with spaces used as title).
|
|
843
819
|
const bare = rest.match(/^([A-Za-z0-9_-]+)\s*$/);
|
|
@@ -848,22 +824,30 @@ function parseSubgraphHeader(line) {
|
|
|
848
824
|
return { id: rest, title: rest, nodeIds: [], subgraphIds: [] };
|
|
849
825
|
}
|
|
850
826
|
/**
|
|
851
|
-
*
|
|
852
|
-
*
|
|
853
|
-
*
|
|
854
|
-
* every later line that happens to mention it. Without this, a node declared
|
|
855
|
-
* inside a nested subgraph gets re-attached to an ENCLOSING subgraph the next
|
|
856
|
-
* time an edge outside the nested block references it (e.g. `B --> C` after
|
|
857
|
-
* `C`'s subgraph already closed), leaking it into the wrong cluster.
|
|
827
|
+
* Note that the innermost open subgraph mentions `nodeId` (a node statement or
|
|
828
|
+
* either end of a link inside it). Membership is settled when the subgraph
|
|
829
|
+
* closes, see {@link closeSubgraph}.
|
|
858
830
|
*/
|
|
859
|
-
function
|
|
860
|
-
if (attached.has(nodeId))
|
|
861
|
-
return;
|
|
862
|
-
attached.add(nodeId);
|
|
831
|
+
function mentionInCurrentSubgraph(stack, nodeId) {
|
|
863
832
|
const top = stack[stack.length - 1];
|
|
864
|
-
if (top)
|
|
833
|
+
if (top && !top.nodeIds.includes(nodeId))
|
|
865
834
|
top.nodeIds.push(nodeId);
|
|
866
835
|
}
|
|
836
|
+
/**
|
|
837
|
+
* Settle a subgraph's members when it closes, the way Mermaid does: every node
|
|
838
|
+
* it mentions, wherever that node was first declared (`c1 --> a2` at the top,
|
|
839
|
+
* then `a1 --> a2` inside `one`, puts `a2` in `one`), except the nodes a
|
|
840
|
+
* subgraph that closed earlier already keeps. A nested subgraph closes before
|
|
841
|
+
* its parent, so `B --> C` written in the parent after the nested block keeps
|
|
842
|
+
* `C` in the nested subgraph; of two sibling subgraphs mentioning the same
|
|
843
|
+
* node, the first one to close keeps it. Checked against Mermaid itself on its
|
|
844
|
+
* official examples (`tools/mermaid-differential/`).
|
|
845
|
+
*/
|
|
846
|
+
function closeSubgraph(subgraph, claimed) {
|
|
847
|
+
subgraph.nodeIds = subgraph.nodeIds.filter((id) => !claimed.has(id));
|
|
848
|
+
for (const id of subgraph.nodeIds)
|
|
849
|
+
claimed.add(id);
|
|
850
|
+
}
|
|
867
851
|
function registerNode(nodes, id, label, labelRuns, shape = 'rect') {
|
|
868
852
|
// Reject ids that collide with Object.prototype (Dagre breaks on them).
|
|
869
853
|
if (!isSafeId(id))
|
|
@@ -876,24 +860,6 @@ function registerNode(nodes, id, label, labelRuns, shape = 'rect') {
|
|
|
876
860
|
nodes.set(id, { id, label, labelRuns, shape });
|
|
877
861
|
return true;
|
|
878
862
|
}
|
|
879
|
-
/**
|
|
880
|
-
* Apply a class's style patch to a node (from `:::class` inline or `class`).
|
|
881
|
-
* If the node is not yet defined, the patch is remembered in `pendingStyles`
|
|
882
|
-
* (via {@link applyNodeStyle}). If the class itself isn't defined *yet* (its
|
|
883
|
-
* `classDef` may still come later in the source), the assignment is queued in
|
|
884
|
-
* `pendingClassAssignments` for the end-of-parse sweep instead of warning
|
|
885
|
-
* immediately — see that array's doc comment in {@link parseMermaid}.
|
|
886
|
-
*/
|
|
887
|
-
function applyClassToNode(nodes, pendingStyles, classDefs, pendingClassAssignments, nodeId, className) {
|
|
888
|
-
if (!className)
|
|
889
|
-
return;
|
|
890
|
-
const patch = classDefs.get(className);
|
|
891
|
-
if (!patch) {
|
|
892
|
-
pendingClassAssignments.push({ nodeIds: [nodeId], className });
|
|
893
|
-
return;
|
|
894
|
-
}
|
|
895
|
-
applyNodeStyle(nodes, pendingStyles, nodeId, patch);
|
|
896
|
-
}
|
|
897
863
|
/**
|
|
898
864
|
* Parse a node statement like `A[Text]`, `B{Decision}`, or bare `C` —
|
|
899
865
|
* including an optional inline class shorthand (`A[Text]:::crit` or bare
|
|
@@ -927,16 +893,77 @@ function parseNodeStatement(line) {
|
|
|
927
893
|
className,
|
|
928
894
|
};
|
|
929
895
|
}
|
|
896
|
+
const bracketed = matchShapeSyntax(rest);
|
|
897
|
+
return bracketed ? { id, ...bracketed, className } : null;
|
|
898
|
+
}
|
|
899
|
+
/**
|
|
900
|
+
* Why a flowchart line could not be read, when the cause is one of the two mistakes seen most in
|
|
901
|
+
* generated diagrams (agent benchmark, 2026-11): a bracket or quote left open, or `->`, which is not a
|
|
902
|
+
* Mermaid link. `null` when neither applies. Text inside quotes and brackets is not looked at, so a label
|
|
903
|
+
* such as `A["x -> y"]` is not mistaken for a link.
|
|
904
|
+
*/
|
|
905
|
+
function unsupportedLineReason(line) {
|
|
906
|
+
const scan = bracketDelta(line);
|
|
907
|
+
if (scan.delta > 0 || scan.inQuotes)
|
|
908
|
+
return 'unclosed bracket or quote';
|
|
909
|
+
let outside = '';
|
|
910
|
+
let depth = 0;
|
|
911
|
+
let inQuotes = false;
|
|
912
|
+
for (const ch of line) {
|
|
913
|
+
if (ch === '"')
|
|
914
|
+
inQuotes = !inQuotes;
|
|
915
|
+
else if (inQuotes)
|
|
916
|
+
continue;
|
|
917
|
+
else if (ch === '[' || ch === '(' || ch === '{')
|
|
918
|
+
depth++;
|
|
919
|
+
else if (ch === ']' || ch === ')' || ch === '}')
|
|
920
|
+
depth = Math.max(0, depth - 1);
|
|
921
|
+
else if (depth === 0)
|
|
922
|
+
outside += ch;
|
|
923
|
+
}
|
|
924
|
+
return /(^|[^-=.])->/.test(outside) ? '`->` is not a link, use `-->`' : null;
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* Read `rest` as one bracketed node shape (`[Text]`, `{Text}`, `((Text))`…) covering all of it, longest
|
|
928
|
+
* syntax first so `(` does not match `((`. The shape's own bracket must close exactly at the end:
|
|
929
|
+
* `[Receive ticket] -> B[Assign crew]` starts with `[` and ends with `]`, but it is two bracketed texts
|
|
930
|
+
* joined by something that is not a link (Mermaid rejects it), not one node labelled
|
|
931
|
+
* `Receive ticket] -> B[Assign crew`. Only the bracket pair that closes the shape is counted, so a label
|
|
932
|
+
* with another kind of bracket (`A[Step 1)]`, `A[f(x)]`) is read as before; a quoted label is taken whole.
|
|
933
|
+
*/
|
|
934
|
+
function matchShapeSyntax(rest) {
|
|
930
935
|
for (const { open, close, shape } of SHAPE_BY_SYNTAX) {
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
936
|
+
if (!rest.startsWith(open) || !rest.endsWith(close))
|
|
937
|
+
continue;
|
|
938
|
+
const inner = rest.slice(open.length, rest.length - close.length).trim();
|
|
939
|
+
if (!closesOnlyAtEnd(inner, close[close.length - 1]))
|
|
940
|
+
continue;
|
|
941
|
+
const parsed = parseLabel(stripQuotedLabel(inner));
|
|
942
|
+
return { label: parsed.text, labelRuns: parsed.runs, shape };
|
|
937
943
|
}
|
|
938
944
|
return null;
|
|
939
945
|
}
|
|
946
|
+
/**
|
|
947
|
+
* Whether an unquoted label leaves its shape's bracket open to the end: its `[`/`]` (or `(`/`)`,
|
|
948
|
+
* `{`/`}`, after `closing`) are balanced and never close more than they opened. Double-quoted text is
|
|
949
|
+
* skipped, so `A["a] x [b"]` stays one label, as in Mermaid.
|
|
950
|
+
*/
|
|
951
|
+
function closesOnlyAtEnd(inner, closing) {
|
|
952
|
+
const opening = closing === ']' ? '[' : closing === ')' ? '(' : '{';
|
|
953
|
+
let depth = 0;
|
|
954
|
+
let inQuotes = false;
|
|
955
|
+
for (const ch of inner) {
|
|
956
|
+
if (ch === '"')
|
|
957
|
+
inQuotes = !inQuotes;
|
|
958
|
+
else if (inQuotes)
|
|
959
|
+
continue;
|
|
960
|
+
else if (ch === opening)
|
|
961
|
+
depth++;
|
|
962
|
+
else if (ch === closing && --depth < 0)
|
|
963
|
+
return false;
|
|
964
|
+
}
|
|
965
|
+
return depth === 0;
|
|
966
|
+
}
|
|
940
967
|
/** Parse a node reference at an edge endpoint: bare id or id with shape. */
|
|
941
968
|
function parseNodeRef(ref) {
|
|
942
969
|
const trimmed = ref.trim();
|
|
@@ -964,14 +991,8 @@ function parseNodeRef(ref) {
|
|
|
964
991
|
className,
|
|
965
992
|
};
|
|
966
993
|
}
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
const inner = rest.slice(open.length, rest.length - close.length).trim();
|
|
970
|
-
const parsed = parseLabel(stripQuotedLabel(inner));
|
|
971
|
-
return { id, label: parsed.text, labelRuns: parsed.runs, shape, className };
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
return null;
|
|
994
|
+
const bracketed = matchShapeSyntax(rest);
|
|
995
|
+
return bracketed ? { id, ...bracketed, className } : null;
|
|
975
996
|
}
|
|
976
997
|
/**
|
|
977
998
|
* Try to match a mid-chain-label edge (`A-- text -->B`) starting at `start`,
|
|
@@ -989,10 +1010,10 @@ function matchMidLabelEdge(line, start) {
|
|
|
989
1010
|
let j = start + family.open.length;
|
|
990
1011
|
while (j < line.length) {
|
|
991
1012
|
if (depth === 0) {
|
|
992
|
-
const closing =
|
|
1013
|
+
const closing = matchClosing(line, j, family);
|
|
993
1014
|
if (closing) {
|
|
994
1015
|
const parsed = parseLabel(stripQuotedLabel(line.slice(start + family.open.length, j).trim()));
|
|
995
|
-
return { type: closing.type, end: j + closing.
|
|
1016
|
+
return { type: closing.type, end: j + closing.length, label: parsed.text, labelRuns: parsed.runs };
|
|
996
1017
|
}
|
|
997
1018
|
}
|
|
998
1019
|
const ch = line[j];
|