@md2nativedocx/core 0.1.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/README.md +15 -0
- package/dist/diagrams/architecture-diagram/parser.d.ts +29 -0
- package/dist/diagrams/architecture-diagram/parser.js +142 -0
- package/dist/diagrams/architecture-diagram/parser.js.map +1 -0
- package/dist/diagrams/architecture-diagram/translator.d.ts +41 -0
- package/dist/diagrams/architecture-diagram/translator.js +172 -0
- package/dist/diagrams/architecture-diagram/translator.js.map +1 -0
- package/dist/diagrams/architecture-diagram/types.d.ts +39 -0
- package/dist/diagrams/architecture-diagram/types.js +18 -0
- package/dist/diagrams/architecture-diagram/types.js.map +1 -0
- package/dist/diagrams/block/parser.d.ts +17 -0
- package/dist/diagrams/block/parser.js +312 -0
- package/dist/diagrams/block/parser.js.map +1 -0
- package/dist/diagrams/block/translator.d.ts +16 -0
- package/dist/diagrams/block/translator.js +137 -0
- package/dist/diagrams/block/translator.js.map +1 -0
- package/dist/diagrams/block/types.d.ts +38 -0
- package/dist/diagrams/block/types.js +6 -0
- package/dist/diagrams/block/types.js.map +1 -0
- package/dist/diagrams/c4/parser.d.ts +29 -0
- package/dist/diagrams/c4/parser.js +264 -0
- package/dist/diagrams/c4/parser.js.map +1 -0
- package/dist/diagrams/c4/translator.d.ts +50 -0
- package/dist/diagrams/c4/translator.js +240 -0
- package/dist/diagrams/c4/translator.js.map +1 -0
- package/dist/diagrams/c4/types.d.ts +74 -0
- package/dist/diagrams/c4/types.js +46 -0
- package/dist/diagrams/c4/types.js.map +1 -0
- package/dist/diagrams/class-diagram/parser.d.ts +40 -0
- package/dist/diagrams/class-diagram/parser.js +213 -0
- package/dist/diagrams/class-diagram/parser.js.map +1 -0
- package/dist/diagrams/class-diagram/translator.d.ts +48 -0
- package/dist/diagrams/class-diagram/translator.js +197 -0
- package/dist/diagrams/class-diagram/translator.js.map +1 -0
- package/dist/diagrams/class-diagram/types.d.ts +54 -0
- package/dist/diagrams/class-diagram/types.js +16 -0
- package/dist/diagrams/class-diagram/types.js.map +1 -0
- package/dist/diagrams/cynefin/parser.d.ts +24 -0
- package/dist/diagrams/cynefin/parser.js +102 -0
- package/dist/diagrams/cynefin/parser.js.map +1 -0
- package/dist/diagrams/cynefin/translator.d.ts +37 -0
- package/dist/diagrams/cynefin/translator.js +190 -0
- package/dist/diagrams/cynefin/translator.js.map +1 -0
- package/dist/diagrams/cynefin/types.d.ts +45 -0
- package/dist/diagrams/cynefin/types.js +36 -0
- package/dist/diagrams/cynefin/types.js.map +1 -0
- package/dist/diagrams/er-diagram/parser.d.ts +29 -0
- package/dist/diagrams/er-diagram/parser.js +156 -0
- package/dist/diagrams/er-diagram/parser.js.map +1 -0
- package/dist/diagrams/er-diagram/translator.d.ts +26 -0
- package/dist/diagrams/er-diagram/translator.js +151 -0
- package/dist/diagrams/er-diagram/translator.js.map +1 -0
- package/dist/diagrams/er-diagram/types.d.ts +44 -0
- package/dist/diagrams/er-diagram/types.js +17 -0
- package/dist/diagrams/er-diagram/types.js.map +1 -0
- package/dist/diagrams/event-modeling/parser.d.ts +16 -0
- package/dist/diagrams/event-modeling/parser.js +193 -0
- package/dist/diagrams/event-modeling/parser.js.map +1 -0
- package/dist/diagrams/event-modeling/translator.d.ts +17 -0
- package/dist/diagrams/event-modeling/translator.js +154 -0
- package/dist/diagrams/event-modeling/translator.js.map +1 -0
- package/dist/diagrams/event-modeling/types.d.ts +46 -0
- package/dist/diagrams/event-modeling/types.js +6 -0
- package/dist/diagrams/event-modeling/types.js.map +1 -0
- package/dist/diagrams/gantt/date-utils.d.ts +51 -0
- package/dist/diagrams/gantt/date-utils.js +131 -0
- package/dist/diagrams/gantt/date-utils.js.map +1 -0
- package/dist/diagrams/gantt/parser.d.ts +37 -0
- package/dist/diagrams/gantt/parser.js +221 -0
- package/dist/diagrams/gantt/parser.js.map +1 -0
- package/dist/diagrams/gantt/translator.d.ts +23 -0
- package/dist/diagrams/gantt/translator.js +259 -0
- package/dist/diagrams/gantt/translator.js.map +1 -0
- package/dist/diagrams/gantt/types.d.ts +31 -0
- package/dist/diagrams/gantt/types.js +14 -0
- package/dist/diagrams/gantt/types.js.map +1 -0
- package/dist/diagrams/git-graph/parser.d.ts +29 -0
- package/dist/diagrams/git-graph/parser.js +237 -0
- package/dist/diagrams/git-graph/parser.js.map +1 -0
- package/dist/diagrams/git-graph/translator.d.ts +49 -0
- package/dist/diagrams/git-graph/translator.js +224 -0
- package/dist/diagrams/git-graph/translator.js.map +1 -0
- package/dist/diagrams/git-graph/types.d.ts +72 -0
- package/dist/diagrams/git-graph/types.js +35 -0
- package/dist/diagrams/git-graph/types.js.map +1 -0
- package/dist/diagrams/ishikawa/parser.d.ts +15 -0
- package/dist/diagrams/ishikawa/parser.js +85 -0
- package/dist/diagrams/ishikawa/parser.js.map +1 -0
- package/dist/diagrams/ishikawa/translator.d.ts +12 -0
- package/dist/diagrams/ishikawa/translator.js +95 -0
- package/dist/diagrams/ishikawa/translator.js.map +1 -0
- package/dist/diagrams/ishikawa/types.d.ts +19 -0
- package/dist/diagrams/ishikawa/types.js +6 -0
- package/dist/diagrams/ishikawa/types.js.map +1 -0
- package/dist/diagrams/journey/parser.d.ts +14 -0
- package/dist/diagrams/journey/parser.js +78 -0
- package/dist/diagrams/journey/parser.js.map +1 -0
- package/dist/diagrams/journey/translator.d.ts +11 -0
- package/dist/diagrams/journey/translator.js +90 -0
- package/dist/diagrams/journey/translator.js.map +1 -0
- package/dist/diagrams/journey/types.d.ts +20 -0
- package/dist/diagrams/journey/types.js +6 -0
- package/dist/diagrams/journey/types.js.map +1 -0
- package/dist/diagrams/kanban/parser.d.ts +17 -0
- package/dist/diagrams/kanban/parser.js +129 -0
- package/dist/diagrams/kanban/parser.js.map +1 -0
- package/dist/diagrams/kanban/translator.d.ts +13 -0
- package/dist/diagrams/kanban/translator.js +72 -0
- package/dist/diagrams/kanban/translator.js.map +1 -0
- package/dist/diagrams/kanban/types.d.ts +21 -0
- package/dist/diagrams/kanban/types.js +6 -0
- package/dist/diagrams/kanban/types.js.map +1 -0
- package/dist/diagrams/mindmap/parser.d.ts +23 -0
- package/dist/diagrams/mindmap/parser.js +108 -0
- package/dist/diagrams/mindmap/parser.js.map +1 -0
- package/dist/diagrams/mindmap/translator.d.ts +31 -0
- package/dist/diagrams/mindmap/translator.js +249 -0
- package/dist/diagrams/mindmap/translator.js.map +1 -0
- package/dist/diagrams/mindmap/types.d.ts +19 -0
- package/dist/diagrams/mindmap/types.js +10 -0
- package/dist/diagrams/mindmap/types.js.map +1 -0
- package/dist/diagrams/packet/parser.d.ts +15 -0
- package/dist/diagrams/packet/parser.js +99 -0
- package/dist/diagrams/packet/parser.js.map +1 -0
- package/dist/diagrams/packet/translator.d.ts +14 -0
- package/dist/diagrams/packet/translator.js +66 -0
- package/dist/diagrams/packet/translator.js.map +1 -0
- package/dist/diagrams/packet/types.d.ts +14 -0
- package/dist/diagrams/packet/types.js +6 -0
- package/dist/diagrams/packet/types.js.map +1 -0
- package/dist/diagrams/pie/chart.d.ts +26 -0
- package/dist/diagrams/pie/chart.js +64 -0
- package/dist/diagrams/pie/chart.js.map +1 -0
- package/dist/diagrams/pie/parser.d.ts +18 -0
- package/dist/diagrams/pie/parser.js +93 -0
- package/dist/diagrams/pie/parser.js.map +1 -0
- package/dist/diagrams/pie/translator.d.ts +15 -0
- package/dist/diagrams/pie/translator.js +150 -0
- package/dist/diagrams/pie/translator.js.map +1 -0
- package/dist/diagrams/pie/types.d.ts +16 -0
- package/dist/diagrams/pie/types.js +6 -0
- package/dist/diagrams/pie/types.js.map +1 -0
- package/dist/diagrams/quadrant/parser.d.ts +24 -0
- package/dist/diagrams/quadrant/parser.js +96 -0
- package/dist/diagrams/quadrant/parser.js.map +1 -0
- package/dist/diagrams/quadrant/translator.d.ts +21 -0
- package/dist/diagrams/quadrant/translator.js +204 -0
- package/dist/diagrams/quadrant/translator.js.map +1 -0
- package/dist/diagrams/quadrant/types.d.ts +41 -0
- package/dist/diagrams/quadrant/types.js +10 -0
- package/dist/diagrams/quadrant/types.js.map +1 -0
- package/dist/diagrams/radar/chart.d.ts +21 -0
- package/dist/diagrams/radar/chart.js +65 -0
- package/dist/diagrams/radar/chart.js.map +1 -0
- package/dist/diagrams/radar/parser.d.ts +15 -0
- package/dist/diagrams/radar/parser.js +186 -0
- package/dist/diagrams/radar/parser.js.map +1 -0
- package/dist/diagrams/radar/translator.d.ts +12 -0
- package/dist/diagrams/radar/translator.js +98 -0
- package/dist/diagrams/radar/translator.js.map +1 -0
- package/dist/diagrams/radar/types.d.ts +29 -0
- package/dist/diagrams/radar/types.js +6 -0
- package/dist/diagrams/radar/types.js.map +1 -0
- package/dist/diagrams/requirement-diagram/parser.d.ts +29 -0
- package/dist/diagrams/requirement-diagram/parser.js +156 -0
- package/dist/diagrams/requirement-diagram/parser.js.map +1 -0
- package/dist/diagrams/requirement-diagram/translator.d.ts +24 -0
- package/dist/diagrams/requirement-diagram/translator.js +214 -0
- package/dist/diagrams/requirement-diagram/translator.js.map +1 -0
- package/dist/diagrams/requirement-diagram/types.d.ts +37 -0
- package/dist/diagrams/requirement-diagram/types.js +18 -0
- package/dist/diagrams/requirement-diagram/types.js.map +1 -0
- package/dist/diagrams/sankey/parser.d.ts +14 -0
- package/dist/diagrams/sankey/parser.js +123 -0
- package/dist/diagrams/sankey/parser.js.map +1 -0
- package/dist/diagrams/sankey/translator.d.ts +15 -0
- package/dist/diagrams/sankey/translator.js +132 -0
- package/dist/diagrams/sankey/translator.js.map +1 -0
- package/dist/diagrams/sankey/types.d.ts +16 -0
- package/dist/diagrams/sankey/types.js +6 -0
- package/dist/diagrams/sankey/types.js.map +1 -0
- package/dist/diagrams/sequence/parser.d.ts +22 -0
- package/dist/diagrams/sequence/parser.js +256 -0
- package/dist/diagrams/sequence/parser.js.map +1 -0
- package/dist/diagrams/sequence/translator.d.ts +21 -0
- package/dist/diagrams/sequence/translator.js +309 -0
- package/dist/diagrams/sequence/translator.js.map +1 -0
- package/dist/diagrams/sequence/types.d.ts +62 -0
- package/dist/diagrams/sequence/types.js +8 -0
- package/dist/diagrams/sequence/types.js.map +1 -0
- package/dist/diagrams/state-diagram/parser.d.ts +37 -0
- package/dist/diagrams/state-diagram/parser.js +215 -0
- package/dist/diagrams/state-diagram/parser.js.map +1 -0
- package/dist/diagrams/state-diagram/translator.d.ts +25 -0
- package/dist/diagrams/state-diagram/translator.js +165 -0
- package/dist/diagrams/state-diagram/translator.js.map +1 -0
- package/dist/diagrams/state-diagram/types.d.ts +38 -0
- package/dist/diagrams/state-diagram/types.js +13 -0
- package/dist/diagrams/state-diagram/types.js.map +1 -0
- package/dist/diagrams/timeline/parser.d.ts +17 -0
- package/dist/diagrams/timeline/parser.js +82 -0
- package/dist/diagrams/timeline/parser.js.map +1 -0
- package/dist/diagrams/timeline/translator.d.ts +13 -0
- package/dist/diagrams/timeline/translator.js +90 -0
- package/dist/diagrams/timeline/translator.js.map +1 -0
- package/dist/diagrams/timeline/types.d.ts +18 -0
- package/dist/diagrams/timeline/types.js +6 -0
- package/dist/diagrams/timeline/types.js.map +1 -0
- package/dist/diagrams/tree-view/parser.d.ts +16 -0
- package/dist/diagrams/tree-view/parser.js +129 -0
- package/dist/diagrams/tree-view/parser.js.map +1 -0
- package/dist/diagrams/tree-view/translator.d.ts +11 -0
- package/dist/diagrams/tree-view/translator.js +73 -0
- package/dist/diagrams/tree-view/translator.js.map +1 -0
- package/dist/diagrams/tree-view/types.d.ts +22 -0
- package/dist/diagrams/tree-view/types.js +6 -0
- package/dist/diagrams/tree-view/types.js.map +1 -0
- package/dist/diagrams/treemap/parser.d.ts +15 -0
- package/dist/diagrams/treemap/parser.js +131 -0
- package/dist/diagrams/treemap/parser.js.map +1 -0
- package/dist/diagrams/treemap/translator.d.ts +15 -0
- package/dist/diagrams/treemap/translator.js +136 -0
- package/dist/diagrams/treemap/translator.js.map +1 -0
- package/dist/diagrams/treemap/types.d.ts +22 -0
- package/dist/diagrams/treemap/types.js +6 -0
- package/dist/diagrams/treemap/types.js.map +1 -0
- package/dist/diagrams/venn/parser.d.ts +27 -0
- package/dist/diagrams/venn/parser.js +129 -0
- package/dist/diagrams/venn/parser.js.map +1 -0
- package/dist/diagrams/venn/translator.d.ts +25 -0
- package/dist/diagrams/venn/translator.js +214 -0
- package/dist/diagrams/venn/translator.js.map +1 -0
- package/dist/diagrams/venn/types.d.ts +33 -0
- package/dist/diagrams/venn/types.js +11 -0
- package/dist/diagrams/venn/types.js.map +1 -0
- package/dist/diagrams/wardley/parser.d.ts +14 -0
- package/dist/diagrams/wardley/parser.js +254 -0
- package/dist/diagrams/wardley/parser.js.map +1 -0
- package/dist/diagrams/wardley/translator.d.ts +15 -0
- package/dist/diagrams/wardley/translator.js +196 -0
- package/dist/diagrams/wardley/translator.js.map +1 -0
- package/dist/diagrams/wardley/types.d.ts +74 -0
- package/dist/diagrams/wardley/types.js +8 -0
- package/dist/diagrams/wardley/types.js.map +1 -0
- package/dist/diagrams/xychart/chart.d.ts +24 -0
- package/dist/diagrams/xychart/chart.js +113 -0
- package/dist/diagrams/xychart/chart.js.map +1 -0
- package/dist/diagrams/xychart/parser.d.ts +16 -0
- package/dist/diagrams/xychart/parser.js +190 -0
- package/dist/diagrams/xychart/parser.js.map +1 -0
- package/dist/diagrams/xychart/translator.d.ts +14 -0
- package/dist/diagrams/xychart/translator.js +187 -0
- package/dist/diagrams/xychart/translator.js.map +1 -0
- package/dist/diagrams/xychart/types.d.ts +32 -0
- package/dist/diagrams/xychart/types.js +6 -0
- package/dist/diagrams/xychart/types.js.map +1 -0
- package/dist/diagrams/zenuml/parser.d.ts +21 -0
- package/dist/diagrams/zenuml/parser.js +228 -0
- package/dist/diagrams/zenuml/parser.js.map +1 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +189 -0
- package/dist/index.js.map +1 -0
- package/dist/label-runs.d.ts +16 -0
- package/dist/label-runs.js +30 -0
- package/dist/label-runs.js.map +1 -0
- package/dist/layout/layout.d.ts +53 -0
- package/dist/layout/layout.js +536 -0
- package/dist/layout/layout.js.map +1 -0
- package/dist/parser/diagram-type.d.ts +41 -0
- package/dist/parser/diagram-type.js +97 -0
- package/dist/parser/diagram-type.js.map +1 -0
- package/dist/parser/index.d.ts +2 -0
- package/dist/parser/index.js +2 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/parser.d.ts +25 -0
- package/dist/parser/parser.js +1134 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/render-diagram.d.ts +156 -0
- package/dist/render-diagram.js +326 -0
- package/dist/render-diagram.js.map +1 -0
- package/dist/rendering-plan.d.ts +47 -0
- package/dist/rendering-plan.js +43 -0
- package/dist/rendering-plan.js.map +1 -0
- package/dist/smartart/chain.d.ts +216 -0
- package/dist/smartart/chain.js +558 -0
- package/dist/smartart/chain.js.map +1 -0
- package/dist/smartart/classify.d.ts +96 -0
- package/dist/smartart/classify.js +162 -0
- package/dist/smartart/classify.js.map +1 -0
- package/dist/smartart/cycle.d.ts +91 -0
- package/dist/smartart/cycle.js +323 -0
- package/dist/smartart/cycle.js.map +1 -0
- package/dist/smartart/dispatch.d.ts +48 -0
- package/dist/smartart/dispatch.js +49 -0
- package/dist/smartart/dispatch.js.map +1 -0
- package/dist/smartart/drawing.d.ts +155 -0
- package/dist/smartart/drawing.js +574 -0
- package/dist/smartart/drawing.js.map +1 -0
- package/dist/smartart/embed.d.ts +78 -0
- package/dist/smartart/embed.js +136 -0
- package/dist/smartart/embed.js.map +1 -0
- package/dist/smartart/from-graph.d.ts +42 -0
- package/dist/smartart/from-graph.js +106 -0
- package/dist/smartart/from-graph.js.map +1 -0
- package/dist/smartart/from-timeline.d.ts +30 -0
- package/dist/smartart/from-timeline.js +59 -0
- package/dist/smartart/from-timeline.js.map +1 -0
- package/dist/smartart/from-tree.d.ts +36 -0
- package/dist/smartart/from-tree.js +74 -0
- package/dist/smartart/from-tree.js.map +1 -0
- package/dist/smartart/generate-options.d.ts +12 -0
- package/dist/smartart/generate-options.js +2 -0
- package/dist/smartart/generate-options.js.map +1 -0
- package/dist/smartart/kanban.d.ts +24 -0
- package/dist/smartart/kanban.js +250 -0
- package/dist/smartart/kanban.js.map +1 -0
- package/dist/smartart/node-shape.d.ts +16 -0
- package/dist/smartart/node-shape.js +22 -0
- package/dist/smartart/node-shape.js.map +1 -0
- package/dist/smartart/styles.d.ts +114 -0
- package/dist/smartart/styles.js +141 -0
- package/dist/smartart/styles.js.map +1 -0
- package/dist/smartart/text.d.ts +16 -0
- package/dist/smartart/text.js +48 -0
- package/dist/smartart/text.js.map +1 -0
- package/dist/smartart/timeline-grouped.d.ts +40 -0
- package/dist/smartart/timeline-grouped.js +279 -0
- package/dist/smartart/timeline-grouped.js.map +1 -0
- package/dist/smartart/timeline.d.ts +51 -0
- package/dist/smartart/timeline.js +276 -0
- package/dist/smartart/timeline.js.map +1 -0
- package/dist/smartart/tree-deep.d.ts +61 -0
- package/dist/smartart/tree-deep.js +293 -0
- package/dist/smartart/tree-deep.js.map +1 -0
- package/dist/smartart/tree.d.ts +159 -0
- package/dist/smartart/tree.js +497 -0
- package/dist/smartart/tree.js.map +1 -0
- package/dist/translator/boxes.d.ts +36 -0
- package/dist/translator/boxes.js +95 -0
- package/dist/translator/boxes.js.map +1 -0
- package/dist/translator/canvas.d.ts +82 -0
- package/dist/translator/canvas.js +135 -0
- package/dist/translator/canvas.js.map +1 -0
- package/dist/translator/graph-shapes.d.ts +98 -0
- package/dist/translator/graph-shapes.js +223 -0
- package/dist/translator/graph-shapes.js.map +1 -0
- package/dist/translator/native-chart.d.ts +64 -0
- package/dist/translator/native-chart.js +109 -0
- package/dist/translator/native-chart.js.map +1 -0
- package/dist/translator/ooxml-translator.d.ts +79 -0
- package/dist/translator/ooxml-translator.js +1213 -0
- package/dist/translator/ooxml-translator.js.map +1 -0
- package/dist/translator/path-shape.d.ts +11 -0
- package/dist/translator/path-shape.js +37 -0
- package/dist/translator/path-shape.js.map +1 -0
- package/dist/translator/unsupported-diagram-note.d.ts +16 -0
- package/dist/translator/unsupported-diagram-note.js +34 -0
- package/dist/translator/unsupported-diagram-note.js.map +1 -0
- package/dist/translator/xml-escape.d.ts +28 -0
- package/dist/translator/xml-escape.js +40 -0
- package/dist/translator/xml-escape.js.map +1 -0
- package/dist/types.d.ts +158 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,1134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mermaid flowchart parser -> intermediate AST.
|
|
3
|
+
*
|
|
4
|
+
* V1 scope (spec §5.1): `graph`/`flowchart` with `TD`/`LR` direction only.
|
|
5
|
+
* Supports node shapes from §6.1, edge types from §6.2, and `subgraph`
|
|
6
|
+
* containers. `classDef`/`style` are intentionally ignored in V1 (§6.3).
|
|
7
|
+
*
|
|
8
|
+
* This module is a pure function from untrusted text to a typed AST. It never
|
|
9
|
+
* emits XML itself — escaping happens in the translator.
|
|
10
|
+
*/
|
|
11
|
+
/** Shape of a node based on its bracket syntax (spec §6.1). */
|
|
12
|
+
const SHAPE_BY_SYNTAX = [
|
|
13
|
+
// Longest/most specific patterns first, so e.g. `[[` matches subroutine
|
|
14
|
+
// before `[` matches rect, and `(((` matches doubleCircle before `((`
|
|
15
|
+
// matches ellipse.
|
|
16
|
+
{ open: '(((', close: ')))', shape: 'doubleCircle' },
|
|
17
|
+
{ open: '([', close: '])', shape: 'stadium' },
|
|
18
|
+
{ open: '[(', close: ')]', shape: 'cylinder' },
|
|
19
|
+
{ open: '((', close: '))', shape: 'ellipse' },
|
|
20
|
+
{ open: '[[', close: ']]', shape: 'subroutine' },
|
|
21
|
+
{ open: '[/', close: '\\]', shape: 'trapezoid' },
|
|
22
|
+
{ open: '[\\', close: '/]', shape: 'trapezoidAlt' },
|
|
23
|
+
{ open: '[/', close: '/]', shape: 'parallelogram' },
|
|
24
|
+
{ open: '[\\', close: '\\]', shape: 'parallelogramAlt' },
|
|
25
|
+
{ open: '{{', close: '}}', shape: 'hexagon' },
|
|
26
|
+
{ open: '[', close: ']', shape: 'rect' },
|
|
27
|
+
{ open: '(', close: ')', shape: 'roundRect' },
|
|
28
|
+
{ open: '{', close: '}', shape: 'diamond' },
|
|
29
|
+
{ open: '>', close: ']', shape: 'asymmetric' },
|
|
30
|
+
];
|
|
31
|
+
/**
|
|
32
|
+
* Shape name (lowercased) -> {@link NodeShape} for the v11.3+ generic
|
|
33
|
+
* `id@{ shape: name, label: "text" }` syntax (spec §6.1 addendum). Sourced
|
|
34
|
+
* from Mermaid's own "Semantic Name" / "Shape Name Aliases" table
|
|
35
|
+
* (https://mermaid.js.org/syntax/flowchart.html), keeping only the names
|
|
36
|
+
* with a faithful single-preset DrawingML match — reused where an alias is
|
|
37
|
+
* just another name for a shape the bracket syntax already supports (e.g.
|
|
38
|
+
* `rounded` -> `roundRect`), and mapped to a new {@link NodeShape} where
|
|
39
|
+
* Word's flowchart preset geometry family (`flowChart*`) or a basic shape
|
|
40
|
+
* (`lightningBolt`, `leftBrace`/`rightBrace`/`bracePair`) has a direct
|
|
41
|
+
* equivalent. Deliberately NOT covered — no single preset renders them
|
|
42
|
+
* faithfully without compound shapes or custom vector paths (`bang`,
|
|
43
|
+
* `browser`, `bucket`, `cloud`, `console`, `data-store`, `divided-process`,
|
|
44
|
+
* `folder`, `fork`/`join`, `lined-document`, `lined-process`, `loop-limit`,
|
|
45
|
+
* `multi-document`, `multi-process`, `person`, `tagged-document`,
|
|
46
|
+
* `tagged-process`) — {@link normalizeShapeAlias} falls back to `rect` for
|
|
47
|
+
* these rather than losing the node, per spec §10's never-lose-data-silently
|
|
48
|
+
* rule (see the mid-chain-label and multi-line-label fixes in TODO.md for
|
|
49
|
+
* the two prior bugs in that same class).
|
|
50
|
+
*/
|
|
51
|
+
const SHAPE_ALIAS_MAP = {
|
|
52
|
+
proc: 'rect',
|
|
53
|
+
process: 'rect',
|
|
54
|
+
rect: 'rect',
|
|
55
|
+
rectangle: 'rect',
|
|
56
|
+
odd: 'rect',
|
|
57
|
+
event: 'roundRect',
|
|
58
|
+
rounded: 'roundRect',
|
|
59
|
+
pill: 'stadium',
|
|
60
|
+
stadium: 'stadium',
|
|
61
|
+
terminal: 'stadium',
|
|
62
|
+
'fr-rect': 'subroutine',
|
|
63
|
+
'framed-rectangle': 'subroutine',
|
|
64
|
+
subproc: 'subroutine',
|
|
65
|
+
subprocess: 'subroutine',
|
|
66
|
+
subroutine: 'subroutine',
|
|
67
|
+
cyl: 'cylinder',
|
|
68
|
+
cylinder: 'cylinder',
|
|
69
|
+
database: 'cylinder',
|
|
70
|
+
db: 'cylinder',
|
|
71
|
+
decision: 'diamond',
|
|
72
|
+
diamond: 'diamond',
|
|
73
|
+
diam: 'diamond',
|
|
74
|
+
question: 'diamond',
|
|
75
|
+
hex: 'hexagon',
|
|
76
|
+
hexagon: 'hexagon',
|
|
77
|
+
prepare: 'hexagon',
|
|
78
|
+
'in-out': 'parallelogram',
|
|
79
|
+
'lean-r': 'parallelogram',
|
|
80
|
+
'lean-right': 'parallelogram',
|
|
81
|
+
'lean-l': 'parallelogramAlt',
|
|
82
|
+
'lean-left': 'parallelogramAlt',
|
|
83
|
+
'out-in': 'parallelogramAlt',
|
|
84
|
+
priority: 'trapezoid',
|
|
85
|
+
'trap-b': 'trapezoid',
|
|
86
|
+
trapezoid: 'trapezoid',
|
|
87
|
+
'trapezoid-bottom': 'trapezoid',
|
|
88
|
+
'inv-trapezoid': 'trapezoidAlt',
|
|
89
|
+
manual: 'trapezoidAlt',
|
|
90
|
+
'trap-t': 'trapezoidAlt',
|
|
91
|
+
'trapezoid-top': 'trapezoidAlt',
|
|
92
|
+
circ: 'ellipse',
|
|
93
|
+
circle: 'ellipse',
|
|
94
|
+
'sm-circ': 'ellipse',
|
|
95
|
+
'small-circle': 'ellipse',
|
|
96
|
+
start: 'ellipse',
|
|
97
|
+
'dbl-circ': 'doubleCircle',
|
|
98
|
+
'double-circle': 'doubleCircle',
|
|
99
|
+
'fr-circ': 'doubleCircle',
|
|
100
|
+
'framed-circle': 'doubleCircle',
|
|
101
|
+
stop: 'doubleCircle',
|
|
102
|
+
doc: 'document',
|
|
103
|
+
document: 'document',
|
|
104
|
+
card: 'card',
|
|
105
|
+
'notched-rectangle': 'card',
|
|
106
|
+
delay: 'delay',
|
|
107
|
+
'half-rounded-rectangle': 'delay',
|
|
108
|
+
extract: 'triangle',
|
|
109
|
+
tri: 'triangle',
|
|
110
|
+
triangle: 'triangle',
|
|
111
|
+
'flipped-triangle': 'triangleInverted',
|
|
112
|
+
'manual-file': 'triangleInverted',
|
|
113
|
+
'flip-tri': 'triangleInverted',
|
|
114
|
+
'internal-storage': 'windowPane',
|
|
115
|
+
'win-pane': 'windowPane',
|
|
116
|
+
'window-pane': 'windowPane',
|
|
117
|
+
collate: 'hourglass',
|
|
118
|
+
hourglass: 'hourglass',
|
|
119
|
+
'curved-trapezoid': 'curvedTrapezoid',
|
|
120
|
+
'curv-trap': 'curvedTrapezoid',
|
|
121
|
+
display: 'curvedTrapezoid',
|
|
122
|
+
'com-link': 'bolt',
|
|
123
|
+
'lightning-bolt': 'bolt',
|
|
124
|
+
brace: 'braceLeft',
|
|
125
|
+
'brace-l': 'braceLeft',
|
|
126
|
+
comment: 'braceLeft',
|
|
127
|
+
'brace-r': 'braceRight',
|
|
128
|
+
braces: 'bracePair',
|
|
129
|
+
'cross-circ': 'crossedCircle',
|
|
130
|
+
'crossed-circle': 'crossedCircle',
|
|
131
|
+
summary: 'crossedCircle',
|
|
132
|
+
'f-circ': 'filledCircle',
|
|
133
|
+
'filled-circle': 'filledCircle',
|
|
134
|
+
junction: 'filledCircle',
|
|
135
|
+
flag: 'paperTape',
|
|
136
|
+
'paper-tape': 'paperTape',
|
|
137
|
+
das: 'horizontalCylinder',
|
|
138
|
+
'h-cyl': 'horizontalCylinder',
|
|
139
|
+
'horizontal-cylinder': 'horizontalCylinder',
|
|
140
|
+
disk: 'linedCylinder',
|
|
141
|
+
'lin-cyl': 'linedCylinder',
|
|
142
|
+
'lined-cylinder': 'linedCylinder',
|
|
143
|
+
'manual-input': 'manualInput',
|
|
144
|
+
'sl-rect': 'manualInput',
|
|
145
|
+
'sloped-rectangle': 'manualInput',
|
|
146
|
+
};
|
|
147
|
+
/** Look up a `shape:` value from `SHAPE_ALIAS_MAP`, defaulting to `rect` for
|
|
148
|
+
* an unrecognized or deliberately-unsupported name (see that map's doc
|
|
149
|
+
* comment) rather than failing the whole node/edge statement. */
|
|
150
|
+
function normalizeShapeAlias(raw) {
|
|
151
|
+
return SHAPE_ALIAS_MAP[stripQuotedLabel(raw.trim()).toLowerCase()] ?? 'rect';
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Parse the body of an `@{ ... }` property bag (`shape: circle, label: "Hi, there"`)
|
|
155
|
+
* into a lowercase-keyed map. A quoted value (`"..."`) is read verbatim up to
|
|
156
|
+
* its closing quote — including any `,`/`:` inside — so a label containing
|
|
157
|
+
* punctuation doesn't fragment the scan; an unquoted value runs to the next
|
|
158
|
+
* top-level comma. Malformed tails (no `:` after a key) are dropped rather
|
|
159
|
+
* than throwing, consistent with this parser's tolerant-degradation style.
|
|
160
|
+
*/
|
|
161
|
+
function parseAtShapeProps(inner) {
|
|
162
|
+
const props = new Map();
|
|
163
|
+
const n = inner.length;
|
|
164
|
+
let i = 0;
|
|
165
|
+
while (i < n) {
|
|
166
|
+
while (i < n && (inner[i] === ',' || /\s/.test(inner[i])))
|
|
167
|
+
i++;
|
|
168
|
+
if (i >= n)
|
|
169
|
+
break;
|
|
170
|
+
const keyStart = i;
|
|
171
|
+
while (i < n && inner[i] !== ':')
|
|
172
|
+
i++;
|
|
173
|
+
if (i >= n)
|
|
174
|
+
break;
|
|
175
|
+
const key = inner.slice(keyStart, i).trim().toLowerCase();
|
|
176
|
+
i++; // skip ':'
|
|
177
|
+
while (i < n && /\s/.test(inner[i]))
|
|
178
|
+
i++;
|
|
179
|
+
let value;
|
|
180
|
+
if (inner[i] === '"') {
|
|
181
|
+
i++;
|
|
182
|
+
const valueStart = i;
|
|
183
|
+
while (i < n && inner[i] !== '"')
|
|
184
|
+
i++;
|
|
185
|
+
value = inner.slice(valueStart, i);
|
|
186
|
+
if (i < n)
|
|
187
|
+
i++; // skip closing quote
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
const valueStart = i;
|
|
191
|
+
while (i < n && inner[i] !== ',')
|
|
192
|
+
i++;
|
|
193
|
+
value = inner.slice(valueStart, i).trim();
|
|
194
|
+
}
|
|
195
|
+
if (key.length > 0)
|
|
196
|
+
props.set(key, value);
|
|
197
|
+
}
|
|
198
|
+
return props;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Recognize the v11.3+ generic shape syntax, `id@{ shape: name, label: "text" }`
|
|
202
|
+
* — the id itself is consumed by the caller's existing id regex (`@` isn't an
|
|
203
|
+
* id character), so `rest` here is just the `@{...}` tail. Returns `null` for
|
|
204
|
+
* anything else (falls through to the bracket-syntax loop). A `label` prop is
|
|
205
|
+
* optional (Mermaid defaults it to the node id, same as a bracket-less bare
|
|
206
|
+
* node) — signaled here as `null` so the caller can apply that default.
|
|
207
|
+
*/
|
|
208
|
+
function parseAtShapeSyntax(rest) {
|
|
209
|
+
if (!rest.startsWith('@{') || !rest.endsWith('}'))
|
|
210
|
+
return null;
|
|
211
|
+
const props = parseAtShapeProps(rest.slice(2, -1));
|
|
212
|
+
const shape = props.has('shape') ? normalizeShapeAlias(props.get('shape')) : 'rect';
|
|
213
|
+
const parsed = props.has('label') ? parseLabel(stripQuotedLabel(props.get('label').trim())) : null;
|
|
214
|
+
return { shape, label: parsed?.text ?? null, labelRuns: parsed?.runs ?? null };
|
|
215
|
+
}
|
|
216
|
+
function fixedOp(token, type) {
|
|
217
|
+
return { type, match: (line, at) => (line.startsWith(token, at) ? token.length : null) };
|
|
218
|
+
}
|
|
219
|
+
function regexOp(regex, type) {
|
|
220
|
+
return {
|
|
221
|
+
type,
|
|
222
|
+
match: (line, at) => {
|
|
223
|
+
const m = regex.exec(line.slice(at));
|
|
224
|
+
return m ? m[0].length : null;
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
const EDGE_OPERATORS = [
|
|
229
|
+
fixedOp('<-->', 'bidirectional'),
|
|
230
|
+
fixedOp('o--o', 'circleBoth'),
|
|
231
|
+
fixedOp('x--x', 'crossBoth'),
|
|
232
|
+
regexOp(/^-\.+->/, 'dotted'), // -.-> / -..-> / -...->
|
|
233
|
+
regexOp(/^-\.+-(?![>ox])/, 'dottedLine'), // -.- / -..- / -...-
|
|
234
|
+
regexOp(/^={2,}>/, 'thick'), // ==> / ===> / ====>
|
|
235
|
+
regexOp(/^-{2,}>/, 'arrow'), // --> / ---> / ---->
|
|
236
|
+
fixedOp('--o', 'circle'),
|
|
237
|
+
fixedOp('--x', 'cross'),
|
|
238
|
+
fixedOp('~~~', 'invisible'),
|
|
239
|
+
regexOp(/^={3,}(?!>)/, 'thickLine'), // === / ==== / =====
|
|
240
|
+
regexOp(/^-{3,}(?![>ox])/, 'line'), // --- / ---- / -----
|
|
241
|
+
];
|
|
242
|
+
/**
|
|
243
|
+
* Mid-chain label syntax (`A-- text -->B`, the Mermaid-recommended alternative
|
|
244
|
+
* to `A-->|text|B`): an opening marker shared by a whole operator family,
|
|
245
|
+
* arbitrary label text, then a closing marker that picks the specific
|
|
246
|
+
* {@link EdgeType} within that family. Only matched when no full token from
|
|
247
|
+
* {@link EDGE_OPERATORS} already matched at the same position (checked first
|
|
248
|
+
* in {@link parseEdgeChain}), so a plain `A-->B` is never reinterpreted here.
|
|
249
|
+
* Each family's closings are ordered longest-token-first so e.g. `.-` doesn't
|
|
250
|
+
* capture prematurely and strand the `>` of `.->`.
|
|
251
|
+
*/
|
|
252
|
+
const MID_LABEL_FAMILIES = [
|
|
253
|
+
{
|
|
254
|
+
open: '--',
|
|
255
|
+
closings: [
|
|
256
|
+
{ token: '-->', type: 'arrow' },
|
|
257
|
+
{ token: '--o', type: 'circle' },
|
|
258
|
+
{ token: '--x', type: 'cross' },
|
|
259
|
+
{ token: '---', type: 'line' },
|
|
260
|
+
],
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
open: '-.',
|
|
264
|
+
closings: [
|
|
265
|
+
{ token: '.->', type: 'dotted' },
|
|
266
|
+
{ token: '.-', type: 'dottedLine' },
|
|
267
|
+
],
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
open: '==',
|
|
271
|
+
closings: [
|
|
272
|
+
{ token: '==>', type: 'thick' },
|
|
273
|
+
{ token: '===', type: 'thickLine' },
|
|
274
|
+
],
|
|
275
|
+
},
|
|
276
|
+
];
|
|
277
|
+
/**
|
|
278
|
+
* Node ids that collide with Object.prototype members. Dagre stores nodes in a
|
|
279
|
+
* plain object, so an id like `__proto__`, `constructor`, or `length` would
|
|
280
|
+
* resolve to the prototype instead of the node, breaking layout (and enabling
|
|
281
|
+
* prototype pollution). These ids are rejected with a warning (non-fatal).
|
|
282
|
+
*/
|
|
283
|
+
const RESERVED_IDS = new Set([
|
|
284
|
+
'__proto__',
|
|
285
|
+
'constructor',
|
|
286
|
+
'prototype',
|
|
287
|
+
'length',
|
|
288
|
+
'name',
|
|
289
|
+
'toString',
|
|
290
|
+
'hasOwnProperty',
|
|
291
|
+
'valueOf',
|
|
292
|
+
'isPrototypeOf',
|
|
293
|
+
'propertyIsEnumerable',
|
|
294
|
+
'toLocaleString',
|
|
295
|
+
]);
|
|
296
|
+
/** True if an id is safe to use as a key in a plain object (Dagre-safe). */
|
|
297
|
+
function isSafeId(id) {
|
|
298
|
+
return !RESERVED_IDS.has(id);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Strip a pair of surrounding double quotes from a node/edge label, the
|
|
302
|
+
* syntax Mermaid itself recommends for Unicode/special characters
|
|
303
|
+
* (`id["Hello, World"]`). Without this the quotes were kept literally in
|
|
304
|
+
* the rendered text — found empirically while building
|
|
305
|
+
* `docs/markdown-mermaid-compliance-table.md` (2026-09-03), logged in `TODO.md`.
|
|
306
|
+
* Unquoted text (`id[Hello World]`) is returned unchanged.
|
|
307
|
+
*/
|
|
308
|
+
function stripQuotedLabel(text) {
|
|
309
|
+
if (text.length >= 2 && text.startsWith('"') && text.endsWith('"')) {
|
|
310
|
+
return text.slice(1, -1);
|
|
311
|
+
}
|
|
312
|
+
return text;
|
|
313
|
+
}
|
|
314
|
+
/** Mermaid's named HTML-derived entity codes (written without the leading
|
|
315
|
+
* `&`, e.g. `#quot;`), the ones Mermaid's own docs call out explicitly. */
|
|
316
|
+
const NAMED_ENTITIES = [
|
|
317
|
+
['quot', '"'],
|
|
318
|
+
['amp', '&'],
|
|
319
|
+
['lt', '<'],
|
|
320
|
+
['gt', '>'],
|
|
321
|
+
['nbsp', ' '],
|
|
322
|
+
['apos', "'"],
|
|
323
|
+
];
|
|
324
|
+
/**
|
|
325
|
+
* Parse the small set of inline-text conveniences Mermaid supports (found via
|
|
326
|
+
* `docs/markdown-mermaid-compliance-table.md` §5, logged in `TODO.md`) into
|
|
327
|
+
* both a flattened plain-text fallback (`text`, used for box-name attributes
|
|
328
|
+
* and `layout.ts`'s width/height estimation — neither needs to know about a
|
|
329
|
+
* bold/italic span) and a structured rich-text body (`runs`, `types.ts`'s
|
|
330
|
+
* `LabelToken[]` — what the translator actually renders):
|
|
331
|
+
* - Mermaid's entity codes (`#quot;`, `#9829;`, ...) are decoded to their
|
|
332
|
+
* character first — the numeric form via its Unicode code point, plus the
|
|
333
|
+
* handful of named entities Mermaid documents — regardless of the two
|
|
334
|
+
* cases below.
|
|
335
|
+
* - `<br/>` (and `<br>`, `<br />`, case-insensitive) becomes a real line
|
|
336
|
+
* break: a `{ break: true }` token in `runs` (`text` flattens it to a
|
|
337
|
+
* single space instead, same as before this function could produce real
|
|
338
|
+
* breaks — a fallback string has nowhere to put a second line).
|
|
339
|
+
* - A "Markdown string" (a label wrapped in backticks) has its delimiters
|
|
340
|
+
* stripped, and `**bold**`/`__bold__`/`*italic*`/`_italic_` spans become
|
|
341
|
+
* real `LabelRun.bold`/`.italic` tokens instead of literal asterisks or a
|
|
342
|
+
* stripped-to-plain-text approximation. Emphasis markers are only
|
|
343
|
+
* recognized inside a Markdown string — Mermaid's classic (non-backtick)
|
|
344
|
+
* label syntax never interprets them, so a literal `**` in an ordinary
|
|
345
|
+
* label stays literal, unchanged from before.
|
|
346
|
+
*/
|
|
347
|
+
function parseLabel(text) {
|
|
348
|
+
let decoded = text.replace(/#(\d+);/g, (_, code) => String.fromCodePoint(Number(code)));
|
|
349
|
+
for (const [name, char] of NAMED_ENTITIES) {
|
|
350
|
+
decoded = decoded.split(`#${name};`).join(char);
|
|
351
|
+
}
|
|
352
|
+
const isMarkdownString = decoded.length >= 2 && decoded.startsWith('`') && decoded.endsWith('`');
|
|
353
|
+
if (isMarkdownString)
|
|
354
|
+
decoded = decoded.slice(1, -1);
|
|
355
|
+
const segments = decoded.split(/<br\s*\/?>/gi);
|
|
356
|
+
const runs = [];
|
|
357
|
+
const plainSegments = [];
|
|
358
|
+
segments.forEach((segment, i) => {
|
|
359
|
+
if (i > 0)
|
|
360
|
+
runs.push({ break: true });
|
|
361
|
+
const segmentRuns = isMarkdownString
|
|
362
|
+
? parseEmphasis(segment)
|
|
363
|
+
: segment.length > 0
|
|
364
|
+
? [{ text: segment }]
|
|
365
|
+
: [];
|
|
366
|
+
runs.push(...segmentRuns);
|
|
367
|
+
plainSegments.push(segmentRuns.map((r) => r.text).join(''));
|
|
368
|
+
});
|
|
369
|
+
return { text: plainSegments.join(' '), runs };
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Split a Markdown-string segment (no `<br/>` in it — {@link parseLabel}
|
|
373
|
+
* already split those out) into plain/bold/italic {@link LabelRun}s.
|
|
374
|
+
* Non-nested and non-overlapping — CommonMark's full emphasis-resolution
|
|
375
|
+
* algorithm is out of scope here (V1 tolerance, same spirit as the rest of
|
|
376
|
+
* this parser); `**`/`__` (bold) is tried before `*`/`_` (italic) at each
|
|
377
|
+
* position so a bold span is never misread as two italic markers.
|
|
378
|
+
*/
|
|
379
|
+
function parseEmphasis(text) {
|
|
380
|
+
const runs = [];
|
|
381
|
+
const re = /(\*\*|__)([\s\S]+?)\1|(\*|_)([\s\S]+?)\3/g;
|
|
382
|
+
let last = 0;
|
|
383
|
+
let m;
|
|
384
|
+
while ((m = re.exec(text)) !== null) {
|
|
385
|
+
if (m.index > last)
|
|
386
|
+
runs.push({ text: text.slice(last, m.index) });
|
|
387
|
+
if (m[1] !== undefined)
|
|
388
|
+
runs.push({ text: m[2], bold: true });
|
|
389
|
+
else
|
|
390
|
+
runs.push({ text: m[4], italic: true });
|
|
391
|
+
last = re.lastIndex;
|
|
392
|
+
}
|
|
393
|
+
if (last < text.length)
|
|
394
|
+
runs.push({ text: text.slice(last) });
|
|
395
|
+
return runs;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Parse a CSS-like `prop:value,prop:value` property list (`classDef`/`style`/
|
|
399
|
+
* `linkStyle`, spec §6.3) into a lowercase-keyed map. Order-independent by
|
|
400
|
+
* design — unlike the old `classDef` regex this replaces, which required
|
|
401
|
+
* `fill:` to be the first (and only) property, silently failing the whole
|
|
402
|
+
* line otherwise. Values that themselves contain a comma (e.g. a
|
|
403
|
+
* `stroke-dasharray` list) will fragment here, but none of the properties
|
|
404
|
+
* this parser actually reads (`fill`, `stroke`, `stroke-width`) are affected.
|
|
405
|
+
*/
|
|
406
|
+
function parseCssStyleProps(text) {
|
|
407
|
+
const props = new Map();
|
|
408
|
+
for (const part of text.split(',')) {
|
|
409
|
+
const i = part.indexOf(':');
|
|
410
|
+
if (i === -1)
|
|
411
|
+
continue;
|
|
412
|
+
const key = part.slice(0, i).trim().toLowerCase();
|
|
413
|
+
const value = part.slice(i + 1).trim();
|
|
414
|
+
if (key.length > 0 && value.length > 0)
|
|
415
|
+
props.set(key, value);
|
|
416
|
+
}
|
|
417
|
+
return props;
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Normalize a CSS color value to a bare 6-digit uppercase hex string (what
|
|
421
|
+
* the translator's `hexColor()` expects), expanding the 3-digit shorthand
|
|
422
|
+
* (`#f9f` -> `FF99FF`). Returns `null` for anything else (a named color like
|
|
423
|
+
* `red`, `transparent`, a `rgb(...)` call, ...) — deliberately out of scope,
|
|
424
|
+
* same tolerant-degradation style as an unrecognized `@{shape: ...}` name:
|
|
425
|
+
* the property is just dropped rather than failing the whole statement.
|
|
426
|
+
*/
|
|
427
|
+
function parseHexColor(value) {
|
|
428
|
+
const v = value.trim();
|
|
429
|
+
const hex6 = v.match(/^#?([0-9A-Fa-f]{6})$/);
|
|
430
|
+
if (hex6)
|
|
431
|
+
return hex6[1].toUpperCase();
|
|
432
|
+
const hex3 = v.match(/^#?([0-9A-Fa-f]{3})$/);
|
|
433
|
+
if (hex3) {
|
|
434
|
+
const [r, g, b] = hex3[1];
|
|
435
|
+
return `${r}${r}${g}${g}${b}${b}`.toUpperCase();
|
|
436
|
+
}
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
/** Extract `fill`/`stroke` from a parsed property map, dropping anything not
|
|
440
|
+
* a recognized hex color (see {@link parseHexColor}). */
|
|
441
|
+
function extractNodeStyle(props) {
|
|
442
|
+
const patch = {};
|
|
443
|
+
const fillValue = props.get('fill');
|
|
444
|
+
if (fillValue) {
|
|
445
|
+
const hex = parseHexColor(fillValue);
|
|
446
|
+
if (hex)
|
|
447
|
+
patch.fill = hex;
|
|
448
|
+
}
|
|
449
|
+
const strokeValue = props.get('stroke');
|
|
450
|
+
if (strokeValue) {
|
|
451
|
+
const hex = parseHexColor(strokeValue);
|
|
452
|
+
if (hex)
|
|
453
|
+
patch.stroke = hex;
|
|
454
|
+
}
|
|
455
|
+
return patch;
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Apply a `fill`/`stroke` patch to a node, or — if the node hasn't been
|
|
459
|
+
* declared yet (a class/style referencing a node defined later in the
|
|
460
|
+
* source) — remember it in `pendingStyles` for the end-of-parse sweep. Merges
|
|
461
|
+
* onto whatever patch (if any) is already pending, so `style A stroke:#000`
|
|
462
|
+
* followed later by `A:::crit` (a fill-only class) doesn't clobber the
|
|
463
|
+
* stroke the first statement set.
|
|
464
|
+
*/
|
|
465
|
+
function applyNodeStyle(nodes, pendingStyles, nodeId, patch) {
|
|
466
|
+
if (patch.fill === undefined && patch.stroke === undefined)
|
|
467
|
+
return;
|
|
468
|
+
const existing = nodes.get(nodeId);
|
|
469
|
+
if (existing) {
|
|
470
|
+
if (patch.fill !== undefined)
|
|
471
|
+
existing.fill = patch.fill;
|
|
472
|
+
if (patch.stroke !== undefined)
|
|
473
|
+
existing.stroke = patch.stroke;
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
const pending = pendingStyles.get(nodeId) ?? {};
|
|
477
|
+
if (patch.fill !== undefined)
|
|
478
|
+
pending.fill = patch.fill;
|
|
479
|
+
if (patch.stroke !== undefined)
|
|
480
|
+
pending.stroke = patch.stroke;
|
|
481
|
+
pendingStyles.set(nodeId, pending);
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Merge physical lines that a bracketed node label wraps across, e.g.
|
|
485
|
+
* Lire --> Valider{Config
|
|
486
|
+
* valide?}:::gate
|
|
487
|
+
* Mermaid treats a raw newline inside `[]`/`()`/`{}` as part of the label
|
|
488
|
+
* text (rendered as a second line); this parser is otherwise line-based, so
|
|
489
|
+
* it would see two broken statements, warn on both, and silently drop the
|
|
490
|
+
* edge — disconnecting the diagram. Found via medium-realistic.mmd
|
|
491
|
+
* (2026-09-04). Lines that are already bracket-balanced (the common case)
|
|
492
|
+
* flush immediately and are unaffected.
|
|
493
|
+
*/
|
|
494
|
+
function joinBracketContinuations(lines) {
|
|
495
|
+
const result = [];
|
|
496
|
+
let buffer = [];
|
|
497
|
+
let depth = 0;
|
|
498
|
+
for (const line of lines) {
|
|
499
|
+
buffer.push(line.trim());
|
|
500
|
+
depth += bracketDelta(line);
|
|
501
|
+
if (depth <= 0) {
|
|
502
|
+
result.push(buffer.join(' '));
|
|
503
|
+
buffer = [];
|
|
504
|
+
depth = 0;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
if (buffer.length > 0) {
|
|
508
|
+
result.push(buffer.join(' '));
|
|
509
|
+
}
|
|
510
|
+
return result;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Net count of `[`/`(`/`{` minus `]`/`)`/`}` in a line, ignoring anything
|
|
514
|
+
* inside a double-quoted label (a literal bracket character in quoted text,
|
|
515
|
+
* e.g. `n["["]`, must not be mistaken for an unterminated statement).
|
|
516
|
+
*/
|
|
517
|
+
function bracketDelta(line) {
|
|
518
|
+
let delta = 0;
|
|
519
|
+
let inQuotes = false;
|
|
520
|
+
for (const ch of line) {
|
|
521
|
+
if (ch === '"') {
|
|
522
|
+
inQuotes = !inQuotes;
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
if (inQuotes)
|
|
526
|
+
continue;
|
|
527
|
+
if (ch === '[' || ch === '(' || ch === '{')
|
|
528
|
+
delta++;
|
|
529
|
+
else if (ch === ']' || ch === ')' || ch === '}')
|
|
530
|
+
delta--;
|
|
531
|
+
}
|
|
532
|
+
return delta;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Parse a Mermaid flowchart block into an intermediate AST.
|
|
536
|
+
* Throws {@link MermaidParseError} on structurally invalid input.
|
|
537
|
+
*/
|
|
538
|
+
export function parseMermaid(text) {
|
|
539
|
+
const warnings = [];
|
|
540
|
+
const nodes = new Map();
|
|
541
|
+
const edges = [];
|
|
542
|
+
const subgraphs = [];
|
|
543
|
+
const subgraphStack = [];
|
|
544
|
+
// Node ids whose subgraph membership (or lack of one) has already been
|
|
545
|
+
// decided, by the first line that mentions them — see attachToCurrentSubgraph.
|
|
546
|
+
const subgraphAttached = new Set();
|
|
547
|
+
// class name -> style patch, from `classDef` (spec §6.3).
|
|
548
|
+
const classDefs = new Map();
|
|
549
|
+
// node id -> style patch, for nodes styled (via class/`:::`/`style`) before they are defined.
|
|
550
|
+
const pendingStyles = new Map();
|
|
551
|
+
// Deferred `class`/`:::` assignments whose `classDef` hadn't been seen yet
|
|
552
|
+
// at the point the assignment was parsed — `classDef` conventionally comes
|
|
553
|
+
// first in a diagram but nothing requires it to; resolved once the whole
|
|
554
|
+
// document (and therefore every `classDef`) is known, mirroring how
|
|
555
|
+
// `pendingStyles` above already handles the opposite ordering (a class
|
|
556
|
+
// applied to a node declared later in the source).
|
|
557
|
+
const pendingClassAssignments = [];
|
|
558
|
+
// Deferred `linkStyle` statements (spec §6.3): applied after the full edge
|
|
559
|
+
// list is known, since an index can refer to an edge declared later in the
|
|
560
|
+
// source (the common case — `linkStyle` conventionally comes last).
|
|
561
|
+
const pendingLinkStyles = [];
|
|
562
|
+
let direction = 'TD';
|
|
563
|
+
const lines = joinBracketContinuations(text.split(/\r?\n/));
|
|
564
|
+
for (const rawLine of lines) {
|
|
565
|
+
const line = rawLine.trim();
|
|
566
|
+
if (line.length === 0)
|
|
567
|
+
continue;
|
|
568
|
+
// Header line: graph TD / flowchart LR/BT/RL / swimlane-beta LR. `TB` is
|
|
569
|
+
// Mermaid's own documented alias for `TD` (top-to-bottom either way) and
|
|
570
|
+
// maps straight through to it; the other three are distinct directions,
|
|
571
|
+
// all laid out by Dagre (`layout.ts`) and by SmartArt's `linDir` param
|
|
572
|
+
// (`chain.ts`/`tree.ts`). `swimlane-beta` is accepted as a third header
|
|
573
|
+
// keyword alongside `graph`/`flowchart` (not a separate diagram type):
|
|
574
|
+
// per Mermaid's own docs (mermaid.js.org/syntax/swimlanes.html),
|
|
575
|
+
// swimlane-beta "carries the full Flowchart feature set" — identical
|
|
576
|
+
// node/edge grammar, top-level `subgraph`s rendered as lanes instead of
|
|
577
|
+
// nested boxes. `detectDiagramType()` already classifies this header as
|
|
578
|
+
// `'flowchart'` for the same reason (see its doc comment).
|
|
579
|
+
const header = line.match(/^(?:graph|flowchart|swimlane-beta)\s+(TD|TB|LR|BT|RL)\b/i);
|
|
580
|
+
if (header) {
|
|
581
|
+
const requested = header[1].toUpperCase();
|
|
582
|
+
direction = requested === 'TB' ? 'TD' : requested;
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
// Allow a bare "graph"/"flowchart"/"swimlane-beta" with no direction.
|
|
586
|
+
if (/^(?:graph|flowchart|swimlane-beta)\s*$/i.test(line))
|
|
587
|
+
continue;
|
|
588
|
+
// Comments
|
|
589
|
+
if (line.startsWith('%%'))
|
|
590
|
+
continue;
|
|
591
|
+
// classDef Name1,Name2 fill:#XXX,stroke:#XXX,... (spec §6.3). Properties
|
|
592
|
+
// are order-independent and multiple class names may share one
|
|
593
|
+
// definition — neither was true of the regex this replaced, which
|
|
594
|
+
// required `fill:` first and exactly one class name (both fixed here as
|
|
595
|
+
// a side effect of reusing the same generic property-list parser as
|
|
596
|
+
// `style`/`linkStyle` below).
|
|
597
|
+
const classDef = line.match(/^classDef\s+([A-Za-z0-9_,\s-]+)\s+(.+)$/i);
|
|
598
|
+
if (classDef) {
|
|
599
|
+
const patch = extractNodeStyle(parseCssStyleProps(classDef[2]));
|
|
600
|
+
for (const name of classDef[1].split(',')) {
|
|
601
|
+
const trimmed = name.trim();
|
|
602
|
+
if (trimmed.length > 0)
|
|
603
|
+
classDefs.set(trimmed, patch);
|
|
604
|
+
}
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
// class A,B,C className — apply a defined class to nodes.
|
|
608
|
+
const classAssign = line.match(/^class\s+([A-Za-z0-9_,\s-]+)\s+([A-Za-z0-9_-]+)\s*$/i);
|
|
609
|
+
if (classAssign) {
|
|
610
|
+
const className = classAssign[2];
|
|
611
|
+
const ids = classAssign[1]
|
|
612
|
+
.split(',')
|
|
613
|
+
.map((s) => s.trim())
|
|
614
|
+
.filter((s) => s.length > 0);
|
|
615
|
+
const patch = classDefs.get(className);
|
|
616
|
+
if (patch) {
|
|
617
|
+
for (const id of ids)
|
|
618
|
+
applyNodeStyle(nodes, pendingStyles, id, patch);
|
|
619
|
+
}
|
|
620
|
+
else {
|
|
621
|
+
pendingClassAssignments.push({ nodeIds: ids, className });
|
|
622
|
+
}
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
// style A fill:#XXX,stroke:#XXX,... (spec §6.3) — direct styling, no
|
|
626
|
+
// classDef indirection. Same property parser as classDef/linkStyle.
|
|
627
|
+
const styleStmt = line.match(/^style\s+([A-Za-z0-9_-]+)\s+(.+)$/i);
|
|
628
|
+
if (styleStmt) {
|
|
629
|
+
applyNodeStyle(nodes, pendingStyles, styleStmt[1], extractNodeStyle(parseCssStyleProps(styleStmt[2])));
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
// linkStyle 0,2 stroke:#XXX,stroke-width:Npx / linkStyle default ...
|
|
633
|
+
// (spec §6.3). Indices refer to edge declaration order; resolved against
|
|
634
|
+
// the final edge list after the whole document is parsed (see
|
|
635
|
+
// `pendingLinkStyles` below), since `linkStyle` conventionally comes
|
|
636
|
+
// after the edges it targets.
|
|
637
|
+
const linkStyleStmt = line.match(/^linkStyle\s+(default|[\d\s,]+?)\s+(.+)$/i);
|
|
638
|
+
if (linkStyleStmt) {
|
|
639
|
+
const target = linkStyleStmt[1].trim();
|
|
640
|
+
const props = parseCssStyleProps(linkStyleStmt[2]);
|
|
641
|
+
const patch = {};
|
|
642
|
+
const strokeValue = props.get('stroke');
|
|
643
|
+
if (strokeValue) {
|
|
644
|
+
const hex = parseHexColor(strokeValue);
|
|
645
|
+
if (hex)
|
|
646
|
+
patch.stroke = hex;
|
|
647
|
+
}
|
|
648
|
+
const widthValue = props.get('stroke-width');
|
|
649
|
+
if (widthValue) {
|
|
650
|
+
const px = parseFloat(widthValue);
|
|
651
|
+
if (Number.isFinite(px) && px > 0)
|
|
652
|
+
patch.strokeWidth = px;
|
|
653
|
+
}
|
|
654
|
+
if (patch.stroke !== undefined || patch.strokeWidth !== undefined) {
|
|
655
|
+
const indices = target.toLowerCase() === 'default'
|
|
656
|
+
? 'default'
|
|
657
|
+
: target
|
|
658
|
+
.split(',')
|
|
659
|
+
.map((s) => parseInt(s.trim(), 10))
|
|
660
|
+
.filter((n) => Number.isInteger(n) && n >= 0);
|
|
661
|
+
pendingLinkStyles.push({ indices, patch });
|
|
662
|
+
}
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
// subgraph ... end
|
|
666
|
+
if (/^subgraph\b/i.test(line)) {
|
|
667
|
+
const sg = parseSubgraphHeader(line);
|
|
668
|
+
subgraphs.push(sg);
|
|
669
|
+
subgraphStack.push(sg);
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
if (/^end\s*$/i.test(line)) {
|
|
673
|
+
if (subgraphStack.length > 0) {
|
|
674
|
+
const closed = subgraphStack.pop();
|
|
675
|
+
const parent = subgraphStack[subgraphStack.length - 1];
|
|
676
|
+
if (parent && !parent.subgraphIds.includes(closed.id)) {
|
|
677
|
+
parent.subgraphIds.push(closed.id);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
else {
|
|
681
|
+
warnings.push('Unexpected `end` without matching `subgraph`.');
|
|
682
|
+
}
|
|
683
|
+
continue;
|
|
684
|
+
}
|
|
685
|
+
// A subgraph's own nested `direction` statement (spec §6.1 follow-up):
|
|
686
|
+
// parsed into Subgraph.direction (types.ts) rather than falling through
|
|
687
|
+
// to the generic "Unsupported line ignored" warning below, but not yet
|
|
688
|
+
// applied to layout — see Subgraph.direction's doc comment for why
|
|
689
|
+
// (Dagre has no per-cluster rankdir). Only recognized inside an open
|
|
690
|
+
// subgraph block; elsewhere it's not valid Mermaid syntax at all, so it
|
|
691
|
+
// still falls through to the generic warning.
|
|
692
|
+
if (subgraphStack.length > 0) {
|
|
693
|
+
const subgraphDirection = line.match(/^direction\s+(TD|TB|LR|BT|RL)\b/i);
|
|
694
|
+
if (subgraphDirection) {
|
|
695
|
+
const requested = subgraphDirection[1].toUpperCase();
|
|
696
|
+
const current = subgraphStack[subgraphStack.length - 1];
|
|
697
|
+
current.direction = requested === 'TB' ? 'TD' : requested;
|
|
698
|
+
warnings.push(`Subgraph "${current.id}" direction (${current.direction}) is not yet applied to its layout (V1 limitation) -- it renders using the flowchart's own direction instead.`);
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
// Edge statement(s): A --> B, A -->|label| B, A --- B, chained
|
|
703
|
+
// A --> B --> C, and fan-out/fan-in via `&` (A --> B & C, A & B --> C).
|
|
704
|
+
const chainEdges = parseEdgeChain(line);
|
|
705
|
+
if (chainEdges) {
|
|
706
|
+
for (const edge of chainEdges) {
|
|
707
|
+
edges.push({
|
|
708
|
+
from: edge.from,
|
|
709
|
+
to: edge.to,
|
|
710
|
+
type: edge.type,
|
|
711
|
+
label: edge.label,
|
|
712
|
+
labelRuns: edge.labelRuns,
|
|
713
|
+
});
|
|
714
|
+
if (!registerNode(nodes, edge.from, edge.fromLabel, edge.fromLabelRuns, edge.fromShape)) {
|
|
715
|
+
warnings.push(`Node id "${edge.from}" is reserved and was ignored.`);
|
|
716
|
+
}
|
|
717
|
+
if (!registerNode(nodes, edge.to, edge.toLabel, edge.toLabelRuns, edge.toShape)) {
|
|
718
|
+
warnings.push(`Node id "${edge.to}" is reserved and was ignored.`);
|
|
719
|
+
}
|
|
720
|
+
// Apply inline class styles (`A:::crit`) to the edge endpoints.
|
|
721
|
+
applyClassToNode(nodes, pendingStyles, classDefs, pendingClassAssignments, edge.from, edge.fromClass);
|
|
722
|
+
applyClassToNode(nodes, pendingStyles, classDefs, pendingClassAssignments, edge.to, edge.toClass);
|
|
723
|
+
attachToCurrentSubgraph(subgraphStack, edge.from, subgraphAttached);
|
|
724
|
+
attachToCurrentSubgraph(subgraphStack, edge.to, subgraphAttached);
|
|
725
|
+
}
|
|
726
|
+
continue;
|
|
727
|
+
}
|
|
728
|
+
// Bare node definition: id[Text] or id
|
|
729
|
+
const node = parseNodeStatement(line);
|
|
730
|
+
if (node) {
|
|
731
|
+
if (!registerNode(nodes, node.id, node.label, node.labelRuns, node.shape)) {
|
|
732
|
+
warnings.push(`Node id "${node.id}" is reserved and was ignored.`);
|
|
733
|
+
}
|
|
734
|
+
applyClassToNode(nodes, pendingStyles, classDefs, pendingClassAssignments, node.id, node.className);
|
|
735
|
+
attachToCurrentSubgraph(subgraphStack, node.id, subgraphAttached);
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
// Unsupported construct — warn but keep going (V1 tolerance).
|
|
739
|
+
warnings.push(`Unsupported line ignored: ${line}`);
|
|
740
|
+
}
|
|
741
|
+
// Resolve class/`:::` assignments whose `classDef` appeared later in the
|
|
742
|
+
// source (or never at all) — a class name still undefined at this point,
|
|
743
|
+
// once the whole document has been scanned, is a genuine typo/omission and
|
|
744
|
+
// still warns, just later than before.
|
|
745
|
+
for (const { nodeIds, className } of pendingClassAssignments) {
|
|
746
|
+
const patch = classDefs.get(className);
|
|
747
|
+
if (!patch) {
|
|
748
|
+
warnings.push(`classDef "${className}" referenced but not defined.`);
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
for (const id of nodeIds) {
|
|
752
|
+
applyNodeStyle(nodes, pendingStyles, id, patch);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
// Apply any pending class/style patches (including ones just resolved
|
|
756
|
+
// above) to nodes that were defined after the class/style statement that
|
|
757
|
+
// targets them.
|
|
758
|
+
for (const [id, patch] of pendingStyles) {
|
|
759
|
+
const existing = nodes.get(id);
|
|
760
|
+
if (existing) {
|
|
761
|
+
if (patch.fill !== undefined)
|
|
762
|
+
existing.fill = patch.fill;
|
|
763
|
+
if (patch.stroke !== undefined)
|
|
764
|
+
existing.stroke = patch.stroke;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
// Apply deferred `linkStyle` statements now that the edge list is final —
|
|
768
|
+
// `default` covers every edge, otherwise each index picks one edge in
|
|
769
|
+
// declaration order; an out-of-range index is silently skipped (V1
|
|
770
|
+
// tolerance, consistent with the rest of the parser).
|
|
771
|
+
for (const { indices, patch } of pendingLinkStyles) {
|
|
772
|
+
const targets = indices === 'default' ? edges.map((_, i) => i) : indices;
|
|
773
|
+
for (const i of targets) {
|
|
774
|
+
const edge = edges[i];
|
|
775
|
+
if (!edge)
|
|
776
|
+
continue;
|
|
777
|
+
if (patch.stroke !== undefined)
|
|
778
|
+
edge.stroke = patch.stroke;
|
|
779
|
+
if (patch.strokeWidth !== undefined)
|
|
780
|
+
edge.strokeWidth = patch.strokeWidth;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
// A subgraph id must not also be a node: Mermaid allows edges between
|
|
784
|
+
// subgraphs (e.g. `U1 --> Y2`), and our parser would otherwise register the
|
|
785
|
+
// subgraph id as a plain node, which breaks Dagre (an id cannot be both a
|
|
786
|
+
// node and a cluster). Drop such nodes and warn. Edges referencing subgraphs
|
|
787
|
+
// as endpoints are dropped too (V1 does not support inter-subgraph edges).
|
|
788
|
+
const subgraphIdSet = new Set(subgraphs.map((s) => s.id));
|
|
789
|
+
for (const id of subgraphIdSet) {
|
|
790
|
+
if (nodes.has(id)) {
|
|
791
|
+
nodes.delete(id);
|
|
792
|
+
warnings.push(`Subgraph "${id}" was also used as a node; node ignored.`);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
const edgesWithoutSubgraphs = edges.filter((e) => {
|
|
796
|
+
if (subgraphIdSet.has(e.from) || subgraphIdSet.has(e.to)) {
|
|
797
|
+
warnings.push(`Edge ${e.from} -> ${e.to} references a subgraph; ignored (V1).`);
|
|
798
|
+
return false;
|
|
799
|
+
}
|
|
800
|
+
return true;
|
|
801
|
+
});
|
|
802
|
+
if (subgraphStack.length > 0) {
|
|
803
|
+
warnings.push('Unclosed `subgraph` block.');
|
|
804
|
+
}
|
|
805
|
+
return {
|
|
806
|
+
ast: {
|
|
807
|
+
direction,
|
|
808
|
+
nodes: [...nodes.values()],
|
|
809
|
+
edges: edgesWithoutSubgraphs,
|
|
810
|
+
subgraphs,
|
|
811
|
+
},
|
|
812
|
+
warnings,
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
function parseSubgraphHeader(line) {
|
|
816
|
+
const rest = line.replace(/^subgraph\b/i, '').trim();
|
|
817
|
+
// subgraph id["Title"] or subgraph id[Title] or subgraph "Title" or subgraph id
|
|
818
|
+
const withTitle = rest.match(/^([A-Za-z0-9_-]+)\s*\[\s*"([^"]*)"\s*\]/);
|
|
819
|
+
if (withTitle) {
|
|
820
|
+
return { id: withTitle[1], title: withTitle[2], nodeIds: [], subgraphIds: [] };
|
|
821
|
+
}
|
|
822
|
+
const withTitleNoQuotes = rest.match(/^([A-Za-z0-9_-]+)\s*\[\s*([^\]]*)\s*\]/);
|
|
823
|
+
if (withTitleNoQuotes) {
|
|
824
|
+
return { id: withTitleNoQuotes[1], title: withTitleNoQuotes[2].trim(), nodeIds: [], subgraphIds: [] };
|
|
825
|
+
}
|
|
826
|
+
const quotedTitle = rest.match(/^"([^"]+)"\s*$/);
|
|
827
|
+
if (quotedTitle) {
|
|
828
|
+
return { id: quotedTitle[1], title: quotedTitle[1], nodeIds: [], subgraphIds: [] };
|
|
829
|
+
}
|
|
830
|
+
// Bare id (or id with spaces used as title).
|
|
831
|
+
const bare = rest.match(/^([A-Za-z0-9_-]+)\s*$/);
|
|
832
|
+
if (bare) {
|
|
833
|
+
return { id: bare[1], title: bare[1], nodeIds: [], subgraphIds: [] };
|
|
834
|
+
}
|
|
835
|
+
// Fallback: treat whole rest as id.
|
|
836
|
+
return { id: rest, title: rest, nodeIds: [], subgraphIds: [] };
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* Attach a node to the innermost currently-open subgraph, but only the FIRST
|
|
840
|
+
* time that node id is seen — matching Mermaid's own semantics, where a
|
|
841
|
+
* node's subgraph membership is decided by where it's first declared, not by
|
|
842
|
+
* every later line that happens to mention it. Without this, a node declared
|
|
843
|
+
* inside a nested subgraph gets re-attached to an ENCLOSING subgraph the next
|
|
844
|
+
* time an edge outside the nested block references it (e.g. `B --> C` after
|
|
845
|
+
* `C`'s subgraph already closed), leaking it into the wrong cluster.
|
|
846
|
+
*/
|
|
847
|
+
function attachToCurrentSubgraph(stack, nodeId, attached) {
|
|
848
|
+
if (attached.has(nodeId))
|
|
849
|
+
return;
|
|
850
|
+
attached.add(nodeId);
|
|
851
|
+
const top = stack[stack.length - 1];
|
|
852
|
+
if (top)
|
|
853
|
+
top.nodeIds.push(nodeId);
|
|
854
|
+
}
|
|
855
|
+
function registerNode(nodes, id, label, labelRuns, shape = 'rect') {
|
|
856
|
+
// Reject ids that collide with Object.prototype (Dagre breaks on them).
|
|
857
|
+
if (!isSafeId(id))
|
|
858
|
+
return false;
|
|
859
|
+
const existing = nodes.get(id);
|
|
860
|
+
if (existing) {
|
|
861
|
+
// Keep first-seen shape/label; later bare references don't override.
|
|
862
|
+
return true;
|
|
863
|
+
}
|
|
864
|
+
nodes.set(id, { id, label, labelRuns, shape });
|
|
865
|
+
return true;
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* Apply a class's style patch to a node (from `:::class` inline or `class`).
|
|
869
|
+
* If the node is not yet defined, the patch is remembered in `pendingStyles`
|
|
870
|
+
* (via {@link applyNodeStyle}). If the class itself isn't defined *yet* (its
|
|
871
|
+
* `classDef` may still come later in the source), the assignment is queued in
|
|
872
|
+
* `pendingClassAssignments` for the end-of-parse sweep instead of warning
|
|
873
|
+
* immediately — see that array's doc comment in {@link parseMermaid}.
|
|
874
|
+
*/
|
|
875
|
+
function applyClassToNode(nodes, pendingStyles, classDefs, pendingClassAssignments, nodeId, className) {
|
|
876
|
+
if (!className)
|
|
877
|
+
return;
|
|
878
|
+
const patch = classDefs.get(className);
|
|
879
|
+
if (!patch) {
|
|
880
|
+
pendingClassAssignments.push({ nodeIds: [nodeId], className });
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
applyNodeStyle(nodes, pendingStyles, nodeId, patch);
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Parse a node statement like `A[Text]`, `B{Decision}`, or bare `C` —
|
|
887
|
+
* including an optional inline class shorthand (`A[Text]:::crit` or bare
|
|
888
|
+
* `A:::crit`), which {@link parseNodeRef} already handled at edge endpoints
|
|
889
|
+
* but this standalone-declaration path did not (found via
|
|
890
|
+
* `docs/markdown-mermaid-compliance-table.md` §5, logged in `TODO.md`).
|
|
891
|
+
*/
|
|
892
|
+
function parseNodeStatement(line) {
|
|
893
|
+
const idMatch = line.match(/^([A-Za-z0-9_-]+)\s*(.*)$/);
|
|
894
|
+
if (!idMatch)
|
|
895
|
+
return null;
|
|
896
|
+
const id = idMatch[1];
|
|
897
|
+
let rest = idMatch[2].trim();
|
|
898
|
+
// Optional inline class: `A[Text]:::crit` or `A:::crit`.
|
|
899
|
+
let className = null;
|
|
900
|
+
const classMatch = rest.match(/^(.*?):::\s*([A-Za-z0-9_-]+)\s*$/);
|
|
901
|
+
if (classMatch) {
|
|
902
|
+
className = classMatch[2];
|
|
903
|
+
rest = classMatch[1].trim();
|
|
904
|
+
}
|
|
905
|
+
if (rest.length === 0) {
|
|
906
|
+
return { id, label: id, labelRuns: [{ text: id }], shape: 'rect', className };
|
|
907
|
+
}
|
|
908
|
+
const atShape = parseAtShapeSyntax(rest);
|
|
909
|
+
if (atShape) {
|
|
910
|
+
return {
|
|
911
|
+
id,
|
|
912
|
+
label: atShape.label ?? id,
|
|
913
|
+
labelRuns: atShape.labelRuns ?? [{ text: id }],
|
|
914
|
+
shape: atShape.shape,
|
|
915
|
+
className,
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
for (const { open, close, shape } of SHAPE_BY_SYNTAX) {
|
|
919
|
+
// Longest patterns first to avoid `(` matching `((`.
|
|
920
|
+
if (rest.startsWith(open) && rest.endsWith(close)) {
|
|
921
|
+
const inner = rest.slice(open.length, rest.length - close.length).trim();
|
|
922
|
+
const parsed = parseLabel(stripQuotedLabel(inner));
|
|
923
|
+
return { id, label: parsed.text, labelRuns: parsed.runs, shape, className };
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
return null;
|
|
927
|
+
}
|
|
928
|
+
/** Parse a node reference at an edge endpoint: bare id or id with shape. */
|
|
929
|
+
function parseNodeRef(ref) {
|
|
930
|
+
const trimmed = ref.trim();
|
|
931
|
+
const idMatch = trimmed.match(/^([A-Za-z0-9_-]+)\s*(.*)$/);
|
|
932
|
+
if (!idMatch)
|
|
933
|
+
return null;
|
|
934
|
+
const id = idMatch[1];
|
|
935
|
+
let rest = idMatch[2].trim();
|
|
936
|
+
// Optional inline class: `A[Text]:::crit` or `A:::crit`.
|
|
937
|
+
let className = null;
|
|
938
|
+
const classMatch = rest.match(/^(.*?):::\s*([A-Za-z0-9_-]+)\s*$/);
|
|
939
|
+
if (classMatch) {
|
|
940
|
+
className = classMatch[2];
|
|
941
|
+
rest = classMatch[1].trim();
|
|
942
|
+
}
|
|
943
|
+
if (rest.length === 0)
|
|
944
|
+
return { id, label: id, labelRuns: [{ text: id }], shape: 'rect', className };
|
|
945
|
+
const atShape = parseAtShapeSyntax(rest);
|
|
946
|
+
if (atShape) {
|
|
947
|
+
return {
|
|
948
|
+
id,
|
|
949
|
+
label: atShape.label ?? id,
|
|
950
|
+
labelRuns: atShape.labelRuns ?? [{ text: id }],
|
|
951
|
+
shape: atShape.shape,
|
|
952
|
+
className,
|
|
953
|
+
};
|
|
954
|
+
}
|
|
955
|
+
for (const { open, close, shape } of SHAPE_BY_SYNTAX) {
|
|
956
|
+
if (rest.startsWith(open) && rest.endsWith(close)) {
|
|
957
|
+
const inner = rest.slice(open.length, rest.length - close.length).trim();
|
|
958
|
+
const parsed = parseLabel(stripQuotedLabel(inner));
|
|
959
|
+
return { id, label: parsed.text, labelRuns: parsed.runs, shape, className };
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
return null;
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Try to match a mid-chain-label edge (`A-- text -->B`) starting at `start`,
|
|
966
|
+
* where `line[start]` begins one of {@link MID_LABEL_FAMILIES}'s open
|
|
967
|
+
* markers. Scans forward for that family's closing marker, itself only at
|
|
968
|
+
* bracket depth 0 so a label can't swallow an unrelated later operator
|
|
969
|
+
* through a stray bracket. Returns `null` if no closing marker follows
|
|
970
|
+
* (e.g. a bare trailing `--` that isn't actually an edge).
|
|
971
|
+
*/
|
|
972
|
+
function matchMidLabelEdge(line, start) {
|
|
973
|
+
const family = MID_LABEL_FAMILIES.find((f) => line.startsWith(f.open, start));
|
|
974
|
+
if (!family)
|
|
975
|
+
return null;
|
|
976
|
+
let depth = 0;
|
|
977
|
+
let j = start + family.open.length;
|
|
978
|
+
while (j < line.length) {
|
|
979
|
+
if (depth === 0) {
|
|
980
|
+
const closing = family.closings.find((c) => line.startsWith(c.token, j));
|
|
981
|
+
if (closing) {
|
|
982
|
+
const parsed = parseLabel(stripQuotedLabel(line.slice(start + family.open.length, j).trim()));
|
|
983
|
+
return { type: closing.type, end: j + closing.token.length, label: parsed.text, labelRuns: parsed.runs };
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
const ch = line[j];
|
|
987
|
+
if (ch === '[' || ch === '(' || ch === '{')
|
|
988
|
+
depth++;
|
|
989
|
+
else if (ch === ']' || ch === ')' || ch === '}')
|
|
990
|
+
depth = Math.max(0, depth - 1);
|
|
991
|
+
j++;
|
|
992
|
+
}
|
|
993
|
+
return null;
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Parse a line as one or more edge statements: a plain `A --> B`, a labeled
|
|
997
|
+
* `A -->|Yes| B` or `A-- Yes -->B`, a same-line chain (`A --> B --> C`),
|
|
998
|
+
* and/or fan-out/fan-in via `&` (`A --> B & C`, `A & B --> C`). Returns
|
|
999
|
+
* `null` if the line has no recognizable edge operator at all (so the caller
|
|
1000
|
+
* can fall back to treating it as a bare node declaration).
|
|
1001
|
+
*
|
|
1002
|
+
* Operators are only matched at bracket depth 0, so a node label containing
|
|
1003
|
+
* a literal `---`-like sequence (`A[Step 1 --- Step 2]`) is never mistaken
|
|
1004
|
+
* for an edge operator — the scanner tracks `[`/`(`/`{` nesting the same way
|
|
1005
|
+
* {@link joinBracketContinuations} does.
|
|
1006
|
+
*/
|
|
1007
|
+
function parseEdgeChain(line) {
|
|
1008
|
+
const matches = [];
|
|
1009
|
+
let depth = 0;
|
|
1010
|
+
let i = 0;
|
|
1011
|
+
while (i < line.length) {
|
|
1012
|
+
if (depth === 0) {
|
|
1013
|
+
let op = null;
|
|
1014
|
+
for (const matcher of EDGE_OPERATORS) {
|
|
1015
|
+
const length = matcher.match(line, i);
|
|
1016
|
+
if (length !== null) {
|
|
1017
|
+
op = { type: matcher.type, length };
|
|
1018
|
+
break;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
if (op) {
|
|
1022
|
+
let end = i + op.length;
|
|
1023
|
+
let label = null;
|
|
1024
|
+
let labelRuns = null;
|
|
1025
|
+
const labelMatch = line.slice(end).match(/^\|([^|]*)\|/);
|
|
1026
|
+
if (labelMatch) {
|
|
1027
|
+
const parsed = parseLabel(stripQuotedLabel(labelMatch[1].trim()));
|
|
1028
|
+
label = parsed.text;
|
|
1029
|
+
labelRuns = parsed.runs;
|
|
1030
|
+
end += labelMatch[0].length;
|
|
1031
|
+
}
|
|
1032
|
+
matches.push({ type: op.type, start: i, end, label, labelRuns });
|
|
1033
|
+
i = end;
|
|
1034
|
+
continue;
|
|
1035
|
+
}
|
|
1036
|
+
const mid = matchMidLabelEdge(line, i);
|
|
1037
|
+
if (mid) {
|
|
1038
|
+
matches.push({ type: mid.type, start: i, end: mid.end, label: mid.label, labelRuns: mid.labelRuns });
|
|
1039
|
+
i = mid.end;
|
|
1040
|
+
continue;
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
const ch = line[i];
|
|
1044
|
+
if (ch === '[' || ch === '(' || ch === '{')
|
|
1045
|
+
depth++;
|
|
1046
|
+
else if (ch === ']' || ch === ')' || ch === '}')
|
|
1047
|
+
depth = Math.max(0, depth - 1);
|
|
1048
|
+
i++;
|
|
1049
|
+
}
|
|
1050
|
+
if (matches.length === 0)
|
|
1051
|
+
return null;
|
|
1052
|
+
const segments = [];
|
|
1053
|
+
let cursor = 0;
|
|
1054
|
+
for (const m of matches) {
|
|
1055
|
+
segments.push(line.slice(cursor, m.start));
|
|
1056
|
+
cursor = m.end;
|
|
1057
|
+
}
|
|
1058
|
+
segments.push(line.slice(cursor));
|
|
1059
|
+
const nodeLists = segments.map((seg) => parseNodeList(seg));
|
|
1060
|
+
if (nodeLists.some((list) => list === null))
|
|
1061
|
+
return null;
|
|
1062
|
+
const edges = [];
|
|
1063
|
+
for (let k = 0; k < matches.length; k++) {
|
|
1064
|
+
const leftList = nodeLists[k];
|
|
1065
|
+
const rightList = nodeLists[k + 1];
|
|
1066
|
+
const { type, label, labelRuns } = matches[k];
|
|
1067
|
+
for (const left of leftList) {
|
|
1068
|
+
for (const right of rightList) {
|
|
1069
|
+
edges.push({
|
|
1070
|
+
from: left.id,
|
|
1071
|
+
to: right.id,
|
|
1072
|
+
fromLabel: left.label,
|
|
1073
|
+
fromLabelRuns: left.labelRuns,
|
|
1074
|
+
toLabel: right.label,
|
|
1075
|
+
toLabelRuns: right.labelRuns,
|
|
1076
|
+
fromShape: left.shape,
|
|
1077
|
+
toShape: right.shape,
|
|
1078
|
+
fromClass: left.className,
|
|
1079
|
+
toClass: right.className,
|
|
1080
|
+
type,
|
|
1081
|
+
label,
|
|
1082
|
+
labelRuns,
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
return edges;
|
|
1088
|
+
}
|
|
1089
|
+
/** Split a node-list segment (`A & B & C`) into individual node references. */
|
|
1090
|
+
function parseNodeList(segment) {
|
|
1091
|
+
const parts = splitTopLevelAmpersand(segment);
|
|
1092
|
+
if (parts.length === 0)
|
|
1093
|
+
return null;
|
|
1094
|
+
const refs = [];
|
|
1095
|
+
for (const part of parts) {
|
|
1096
|
+
const ref = parseNodeRef(part);
|
|
1097
|
+
if (!ref)
|
|
1098
|
+
return null;
|
|
1099
|
+
refs.push(ref);
|
|
1100
|
+
}
|
|
1101
|
+
return refs;
|
|
1102
|
+
}
|
|
1103
|
+
/**
|
|
1104
|
+
* Split on `&` outside bracket nesting, so `&` inside a node's label text
|
|
1105
|
+
* (`A["Tom & Jerry"]`) isn't mistaken for the fan-out/fan-in list separator.
|
|
1106
|
+
*/
|
|
1107
|
+
function splitTopLevelAmpersand(segment) {
|
|
1108
|
+
const parts = [];
|
|
1109
|
+
let depth = 0;
|
|
1110
|
+
let current = '';
|
|
1111
|
+
for (const ch of segment) {
|
|
1112
|
+
if (ch === '[' || ch === '(' || ch === '{')
|
|
1113
|
+
depth++;
|
|
1114
|
+
else if (ch === ']' || ch === ')' || ch === '}')
|
|
1115
|
+
depth = Math.max(0, depth - 1);
|
|
1116
|
+
if (ch === '&' && depth === 0) {
|
|
1117
|
+
parts.push(current);
|
|
1118
|
+
current = '';
|
|
1119
|
+
}
|
|
1120
|
+
else {
|
|
1121
|
+
current += ch;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
parts.push(current);
|
|
1125
|
+
return parts.map((p) => p.trim()).filter((p) => p.length > 0);
|
|
1126
|
+
}
|
|
1127
|
+
/** Error thrown for structurally invalid Mermaid input. */
|
|
1128
|
+
export class MermaidParseError extends Error {
|
|
1129
|
+
constructor(message) {
|
|
1130
|
+
super(message);
|
|
1131
|
+
this.name = 'MermaidParseError';
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
//# sourceMappingURL=parser.js.map
|