@zyro-sec/merman 0.0.0-stage → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/package.json +29 -4
  2. package/script/layout-audit.ts +98 -0
  3. package/src/core/canvas.test.ts +203 -0
  4. package/src/core/canvas.ts +228 -0
  5. package/src/core/color/style.test.ts +24 -0
  6. package/src/core/color/style.ts +56 -0
  7. package/src/core/drawing.test.ts +116 -0
  8. package/src/core/drawing.ts +301 -0
  9. package/src/core/geometry.test.ts +112 -0
  10. package/src/core/geometry.ts +347 -0
  11. package/src/core/mermaid.ts +38 -0
  12. package/src/core/render-grid.ts +51 -0
  13. package/src/core/spatial.test.ts +87 -0
  14. package/src/core/spatial.ts +243 -0
  15. package/src/core/text-lines.ts +32 -0
  16. package/src/core/text.test.ts +33 -0
  17. package/src/core/text.ts +44 -0
  18. package/src/detect.ts +17 -0
  19. package/src/diagnostics.ts +16 -0
  20. package/src/flowchart/drawing.ts +340 -0
  21. package/src/flowchart/flowchart.test.ts +2800 -0
  22. package/src/flowchart/labels.test.ts +127 -0
  23. package/src/flowchart/labels.ts +144 -0
  24. package/src/flowchart/layout.ts +1427 -0
  25. package/src/flowchart/options.ts +12 -0
  26. package/src/flowchart/parser.ts +467 -0
  27. package/src/flowchart/render.ts +10 -0
  28. package/src/flowchart/routing.test.ts +368 -0
  29. package/src/flowchart/routing.ts +1542 -0
  30. package/src/flowchart/style.ts +79 -0
  31. package/src/flowchart/types.ts +65 -0
  32. package/src/gantt/diagram.test.ts +124 -0
  33. package/src/gantt/diagram.ts +8 -0
  34. package/src/gantt/drawing.ts +191 -0
  35. package/src/gantt/parser.ts +161 -0
  36. package/src/gantt/render-grid.ts +17 -0
  37. package/src/gantt/style.ts +36 -0
  38. package/src/gantt/types.ts +44 -0
  39. package/src/gitgraph/diagram.test.ts +183 -0
  40. package/src/gitgraph/diagram.ts +8 -0
  41. package/src/gitgraph/drawing.ts +234 -0
  42. package/src/gitgraph/parser.ts +219 -0
  43. package/src/gitgraph/render-grid.ts +17 -0
  44. package/src/gitgraph/style.ts +37 -0
  45. package/src/gitgraph/types.ts +36 -0
  46. package/src/layout-audit.test.ts +92 -0
  47. package/src/markdown.ts +336 -0
  48. package/src/palette.test.ts +103 -0
  49. package/src/palette.ts +75 -0
  50. package/src/plugin.ts +15 -0
  51. package/src/sequence/diagram.test.ts +757 -0
  52. package/src/sequence/diagram.ts +8 -0
  53. package/src/sequence/drawing.ts +287 -0
  54. package/src/sequence/options.ts +8 -0
  55. package/src/sequence/parser.ts +282 -0
  56. package/src/sequence/placement.test.ts +219 -0
  57. package/src/sequence/placement.ts +684 -0
  58. package/src/sequence/render-grid.ts +22 -0
  59. package/src/sequence/style.ts +81 -0
  60. package/src/sequence/types.ts +74 -0
  61. package/src/state/diagram.test.ts +1386 -0
  62. package/src/state/diagram.ts +8 -0
  63. package/src/state/drawing.ts +360 -0
  64. package/src/state/endpoint.ts +15 -0
  65. package/src/state/layout.test.ts +314 -0
  66. package/src/state/layout.ts +1134 -0
  67. package/src/state/note.ts +59 -0
  68. package/src/state/options.ts +11 -0
  69. package/src/state/parser.ts +224 -0
  70. package/src/state/render-grid.ts +22 -0
  71. package/src/state/routing.test.ts +693 -0
  72. package/src/state/routing.ts +1522 -0
  73. package/src/state/search.ts +103 -0
  74. package/src/state/style.test.ts +113 -0
  75. package/src/state/style.ts +52 -0
  76. package/src/state/types.ts +63 -0
  77. package/src/state/visible-model.test.ts +84 -0
  78. package/src/state/visible-model.ts +77 -0
  79. package/src/test/diagnostics.test.ts +129 -0
  80. package/src/test/diagram.ts +32 -0
  81. package/src/test/layout-audit/fixtures.ts +538 -0
  82. package/src/test/layout-audit/harness.ts +522 -0
  83. package/src/test/markdown.test.ts +474 -0
  84. package/src/timeline/diagram.test.ts +188 -0
  85. package/src/timeline/diagram.ts +8 -0
  86. package/src/timeline/drawing.ts +109 -0
  87. package/src/timeline/parser.ts +119 -0
  88. package/src/timeline/render-grid.ts +17 -0
  89. package/src/timeline/style.ts +36 -0
  90. package/src/timeline/types.ts +31 -0
  91. package/tsconfig.json +10 -0
  92. package/README.md +0 -3
@@ -0,0 +1,12 @@
1
+ import type { BorderStyle } from "@opentui/core"
2
+ import type { FlowchartDirection } from "./types.js"
3
+
4
+ export interface FlowchartDiagramRenderOptions {
5
+ compact?: boolean
6
+ direction?: FlowchartDirection
7
+ borderStyle?: BorderStyle
8
+ minNodeGap?: number
9
+ minRankGap?: number
10
+ /** Target rendered width. Oversized horizontal layouts fold vertically and broad vertical ranks wrap. */
11
+ layoutMaxWidth?: number
12
+ }
@@ -0,0 +1,467 @@
1
+ import type {
2
+ FlowchartDiagram,
3
+ FlowchartDirection,
4
+ FlowchartEdge,
5
+ FlowchartEdgeStyle,
6
+ FlowchartNode,
7
+ FlowchartSubgraph,
8
+ } from "./types.js"
9
+ import { MermaidSyntaxError } from "../diagnostics.js"
10
+ import {
11
+ firstMeaningfulMermaidLine,
12
+ meaningfulNumberedMermaidLines,
13
+ stripMermaidQuotes as stripQuotes,
14
+ } from "../core/mermaid.js"
15
+
16
+ const DEFAULT_DIRECTION = "TD" satisfies FlowchartDirection
17
+ const FLOWCHART_HEADER_RE = /^(flowchart|graph)(?:\s+(TB|TD|BT|LR|RL))?$/i
18
+ const ID_RE = "[A-Za-z_][A-Za-z0-9_.-]*"
19
+ const SUBGRAPH_RE = /^subgraph\s+(.+)$/i
20
+ const SUBGRAPH_WITH_LABEL_RE = new RegExp(`^(${ID_RE})\\s*\\[(.+)\\]$`)
21
+ const SUBGRAPH_DIRECTION_RE = /^direction\s+(TB|TD|BT|LR|RL)$/i
22
+ const IGNORED_PRESENTATION_RE = /^(?:classDef|class|style|linkStyle)\b/i
23
+ const DATABASE_NODE_RE = new RegExp(`^(${ID_RE})\\[\\((.+)\\)\\]$`)
24
+ const SUBROUTINE_NODE_RE = new RegExp(`^(${ID_RE})\\[\\[(.+)\\]\\]$`)
25
+ const ROUNDED_BRACKET_NODE_RE = new RegExp(`^(${ID_RE})\\(\\[(.+)\\]\\)$`)
26
+ const ROUNDED_NODE_RE = new RegExp(`^(${ID_RE})\\((.+)\\)$`)
27
+ const DECISION_NODE_RE = new RegExp(`^(${ID_RE})\\{(.+)\\}$`)
28
+ const BOX_NODE_RE = new RegExp(`^(${ID_RE})\\[(.+)\\]$`)
29
+ const ID_ONLY_RE = new RegExp(`^${ID_RE}$`)
30
+ const EXPLICIT_NODE_SHAPE_RE = new RegExp(`^${ID_RE}(?:\\[|\\(|\\{)`)
31
+ const CIRCLE_NODE_RE = new RegExp(`^${ID_RE}\\(\\(.+\\)\\)$`)
32
+ const MAX_FLOWCHART_LINE_LENGTH = 10_000
33
+ const EDGE_OPERATOR_RE =
34
+ /(-\.(?!->)(.+?)\.(?:->|-))|(--|==|-\.)\s+(.+?)\s+(-->|==>|\.->|-\.->|\.-)|(<-->|-->|==>|-\.->|---|~~~)\s*(?:\|([^|]*)\|\s*)?/dg
35
+
36
+ function normalizeDirection(value?: string): FlowchartDirection {
37
+ const upper = value?.toUpperCase()
38
+ if (upper === "TB" || upper === "TD" || upper === "BT" || upper === "LR" || upper === "RL") return upper
39
+ return DEFAULT_DIRECTION
40
+ }
41
+
42
+ function normalizeSubgraphId(value: string, index: number): string {
43
+ const stripped = stripQuotes(value)
44
+ return ID_ONLY_RE.test(stripped) ? stripped : `subgraph_${index + 1}`
45
+ }
46
+
47
+ function parseSubgraphToken(token: string, index: number): Pick<FlowchartSubgraph, "id" | "label"> {
48
+ const trimmed = token
49
+ .trim()
50
+ .replace(/\s*:::.*$/, "")
51
+ .replace(/;$/, "")
52
+ const withLabel = trimmed.match(SUBGRAPH_WITH_LABEL_RE)
53
+ if (withLabel) {
54
+ return { id: withLabel[1]!, label: stripQuotes(withLabel[2]!) }
55
+ }
56
+
57
+ const label = stripQuotes(trimmed)
58
+ return { id: normalizeSubgraphId(trimmed, index), label }
59
+ }
60
+
61
+ function parseNodeToken(token: string): FlowchartNode {
62
+ const trimmed = token.trim().replace(/;$/, "")
63
+ const database = trimmed.match(DATABASE_NODE_RE)
64
+ if (database) return { id: database[1]!, label: stripQuotes(database[2]!), shape: "database" }
65
+
66
+ const subroutine = trimmed.match(SUBROUTINE_NODE_RE)
67
+ if (subroutine) return { id: subroutine[1]!, label: stripQuotes(subroutine[2]!), shape: "subroutine" }
68
+
69
+ const roundedBracket = trimmed.match(ROUNDED_BRACKET_NODE_RE)
70
+ if (roundedBracket) return { id: roundedBracket[1]!, label: stripQuotes(roundedBracket[2]!), shape: "rounded" }
71
+
72
+ const rounded = trimmed.match(ROUNDED_NODE_RE)
73
+ if (rounded) return { id: rounded[1]!, label: stripQuotes(rounded[2]!), shape: "rounded" }
74
+
75
+ const decision = trimmed.match(DECISION_NODE_RE)
76
+ if (decision) return { id: decision[1]!, label: stripQuotes(decision[2]!), shape: "decision" }
77
+
78
+ const box = trimmed.match(BOX_NODE_RE)
79
+ if (box) return { id: box[1]!, label: stripQuotes(box[2]!), shape: "box" }
80
+
81
+ return { id: trimmed, label: trimmed, shape: "box" }
82
+ }
83
+
84
+ function isSupportedNodeToken(token: string): boolean {
85
+ const trimmed = stripNodeToken(token)
86
+ if (CIRCLE_NODE_RE.test(trimmed)) return false
87
+ return (
88
+ ID_ONLY_RE.test(trimmed) ||
89
+ DATABASE_NODE_RE.test(trimmed) ||
90
+ SUBROUTINE_NODE_RE.test(trimmed) ||
91
+ ROUNDED_BRACKET_NODE_RE.test(trimmed) ||
92
+ ROUNDED_NODE_RE.test(trimmed) ||
93
+ DECISION_NODE_RE.test(trimmed) ||
94
+ BOX_NODE_RE.test(trimmed)
95
+ )
96
+ }
97
+
98
+ function hasExplicitNodeShape(token: string): boolean {
99
+ return EXPLICIT_NODE_SHAPE_RE.test(token.trim())
100
+ }
101
+
102
+ function ensureNode(nodes: Map<string, FlowchartNode>, token: string): FlowchartNode {
103
+ const node = parseNodeToken(token)
104
+ const existing = nodes.get(node.id)
105
+ if (!existing) {
106
+ nodes.set(node.id, node)
107
+ return node
108
+ }
109
+
110
+ if (hasExplicitNodeShape(token)) {
111
+ existing.label = node.label
112
+ existing.shape = node.shape
113
+ }
114
+ return existing
115
+ }
116
+
117
+ function addNodeToSubgraph(subgraph: FlowchartSubgraph | undefined, nodeId: string): void {
118
+ if (!subgraph || subgraph.nodeIds.includes(nodeId)) return
119
+ subgraph.nodeIds.push(nodeId)
120
+ }
121
+
122
+ function stripNodeToken(token: string): string {
123
+ return token
124
+ .replace(/\s*:::.*$/, "")
125
+ .replace(/;$/, "")
126
+ .trim()
127
+ }
128
+
129
+ /** Split an `&`-joined node group, leaving `&` inside labels (brackets or quotes) untouched. */
130
+ function splitNodeGroup(token: string): string[] {
131
+ const groups: string[] = []
132
+ const stack: string[] = []
133
+ let quoted = false
134
+ let start = 0
135
+ const closes: Record<string, string> = { "[": "]", "(": ")", "{": "}" }
136
+
137
+ for (let index = 0; index < token.length; index++) {
138
+ const character = token[index]!
139
+ if (quoted) {
140
+ if (character === '"' && token[index - 1] !== "\\") quoted = false
141
+ continue
142
+ }
143
+ if (character === '"') {
144
+ quoted = true
145
+ continue
146
+ }
147
+ if (character in closes) {
148
+ stack.push(character)
149
+ continue
150
+ }
151
+ if (stack.length > 0 && character === closes[stack.at(-1)!]) {
152
+ stack.pop()
153
+ continue
154
+ }
155
+ if (stack.length === 0 && character === "&") {
156
+ groups.push(token.slice(start, index))
157
+ start = index + 1
158
+ }
159
+ }
160
+ groups.push(token.slice(start))
161
+ return groups
162
+ }
163
+
164
+ function edgeStyleFromArrow(...arrows: string[]): FlowchartEdgeStyle | undefined {
165
+ if (arrows.some((arrow) => arrow.includes("=="))) return "thick"
166
+ if (arrows.some((arrow) => arrow.includes("."))) return "dashed"
167
+ return undefined
168
+ }
169
+
170
+ function createEdge(
171
+ from: string,
172
+ to: string,
173
+ label: string,
174
+ style: FlowchartEdgeStyle | undefined,
175
+ arrowhead: boolean,
176
+ sourceArrowhead: boolean,
177
+ ): FlowchartEdge {
178
+ const edge: FlowchartEdge = style ? { from, to, label, style } : { from, to, label }
179
+ if (!arrowhead) edge.arrowhead = false
180
+ if (sourceArrowhead) edge.sourceArrowhead = true
181
+ return edge
182
+ }
183
+
184
+ interface ParsedEdgeOperator {
185
+ index: number
186
+ end: number
187
+ label: string
188
+ style: FlowchartEdgeStyle | undefined
189
+ arrowhead: boolean
190
+ sourceArrowhead: boolean
191
+ orderOnly: boolean
192
+ }
193
+
194
+ function parseEdgeOperators(line: string): ParsedEdgeOperator[] {
195
+ return [...maskNodeLabelOperators(line).matchAll(EDGE_OPERATOR_RE)].map((match) => {
196
+ const inlineDashedArrow = match[1]
197
+ const startArrow = inlineDashedArrow ?? match[3] ?? match[6]!
198
+ const endArrow = inlineDashedArrow ?? match[5] ?? match[6]!
199
+ const labelGroup = match[2] ? 2 : match[4] ? 4 : match[7] ? 7 : undefined
200
+ const labelRange = labelGroup === undefined ? undefined : match.indices?.[labelGroup]
201
+ return {
202
+ index: match.index,
203
+ end: match.index + match[0].length,
204
+ label: stripQuotes(labelRange ? line.slice(labelRange[0], labelRange[1]) : ""),
205
+ style: edgeStyleFromArrow(startArrow, endArrow),
206
+ arrowhead: endArrow === "~~~" || endArrow.endsWith(">"),
207
+ sourceArrowhead: startArrow.startsWith("<"),
208
+ orderOnly: endArrow === "~~~",
209
+ }
210
+ })
211
+ }
212
+
213
+ function maskNodeLabelOperators(line: string): string {
214
+ const characters = line.split("")
215
+ const stack: string[] = []
216
+ let quoted = false
217
+ const closes: Record<string, string> = { "[": "]", "(": ")", "{": "}" }
218
+
219
+ for (let index = 0; index < characters.length; index++) {
220
+ const character = characters[index]!
221
+ if (quoted) {
222
+ if (character === '"' && characters[index - 1] !== "\\") quoted = false
223
+ else if (/[<>=.-]/.test(character)) characters[index] = " "
224
+ continue
225
+ }
226
+ if (character === '"') {
227
+ quoted = true
228
+ continue
229
+ }
230
+ if (character in closes) {
231
+ stack.push(character)
232
+ continue
233
+ }
234
+ if (stack.length > 0 && character === closes[stack.at(-1)!]) {
235
+ stack.pop()
236
+ continue
237
+ }
238
+ if (stack.length > 0 && /[<>=.-]/.test(character)) characters[index] = " "
239
+ }
240
+ return characters.join("")
241
+ }
242
+
243
+ function hasInternalStatementSeparator(line: string): boolean {
244
+ const stack: string[] = []
245
+ let quoted = false
246
+ let edgeLabel = false
247
+ const closes: Record<string, string> = { "[": "]", "(": ")", "{": "}" }
248
+ const finalIndex = line.trimEnd().length - 1
249
+
250
+ for (let index = 0; index < line.length; index++) {
251
+ const character = line[index]!
252
+ if (quoted) {
253
+ if (character === '"' && line[index - 1] !== "\\") quoted = false
254
+ continue
255
+ }
256
+ if (character === '"') {
257
+ quoted = true
258
+ continue
259
+ }
260
+ if (character in closes) {
261
+ stack.push(character)
262
+ continue
263
+ }
264
+ if (stack.length > 0 && character === closes[stack.at(-1)!]) {
265
+ stack.pop()
266
+ continue
267
+ }
268
+ if (stack.length === 0 && character === "|") {
269
+ edgeLabel = !edgeLabel
270
+ continue
271
+ }
272
+ if (character === ";" && index < finalIndex && stack.length === 0 && !edgeLabel) return true
273
+ }
274
+ return false
275
+ }
276
+
277
+ function resolveSubgraphEndpoints(
278
+ nodes: Map<string, FlowchartNode>,
279
+ edges: readonly FlowchartEdge[],
280
+ subgraphs: FlowchartSubgraph[],
281
+ explicitShapeIds: ReadonlySet<string>,
282
+ referencedInOpenSubgraphs: ReadonlyMap<string, ReadonlySet<string>>,
283
+ ): void {
284
+ const resolved = new Set<string>()
285
+ const descendantsOf = (subgraphId: string): Set<string> => {
286
+ const ids = new Set<string>()
287
+ for (const child of subgraphs) {
288
+ if (child.parentId !== subgraphId) continue
289
+ ids.add(child.id)
290
+ for (const nested of descendantsOf(child.id)) ids.add(nested)
291
+ }
292
+ return ids
293
+ }
294
+ const membersOf = (subgraphId: string, descendants: ReadonlySet<string>): Set<string> => {
295
+ const ids = new Set<string>()
296
+ for (const candidate of subgraphs) {
297
+ if (candidate.id !== subgraphId && !descendants.has(candidate.id)) continue
298
+ for (const nodeId of candidate.nodeIds) {
299
+ if (nodeId === subgraphId || descendants.has(nodeId) || resolved.has(nodeId)) continue
300
+ ids.add(nodeId)
301
+ }
302
+ }
303
+ return ids
304
+ }
305
+
306
+ for (const subgraph of [...subgraphs].reverse()) {
307
+ subgraph.nodeIds = subgraph.nodeIds.filter((id) => !resolved.has(id))
308
+ const descendants = descendantsOf(subgraph.id)
309
+ const members = membersOf(subgraph.id, descendants)
310
+ const linksSelfOrDescendant = edges.some(
311
+ (edge) =>
312
+ (edge.from === subgraph.id && (edge.to === subgraph.id || members.has(edge.to) || descendants.has(edge.to))) ||
313
+ (edge.to === subgraph.id && (members.has(edge.from) || descendants.has(edge.from))),
314
+ )
315
+ if (
316
+ explicitShapeIds.has(subgraph.id) ||
317
+ referencedInOpenSubgraphs.get(subgraph.id)?.has(subgraph.id) ||
318
+ members.size === 0 ||
319
+ linksSelfOrDescendant
320
+ ) {
321
+ continue
322
+ }
323
+ resolved.add(subgraph.id)
324
+ nodes.delete(subgraph.id)
325
+ }
326
+
327
+ for (const subgraph of subgraphs) {
328
+ subgraph.nodeIds = subgraph.nodeIds.filter((id) => !resolved.has(id))
329
+ }
330
+ }
331
+
332
+ export function isMermaidFlowchartDiagram(content: string): boolean {
333
+ return FLOWCHART_HEADER_RE.test(firstMeaningfulMermaidLine(content) ?? "")
334
+ }
335
+
336
+ export function parseMermaidFlowchartDiagram(content: string): FlowchartDiagram {
337
+ const nodes = new Map<string, FlowchartNode>()
338
+ const edges: FlowchartEdge[] = []
339
+ const subgraphs: FlowchartSubgraph[] = []
340
+ const subgraphStack: Array<{ subgraph: FlowchartSubgraph; lineNumber: number; sourceLine: string }> = []
341
+ const explicitShapeIds = new Set<string>()
342
+ const referencedInOpenSubgraphs = new Map<string, Set<string>>()
343
+ const trackNodeToken = (stripped: string): string => {
344
+ const node = ensureNode(nodes, stripped)
345
+ if (hasExplicitNodeShape(stripped)) explicitShapeIds.add(node.id)
346
+ for (const open of subgraphStack) {
347
+ const set = referencedInOpenSubgraphs.get(node.id) ?? new Set<string>()
348
+ set.add(open.subgraph.id)
349
+ referencedInOpenSubgraphs.set(node.id, set)
350
+ }
351
+ return node.id
352
+ }
353
+ let direction: FlowchartDirection = DEFAULT_DIRECTION
354
+
355
+ for (const source of meaningfulNumberedMermaidLines(content)) {
356
+ const line = source.text
357
+ if (line.length > MAX_FLOWCHART_LINE_LENGTH) {
358
+ throw new MermaidSyntaxError("flowchart", source.lineNumber, line, "Flowchart statement is too long")
359
+ }
360
+ if (hasInternalStatementSeparator(line)) throw new MermaidSyntaxError("flowchart", source.lineNumber, line)
361
+ const header = line.match(FLOWCHART_HEADER_RE)
362
+ if (header) {
363
+ direction = normalizeDirection(header[2])
364
+ continue
365
+ }
366
+
367
+ // Mermaid CSS styling does not apply to terminal theme rendering.
368
+ if (IGNORED_PRESENTATION_RE.test(line)) continue
369
+
370
+ const subgraphMatch = line.match(SUBGRAPH_RE)
371
+ if (subgraphMatch) {
372
+ const parsed = parseSubgraphToken(subgraphMatch[1]!, subgraphs.length)
373
+ const subgraph: FlowchartSubgraph = {
374
+ ...parsed,
375
+ nodeIds: [],
376
+ parentId: subgraphStack[subgraphStack.length - 1]?.subgraph.id,
377
+ }
378
+ subgraphs.push(subgraph)
379
+ subgraphStack.push({ subgraph, lineNumber: source.lineNumber, sourceLine: line })
380
+ continue
381
+ }
382
+
383
+ if (/^end$/i.test(line)) {
384
+ if (subgraphStack.length === 0) {
385
+ throw new MermaidSyntaxError("flowchart", source.lineNumber, line, 'Unexpected "end" without an open subgraph')
386
+ }
387
+ subgraphStack.pop()
388
+ continue
389
+ }
390
+
391
+ const currentSubgraph = subgraphStack[subgraphStack.length - 1]?.subgraph
392
+
393
+ const subgraphDirection = line.match(SUBGRAPH_DIRECTION_RE)
394
+ if (subgraphDirection) {
395
+ if (!currentSubgraph) {
396
+ throw new MermaidSyntaxError(
397
+ "flowchart",
398
+ source.lineNumber,
399
+ line,
400
+ 'A "direction" statement requires an open subgraph',
401
+ )
402
+ }
403
+ currentSubgraph.direction = normalizeDirection(subgraphDirection[1])
404
+ continue
405
+ }
406
+
407
+ const edgeOperators = parseEdgeOperators(line)
408
+ if (edgeOperators.length > 0) {
409
+ // Each chain position may be an `&` group (`A & B --> C`), so endpoints are lists of node tokens.
410
+ const nodeGroups = [
411
+ line.slice(0, edgeOperators[0]!.index),
412
+ ...edgeOperators.map((operator, index) =>
413
+ line.slice(operator.end, edgeOperators[index + 1]?.index ?? line.length),
414
+ ),
415
+ ].map((group) => splitNodeGroup(group).map(stripNodeToken))
416
+
417
+ if (nodeGroups.every((group) => group.every((token) => token.length > 0))) {
418
+ const unsupportedEndpoint = nodeGroups.find((group) =>
419
+ group.some((stripped) => !isSupportedNodeToken(stripped)),
420
+ )
421
+ if (unsupportedEndpoint) throw new MermaidSyntaxError("flowchart", source.lineNumber, line)
422
+ const chainNodeIds = nodeGroups.map((group) => group.map(trackNodeToken))
423
+ for (const nodeId of chainNodeIds.flat()) {
424
+ if (nodes.has(nodeId)) addNodeToSubgraph(currentSubgraph, nodeId)
425
+ }
426
+ for (let index = 0; index < edgeOperators.length; index++) {
427
+ const operator = edgeOperators[index]!
428
+ for (const from of chainNodeIds[index]!) {
429
+ for (const to of chainNodeIds[index + 1]!) {
430
+ const edge = createEdge(
431
+ from,
432
+ to,
433
+ operator.label,
434
+ operator.style,
435
+ operator.arrowhead,
436
+ operator.sourceArrowhead,
437
+ )
438
+ edges.push(operator.orderOnly ? { ...edge, orderOnly: true } : edge)
439
+ }
440
+ }
441
+ }
442
+ continue
443
+ }
444
+ }
445
+
446
+ const nodeGroup = splitNodeGroup(line)
447
+ if (nodeGroup.every(isSupportedNodeToken)) {
448
+ for (const token of nodeGroup) addNodeToSubgraph(currentSubgraph, trackNodeToken(stripNodeToken(token)))
449
+ continue
450
+ }
451
+
452
+ throw new MermaidSyntaxError("flowchart", source.lineNumber, line)
453
+ }
454
+
455
+ const unclosedSubgraph = subgraphStack[subgraphStack.length - 1]
456
+ if (unclosedSubgraph) {
457
+ throw new MermaidSyntaxError(
458
+ "flowchart",
459
+ unclosedSubgraph.lineNumber,
460
+ unclosedSubgraph.sourceLine,
461
+ 'Unclosed subgraph; expected "end"',
462
+ )
463
+ }
464
+
465
+ resolveSubgraphEndpoints(nodes, edges, subgraphs, explicitShapeIds, referencedInOpenSubgraphs)
466
+ return { direction, nodes: [...nodes.values()], edges, subgraphs }
467
+ }
@@ -0,0 +1,10 @@
1
+ import { drawFlowchartDiagramGrid } from "./drawing.js"
2
+ import type { FlowchartDiagramRenderOptions } from "./options.js"
3
+ import { parseMermaidFlowchartDiagram } from "./parser.js"
4
+
5
+ export function renderFlowchartDiagram(content: string, options: FlowchartDiagramRenderOptions = {}): string {
6
+ return drawFlowchartDiagramGrid(parseMermaidFlowchartDiagram(content), options).toString({
7
+ trimTop: true,
8
+ trimBottom: true,
9
+ })
10
+ }