@zyro-sec/merman 1.0.2 → 1.0.3
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/package.json +2 -2
- package/script/layout-audit.ts +98 -98
- package/src/core/canvas.ts +228 -228
- package/src/core/color/style.ts +56 -56
- package/src/core/drawing.ts +301 -301
- package/src/core/geometry.ts +347 -347
- package/src/core/mermaid.ts +38 -38
- package/src/core/render-grid.ts +51 -51
- package/src/core/spatial.ts +243 -243
- package/src/core/text-lines.ts +32 -32
- package/src/core/text.ts +44 -44
- package/src/detect.ts +17 -17
- package/src/diagnostics.ts +16 -16
- package/src/flowchart/drawing.ts +340 -340
- package/src/flowchart/labels.ts +144 -144
- package/src/flowchart/layout.ts +1427 -1427
- package/src/flowchart/options.ts +12 -12
- package/src/flowchart/parser.ts +467 -467
- package/src/flowchart/render.ts +10 -10
- package/src/flowchart/routing.ts +1542 -1542
- package/src/flowchart/style.ts +79 -79
- package/src/flowchart/types.ts +65 -65
- package/src/gantt/diagram.ts +8 -8
- package/src/gantt/drawing.ts +191 -191
- package/src/gantt/parser.ts +161 -161
- package/src/gantt/render-grid.ts +17 -17
- package/src/gantt/style.ts +36 -36
- package/src/gantt/types.ts +44 -44
- package/src/gitgraph/diagram.ts +8 -8
- package/src/gitgraph/drawing.ts +234 -234
- package/src/gitgraph/parser.ts +219 -219
- package/src/gitgraph/render-grid.ts +17 -17
- package/src/gitgraph/style.ts +37 -37
- package/src/gitgraph/types.ts +36 -36
- package/src/markdown.ts +336 -336
- package/src/palette.ts +75 -75
- package/src/plugin.ts +15 -15
- package/src/sequence/diagram.ts +8 -8
- package/src/sequence/drawing.ts +287 -287
- package/src/sequence/options.ts +8 -8
- package/src/sequence/parser.ts +282 -282
- package/src/sequence/placement.ts +684 -684
- package/src/sequence/render-grid.ts +22 -22
- package/src/sequence/style.ts +81 -81
- package/src/sequence/types.ts +74 -74
- package/src/state/diagram.ts +8 -8
- package/src/state/drawing.ts +360 -360
- package/src/state/endpoint.ts +15 -15
- package/src/state/layout.ts +1134 -1134
- package/src/state/note.ts +59 -59
- package/src/state/options.ts +11 -11
- package/src/state/parser.ts +224 -224
- package/src/state/render-grid.ts +22 -22
- package/src/state/routing.ts +1522 -1522
- package/src/state/search.ts +103 -103
- package/src/state/style.ts +52 -52
- package/src/state/types.ts +63 -63
- package/src/state/visible-model.ts +77 -77
- package/src/test/diagram.ts +32 -32
- package/src/test/layout-audit/fixtures.ts +538 -538
- package/src/test/layout-audit/harness.ts +522 -522
- package/src/timeline/diagram.ts +8 -8
- package/src/timeline/drawing.ts +109 -109
- package/src/timeline/parser.ts +119 -119
- package/src/timeline/render-grid.ts +17 -17
- package/src/timeline/style.ts +36 -36
- package/src/timeline/types.ts +31 -31
- package/tsconfig.json +10 -10
- package/src/core/canvas.test.ts +0 -203
- package/src/core/color/style.test.ts +0 -24
- package/src/core/drawing.test.ts +0 -116
- package/src/core/geometry.test.ts +0 -112
- package/src/core/spatial.test.ts +0 -87
- package/src/core/text.test.ts +0 -33
- package/src/flowchart/flowchart.test.ts +0 -2800
- package/src/flowchart/labels.test.ts +0 -127
- package/src/flowchart/routing.test.ts +0 -368
- package/src/gantt/diagram.test.ts +0 -124
- package/src/gitgraph/diagram.test.ts +0 -183
- package/src/layout-audit.test.ts +0 -92
- package/src/palette.test.ts +0 -103
- package/src/sequence/diagram.test.ts +0 -757
- package/src/sequence/placement.test.ts +0 -219
- package/src/state/diagram.test.ts +0 -1386
- package/src/state/layout.test.ts +0 -314
- package/src/state/routing.test.ts +0 -693
- package/src/state/style.test.ts +0 -113
- package/src/state/visible-model.test.ts +0 -84
- package/src/test/diagnostics.test.ts +0 -129
- package/src/test/markdown.test.ts +0 -474
- package/src/timeline/diagram.test.ts +0 -188
package/src/sequence/parser.ts
CHANGED
|
@@ -1,282 +1,282 @@
|
|
|
1
|
-
import {
|
|
2
|
-
firstMeaningfulMermaidLine,
|
|
3
|
-
meaningfulNumberedMermaidLines,
|
|
4
|
-
stripMermaidQuotes as stripQuotes,
|
|
5
|
-
} from "../core/mermaid.js"
|
|
6
|
-
import { MermaidSyntaxError } from "../diagnostics.js"
|
|
7
|
-
import type {
|
|
8
|
-
SequenceArrowHead,
|
|
9
|
-
SequenceDiagram,
|
|
10
|
-
SequenceMessage,
|
|
11
|
-
SequenceParticipant,
|
|
12
|
-
SequenceParticipantGroup,
|
|
13
|
-
SequenceStep,
|
|
14
|
-
} from "./types.js"
|
|
15
|
-
|
|
16
|
-
const MESSAGE_RE = /^(.+?)\s*(-->>|->>|--x|-x|--\)|-\)|-->|->)([+-]?)\s*(.+?)\s*:\s*(.*)$/
|
|
17
|
-
const NOTE_RE = /^note\s+(over|left\s+of|right\s+of)\s+(.+?)\s*:\s*(.*)$/i
|
|
18
|
-
const PARTICIPANT_RE = /^(?:participant|actor)\s+(\S+)(?:\s+as\s+(.+))?$/i
|
|
19
|
-
const ACTIVATION_RE = /^(activate|deactivate)\s+(.+)$/i
|
|
20
|
-
const BOX_RE = /^box(?:\s+(.+))?$/i
|
|
21
|
-
const ALT_RE = /^alt\s+(.+)$/i
|
|
22
|
-
const ELSE_RE = /^else(?:\s+(.+))?$/i
|
|
23
|
-
const LOOP_RE = /^loop\s+(.+)$/i
|
|
24
|
-
const AUTONUMBER_RE = /^autonumber(?:\s+(\d+)(?:\s+(\d+))?)?$/i
|
|
25
|
-
const UNSUPPORTED_BIDIRECTIONAL_MESSAGE_RE = /<<-{1,2}>>/
|
|
26
|
-
const MESSAGE_OPERATOR_RE = /-->>|->>|--x|-x|--\)|-\)|-->|->/
|
|
27
|
-
const CSS_COLOR_NAMES = new Set([
|
|
28
|
-
"black",
|
|
29
|
-
"white",
|
|
30
|
-
"red",
|
|
31
|
-
"green",
|
|
32
|
-
"blue",
|
|
33
|
-
"yellow",
|
|
34
|
-
"cyan",
|
|
35
|
-
"magenta",
|
|
36
|
-
"silver",
|
|
37
|
-
"gray",
|
|
38
|
-
"grey",
|
|
39
|
-
"maroon",
|
|
40
|
-
"olive",
|
|
41
|
-
"lime",
|
|
42
|
-
"aqua",
|
|
43
|
-
"teal",
|
|
44
|
-
"navy",
|
|
45
|
-
"fuchsia",
|
|
46
|
-
"purple",
|
|
47
|
-
"orange",
|
|
48
|
-
"brightblack",
|
|
49
|
-
"brightred",
|
|
50
|
-
"brightgreen",
|
|
51
|
-
"brightblue",
|
|
52
|
-
"brightyellow",
|
|
53
|
-
"brightcyan",
|
|
54
|
-
"brightmagenta",
|
|
55
|
-
"brightwhite",
|
|
56
|
-
])
|
|
57
|
-
|
|
58
|
-
function arrowHeadForSyntax(arrow: string): SequenceArrowHead | undefined {
|
|
59
|
-
if (arrow.endsWith("x")) return "cross"
|
|
60
|
-
if (arrow.endsWith(")")) return "async"
|
|
61
|
-
if (arrow.endsWith(">") && !arrow.endsWith(">>")) return "open"
|
|
62
|
-
return undefined
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function isBoxColorToken(value: string): boolean {
|
|
66
|
-
const lowerValue = value.toLowerCase()
|
|
67
|
-
return (
|
|
68
|
-
lowerValue === "transparent" ||
|
|
69
|
-
CSS_COLOR_NAMES.has(lowerValue) ||
|
|
70
|
-
/^#[0-9a-f]{3,8}$/i.test(value) ||
|
|
71
|
-
/^rgba?\(.+\)$/i.test(value)
|
|
72
|
-
)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function splitLeadingBoxToken(value: string): { token: string; rest: string } {
|
|
76
|
-
if (/^rgba?\(/i.test(value)) {
|
|
77
|
-
const closeIndex = value.indexOf(")")
|
|
78
|
-
if (closeIndex >= 0) {
|
|
79
|
-
return { token: value.slice(0, closeIndex + 1), rest: value.slice(closeIndex + 1).trim() }
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const firstSpace = value.search(/\s/)
|
|
84
|
-
return firstSpace < 0
|
|
85
|
-
? { token: value, rest: "" }
|
|
86
|
-
: { token: value.slice(0, firstSpace), rest: value.slice(firstSpace + 1).trim() }
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function boxLabelText(value: string | undefined): string {
|
|
90
|
-
const rawLabel = (value ?? "").trim()
|
|
91
|
-
if ((rawLabel.startsWith('"') && rawLabel.endsWith('"')) || (rawLabel.startsWith("'") && rawLabel.endsWith("'"))) {
|
|
92
|
-
return stripQuotes(rawLabel)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const label = stripQuotes(rawLabel)
|
|
96
|
-
if (!label) return ""
|
|
97
|
-
const { token, rest } = splitLeadingBoxToken(label)
|
|
98
|
-
return isBoxColorToken(token) ? stripQuotes(rest) : label
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function addParticipantToGroup(group: SequenceParticipantGroup | undefined, participantId: string): void {
|
|
102
|
-
if (!group || group.participantIds.includes(participantId)) return
|
|
103
|
-
group.participantIds.push(participantId)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function ensureParticipant(
|
|
107
|
-
participants: SequenceParticipant[],
|
|
108
|
-
id: string,
|
|
109
|
-
label: string = id,
|
|
110
|
-
replaceExistingLabel: boolean = false,
|
|
111
|
-
): void {
|
|
112
|
-
const existing = participants.find((participant) => participant.id === id)
|
|
113
|
-
if (existing) {
|
|
114
|
-
if (replaceExistingLabel) existing.label = label
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
|
-
participants.push({ id, label })
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function isMermaidSequenceDiagram(content: string): boolean {
|
|
121
|
-
return firstMeaningfulMermaidLine(content)?.toLowerCase() === "sequencediagram"
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function parseMermaidSequenceDiagram(content: string): SequenceDiagram {
|
|
125
|
-
const participants: SequenceParticipant[] = []
|
|
126
|
-
const messages: SequenceMessage[] = []
|
|
127
|
-
const steps: SequenceStep[] = []
|
|
128
|
-
const groups: SequenceParticipantGroup[] = []
|
|
129
|
-
const blockStack: Array<{ kind: "box" | "alt" | "loop"; lineNumber: number; sourceLine: string }> = []
|
|
130
|
-
const groupStack: SequenceParticipantGroup[] = []
|
|
131
|
-
let nextMessageNumber: number | undefined
|
|
132
|
-
let messageNumberIncrement = 1
|
|
133
|
-
|
|
134
|
-
for (const source of meaningfulNumberedMermaidLines(content)) {
|
|
135
|
-
const line = source.text
|
|
136
|
-
if (line.toLowerCase() === "sequencediagram") continue
|
|
137
|
-
if (UNSUPPORTED_BIDIRECTIONAL_MESSAGE_RE.test(line)) {
|
|
138
|
-
throw new MermaidSyntaxError("sequence", source.lineNumber, line)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const autonumberMatch = line.match(AUTONUMBER_RE)
|
|
142
|
-
if (autonumberMatch) {
|
|
143
|
-
nextMessageNumber = Number.parseInt(autonumberMatch[1] ?? "1", 10)
|
|
144
|
-
messageNumberIncrement = Number.parseInt(autonumberMatch[2] ?? "1", 10)
|
|
145
|
-
continue
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const boxMatch = line.match(BOX_RE)
|
|
149
|
-
if (boxMatch) {
|
|
150
|
-
const group: SequenceParticipantGroup = { label: boxLabelText(boxMatch[1]), participantIds: [] }
|
|
151
|
-
groups.push(group)
|
|
152
|
-
groupStack.push(group)
|
|
153
|
-
blockStack.push({ kind: "box", lineNumber: source.lineNumber, sourceLine: line })
|
|
154
|
-
continue
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const participantMatch = line.match(PARTICIPANT_RE)
|
|
158
|
-
if (participantMatch) {
|
|
159
|
-
const id = stripQuotes(participantMatch[1]!)
|
|
160
|
-
ensureParticipant(participants, id, stripQuotes(participantMatch[2] ?? id), true)
|
|
161
|
-
addParticipantToGroup(groupStack[groupStack.length - 1], id)
|
|
162
|
-
continue
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const noteMatch = line.match(NOTE_RE)
|
|
166
|
-
if (noteMatch) {
|
|
167
|
-
const position = noteMatch[1]!.toLowerCase().replace(/\s+of$/, "") as "over" | "left" | "right"
|
|
168
|
-
const over = noteMatch[2]!
|
|
169
|
-
.split(",")
|
|
170
|
-
.map((participant) => stripQuotes(participant))
|
|
171
|
-
.filter((participant) => participant.length > 0)
|
|
172
|
-
for (const participant of over) ensureParticipant(participants, participant)
|
|
173
|
-
steps.push({
|
|
174
|
-
type: "note",
|
|
175
|
-
note: {
|
|
176
|
-
over,
|
|
177
|
-
label: stripQuotes(noteMatch[3]!),
|
|
178
|
-
...(position === "over" ? {} : { position }),
|
|
179
|
-
},
|
|
180
|
-
})
|
|
181
|
-
continue
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const activationMatch = line.match(ACTIVATION_RE)
|
|
185
|
-
if (activationMatch) {
|
|
186
|
-
const participant = stripQuotes(activationMatch[2]!)
|
|
187
|
-
ensureParticipant(participants, participant)
|
|
188
|
-
steps.push({
|
|
189
|
-
type: "activation",
|
|
190
|
-
activation: { participant, active: activationMatch[1]!.toLowerCase() === "activate" },
|
|
191
|
-
})
|
|
192
|
-
continue
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const altMatch = line.match(ALT_RE)
|
|
196
|
-
if (altMatch) {
|
|
197
|
-
blockStack.push({ kind: "alt", lineNumber: source.lineNumber, sourceLine: line })
|
|
198
|
-
steps.push({ type: "fragment", fragment: { kind: "alt", label: stripQuotes(altMatch[1]!) } })
|
|
199
|
-
continue
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const loopMatch = line.match(LOOP_RE)
|
|
203
|
-
if (loopMatch) {
|
|
204
|
-
blockStack.push({ kind: "loop", lineNumber: source.lineNumber, sourceLine: line })
|
|
205
|
-
steps.push({ type: "fragment", fragment: { kind: "loop", label: stripQuotes(loopMatch[1]!) } })
|
|
206
|
-
continue
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const elseMatch = line.match(ELSE_RE)
|
|
210
|
-
if (elseMatch) {
|
|
211
|
-
if (blockStack[blockStack.length - 1]?.kind !== "alt") {
|
|
212
|
-
throw new MermaidSyntaxError(
|
|
213
|
-
"sequence",
|
|
214
|
-
source.lineNumber,
|
|
215
|
-
line,
|
|
216
|
-
'Unexpected "else" without an open "alt" block',
|
|
217
|
-
)
|
|
218
|
-
}
|
|
219
|
-
steps.push({ type: "fragment", fragment: { kind: "else", label: stripQuotes(elseMatch[1] ?? "") } })
|
|
220
|
-
continue
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (line.toLowerCase() === "end") {
|
|
224
|
-
const block = blockStack.pop()
|
|
225
|
-
if (!block)
|
|
226
|
-
throw new MermaidSyntaxError("sequence", source.lineNumber, line, 'Unexpected "end" without an open block')
|
|
227
|
-
if (block.kind === "box") {
|
|
228
|
-
groupStack.pop()
|
|
229
|
-
continue
|
|
230
|
-
}
|
|
231
|
-
steps.push({ type: "fragment", fragment: { kind: "end", label: block.kind } })
|
|
232
|
-
continue
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const messageMatch = line.match(MESSAGE_RE)
|
|
236
|
-
if (messageMatch) {
|
|
237
|
-
const from = stripQuotes(messageMatch[1]!)
|
|
238
|
-
const arrow = messageMatch[2]!
|
|
239
|
-
const activationMarker = messageMatch[3]!
|
|
240
|
-
const to = stripQuotes(messageMatch[4]!)
|
|
241
|
-
if (MESSAGE_OPERATOR_RE.test(from) || MESSAGE_OPERATOR_RE.test(to)) {
|
|
242
|
-
throw new MermaidSyntaxError("sequence", source.lineNumber, line)
|
|
243
|
-
}
|
|
244
|
-
const message: SequenceMessage = {
|
|
245
|
-
from,
|
|
246
|
-
to,
|
|
247
|
-
label: stripQuotes(messageMatch[5]!),
|
|
248
|
-
style: arrow.startsWith("--") ? "dashed" : "solid",
|
|
249
|
-
}
|
|
250
|
-
const activeGroup = groupStack[groupStack.length - 1]
|
|
251
|
-
ensureParticipant(participants, from)
|
|
252
|
-
ensureParticipant(participants, to)
|
|
253
|
-
addParticipantToGroup(activeGroup, from)
|
|
254
|
-
addParticipantToGroup(activeGroup, to)
|
|
255
|
-
const head = arrowHeadForSyntax(arrow)
|
|
256
|
-
if (head) message.head = head
|
|
257
|
-
if (nextMessageNumber !== undefined) {
|
|
258
|
-
message.number = nextMessageNumber
|
|
259
|
-
nextMessageNumber += messageNumberIncrement
|
|
260
|
-
}
|
|
261
|
-
if (activationMarker === "+") message.activate = to
|
|
262
|
-
else if (activationMarker === "-") message.deactivate = from
|
|
263
|
-
messages.push(message)
|
|
264
|
-
steps.push({ type: "message", message })
|
|
265
|
-
continue
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
throw new MermaidSyntaxError("sequence", source.lineNumber, line)
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const unclosedBlock = blockStack[blockStack.length - 1]
|
|
272
|
-
if (unclosedBlock) {
|
|
273
|
-
throw new MermaidSyntaxError(
|
|
274
|
-
"sequence",
|
|
275
|
-
unclosedBlock.lineNumber,
|
|
276
|
-
unclosedBlock.sourceLine,
|
|
277
|
-
`Unclosed ${unclosedBlock.kind} block; expected "end"`,
|
|
278
|
-
)
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return { participants, messages, steps, groups }
|
|
282
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
firstMeaningfulMermaidLine,
|
|
3
|
+
meaningfulNumberedMermaidLines,
|
|
4
|
+
stripMermaidQuotes as stripQuotes,
|
|
5
|
+
} from "../core/mermaid.js"
|
|
6
|
+
import { MermaidSyntaxError } from "../diagnostics.js"
|
|
7
|
+
import type {
|
|
8
|
+
SequenceArrowHead,
|
|
9
|
+
SequenceDiagram,
|
|
10
|
+
SequenceMessage,
|
|
11
|
+
SequenceParticipant,
|
|
12
|
+
SequenceParticipantGroup,
|
|
13
|
+
SequenceStep,
|
|
14
|
+
} from "./types.js"
|
|
15
|
+
|
|
16
|
+
const MESSAGE_RE = /^(.+?)\s*(-->>|->>|--x|-x|--\)|-\)|-->|->)([+-]?)\s*(.+?)\s*:\s*(.*)$/
|
|
17
|
+
const NOTE_RE = /^note\s+(over|left\s+of|right\s+of)\s+(.+?)\s*:\s*(.*)$/i
|
|
18
|
+
const PARTICIPANT_RE = /^(?:participant|actor)\s+(\S+)(?:\s+as\s+(.+))?$/i
|
|
19
|
+
const ACTIVATION_RE = /^(activate|deactivate)\s+(.+)$/i
|
|
20
|
+
const BOX_RE = /^box(?:\s+(.+))?$/i
|
|
21
|
+
const ALT_RE = /^alt\s+(.+)$/i
|
|
22
|
+
const ELSE_RE = /^else(?:\s+(.+))?$/i
|
|
23
|
+
const LOOP_RE = /^loop\s+(.+)$/i
|
|
24
|
+
const AUTONUMBER_RE = /^autonumber(?:\s+(\d+)(?:\s+(\d+))?)?$/i
|
|
25
|
+
const UNSUPPORTED_BIDIRECTIONAL_MESSAGE_RE = /<<-{1,2}>>/
|
|
26
|
+
const MESSAGE_OPERATOR_RE = /-->>|->>|--x|-x|--\)|-\)|-->|->/
|
|
27
|
+
const CSS_COLOR_NAMES = new Set([
|
|
28
|
+
"black",
|
|
29
|
+
"white",
|
|
30
|
+
"red",
|
|
31
|
+
"green",
|
|
32
|
+
"blue",
|
|
33
|
+
"yellow",
|
|
34
|
+
"cyan",
|
|
35
|
+
"magenta",
|
|
36
|
+
"silver",
|
|
37
|
+
"gray",
|
|
38
|
+
"grey",
|
|
39
|
+
"maroon",
|
|
40
|
+
"olive",
|
|
41
|
+
"lime",
|
|
42
|
+
"aqua",
|
|
43
|
+
"teal",
|
|
44
|
+
"navy",
|
|
45
|
+
"fuchsia",
|
|
46
|
+
"purple",
|
|
47
|
+
"orange",
|
|
48
|
+
"brightblack",
|
|
49
|
+
"brightred",
|
|
50
|
+
"brightgreen",
|
|
51
|
+
"brightblue",
|
|
52
|
+
"brightyellow",
|
|
53
|
+
"brightcyan",
|
|
54
|
+
"brightmagenta",
|
|
55
|
+
"brightwhite",
|
|
56
|
+
])
|
|
57
|
+
|
|
58
|
+
function arrowHeadForSyntax(arrow: string): SequenceArrowHead | undefined {
|
|
59
|
+
if (arrow.endsWith("x")) return "cross"
|
|
60
|
+
if (arrow.endsWith(")")) return "async"
|
|
61
|
+
if (arrow.endsWith(">") && !arrow.endsWith(">>")) return "open"
|
|
62
|
+
return undefined
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function isBoxColorToken(value: string): boolean {
|
|
66
|
+
const lowerValue = value.toLowerCase()
|
|
67
|
+
return (
|
|
68
|
+
lowerValue === "transparent" ||
|
|
69
|
+
CSS_COLOR_NAMES.has(lowerValue) ||
|
|
70
|
+
/^#[0-9a-f]{3,8}$/i.test(value) ||
|
|
71
|
+
/^rgba?\(.+\)$/i.test(value)
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function splitLeadingBoxToken(value: string): { token: string; rest: string } {
|
|
76
|
+
if (/^rgba?\(/i.test(value)) {
|
|
77
|
+
const closeIndex = value.indexOf(")")
|
|
78
|
+
if (closeIndex >= 0) {
|
|
79
|
+
return { token: value.slice(0, closeIndex + 1), rest: value.slice(closeIndex + 1).trim() }
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const firstSpace = value.search(/\s/)
|
|
84
|
+
return firstSpace < 0
|
|
85
|
+
? { token: value, rest: "" }
|
|
86
|
+
: { token: value.slice(0, firstSpace), rest: value.slice(firstSpace + 1).trim() }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function boxLabelText(value: string | undefined): string {
|
|
90
|
+
const rawLabel = (value ?? "").trim()
|
|
91
|
+
if ((rawLabel.startsWith('"') && rawLabel.endsWith('"')) || (rawLabel.startsWith("'") && rawLabel.endsWith("'"))) {
|
|
92
|
+
return stripQuotes(rawLabel)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const label = stripQuotes(rawLabel)
|
|
96
|
+
if (!label) return ""
|
|
97
|
+
const { token, rest } = splitLeadingBoxToken(label)
|
|
98
|
+
return isBoxColorToken(token) ? stripQuotes(rest) : label
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function addParticipantToGroup(group: SequenceParticipantGroup | undefined, participantId: string): void {
|
|
102
|
+
if (!group || group.participantIds.includes(participantId)) return
|
|
103
|
+
group.participantIds.push(participantId)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function ensureParticipant(
|
|
107
|
+
participants: SequenceParticipant[],
|
|
108
|
+
id: string,
|
|
109
|
+
label: string = id,
|
|
110
|
+
replaceExistingLabel: boolean = false,
|
|
111
|
+
): void {
|
|
112
|
+
const existing = participants.find((participant) => participant.id === id)
|
|
113
|
+
if (existing) {
|
|
114
|
+
if (replaceExistingLabel) existing.label = label
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
participants.push({ id, label })
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function isMermaidSequenceDiagram(content: string): boolean {
|
|
121
|
+
return firstMeaningfulMermaidLine(content)?.toLowerCase() === "sequencediagram"
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function parseMermaidSequenceDiagram(content: string): SequenceDiagram {
|
|
125
|
+
const participants: SequenceParticipant[] = []
|
|
126
|
+
const messages: SequenceMessage[] = []
|
|
127
|
+
const steps: SequenceStep[] = []
|
|
128
|
+
const groups: SequenceParticipantGroup[] = []
|
|
129
|
+
const blockStack: Array<{ kind: "box" | "alt" | "loop"; lineNumber: number; sourceLine: string }> = []
|
|
130
|
+
const groupStack: SequenceParticipantGroup[] = []
|
|
131
|
+
let nextMessageNumber: number | undefined
|
|
132
|
+
let messageNumberIncrement = 1
|
|
133
|
+
|
|
134
|
+
for (const source of meaningfulNumberedMermaidLines(content)) {
|
|
135
|
+
const line = source.text
|
|
136
|
+
if (line.toLowerCase() === "sequencediagram") continue
|
|
137
|
+
if (UNSUPPORTED_BIDIRECTIONAL_MESSAGE_RE.test(line)) {
|
|
138
|
+
throw new MermaidSyntaxError("sequence", source.lineNumber, line)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const autonumberMatch = line.match(AUTONUMBER_RE)
|
|
142
|
+
if (autonumberMatch) {
|
|
143
|
+
nextMessageNumber = Number.parseInt(autonumberMatch[1] ?? "1", 10)
|
|
144
|
+
messageNumberIncrement = Number.parseInt(autonumberMatch[2] ?? "1", 10)
|
|
145
|
+
continue
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const boxMatch = line.match(BOX_RE)
|
|
149
|
+
if (boxMatch) {
|
|
150
|
+
const group: SequenceParticipantGroup = { label: boxLabelText(boxMatch[1]), participantIds: [] }
|
|
151
|
+
groups.push(group)
|
|
152
|
+
groupStack.push(group)
|
|
153
|
+
blockStack.push({ kind: "box", lineNumber: source.lineNumber, sourceLine: line })
|
|
154
|
+
continue
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const participantMatch = line.match(PARTICIPANT_RE)
|
|
158
|
+
if (participantMatch) {
|
|
159
|
+
const id = stripQuotes(participantMatch[1]!)
|
|
160
|
+
ensureParticipant(participants, id, stripQuotes(participantMatch[2] ?? id), true)
|
|
161
|
+
addParticipantToGroup(groupStack[groupStack.length - 1], id)
|
|
162
|
+
continue
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const noteMatch = line.match(NOTE_RE)
|
|
166
|
+
if (noteMatch) {
|
|
167
|
+
const position = noteMatch[1]!.toLowerCase().replace(/\s+of$/, "") as "over" | "left" | "right"
|
|
168
|
+
const over = noteMatch[2]!
|
|
169
|
+
.split(",")
|
|
170
|
+
.map((participant) => stripQuotes(participant))
|
|
171
|
+
.filter((participant) => participant.length > 0)
|
|
172
|
+
for (const participant of over) ensureParticipant(participants, participant)
|
|
173
|
+
steps.push({
|
|
174
|
+
type: "note",
|
|
175
|
+
note: {
|
|
176
|
+
over,
|
|
177
|
+
label: stripQuotes(noteMatch[3]!),
|
|
178
|
+
...(position === "over" ? {} : { position }),
|
|
179
|
+
},
|
|
180
|
+
})
|
|
181
|
+
continue
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const activationMatch = line.match(ACTIVATION_RE)
|
|
185
|
+
if (activationMatch) {
|
|
186
|
+
const participant = stripQuotes(activationMatch[2]!)
|
|
187
|
+
ensureParticipant(participants, participant)
|
|
188
|
+
steps.push({
|
|
189
|
+
type: "activation",
|
|
190
|
+
activation: { participant, active: activationMatch[1]!.toLowerCase() === "activate" },
|
|
191
|
+
})
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const altMatch = line.match(ALT_RE)
|
|
196
|
+
if (altMatch) {
|
|
197
|
+
blockStack.push({ kind: "alt", lineNumber: source.lineNumber, sourceLine: line })
|
|
198
|
+
steps.push({ type: "fragment", fragment: { kind: "alt", label: stripQuotes(altMatch[1]!) } })
|
|
199
|
+
continue
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const loopMatch = line.match(LOOP_RE)
|
|
203
|
+
if (loopMatch) {
|
|
204
|
+
blockStack.push({ kind: "loop", lineNumber: source.lineNumber, sourceLine: line })
|
|
205
|
+
steps.push({ type: "fragment", fragment: { kind: "loop", label: stripQuotes(loopMatch[1]!) } })
|
|
206
|
+
continue
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const elseMatch = line.match(ELSE_RE)
|
|
210
|
+
if (elseMatch) {
|
|
211
|
+
if (blockStack[blockStack.length - 1]?.kind !== "alt") {
|
|
212
|
+
throw new MermaidSyntaxError(
|
|
213
|
+
"sequence",
|
|
214
|
+
source.lineNumber,
|
|
215
|
+
line,
|
|
216
|
+
'Unexpected "else" without an open "alt" block',
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
steps.push({ type: "fragment", fragment: { kind: "else", label: stripQuotes(elseMatch[1] ?? "") } })
|
|
220
|
+
continue
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (line.toLowerCase() === "end") {
|
|
224
|
+
const block = blockStack.pop()
|
|
225
|
+
if (!block)
|
|
226
|
+
throw new MermaidSyntaxError("sequence", source.lineNumber, line, 'Unexpected "end" without an open block')
|
|
227
|
+
if (block.kind === "box") {
|
|
228
|
+
groupStack.pop()
|
|
229
|
+
continue
|
|
230
|
+
}
|
|
231
|
+
steps.push({ type: "fragment", fragment: { kind: "end", label: block.kind } })
|
|
232
|
+
continue
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const messageMatch = line.match(MESSAGE_RE)
|
|
236
|
+
if (messageMatch) {
|
|
237
|
+
const from = stripQuotes(messageMatch[1]!)
|
|
238
|
+
const arrow = messageMatch[2]!
|
|
239
|
+
const activationMarker = messageMatch[3]!
|
|
240
|
+
const to = stripQuotes(messageMatch[4]!)
|
|
241
|
+
if (MESSAGE_OPERATOR_RE.test(from) || MESSAGE_OPERATOR_RE.test(to)) {
|
|
242
|
+
throw new MermaidSyntaxError("sequence", source.lineNumber, line)
|
|
243
|
+
}
|
|
244
|
+
const message: SequenceMessage = {
|
|
245
|
+
from,
|
|
246
|
+
to,
|
|
247
|
+
label: stripQuotes(messageMatch[5]!),
|
|
248
|
+
style: arrow.startsWith("--") ? "dashed" : "solid",
|
|
249
|
+
}
|
|
250
|
+
const activeGroup = groupStack[groupStack.length - 1]
|
|
251
|
+
ensureParticipant(participants, from)
|
|
252
|
+
ensureParticipant(participants, to)
|
|
253
|
+
addParticipantToGroup(activeGroup, from)
|
|
254
|
+
addParticipantToGroup(activeGroup, to)
|
|
255
|
+
const head = arrowHeadForSyntax(arrow)
|
|
256
|
+
if (head) message.head = head
|
|
257
|
+
if (nextMessageNumber !== undefined) {
|
|
258
|
+
message.number = nextMessageNumber
|
|
259
|
+
nextMessageNumber += messageNumberIncrement
|
|
260
|
+
}
|
|
261
|
+
if (activationMarker === "+") message.activate = to
|
|
262
|
+
else if (activationMarker === "-") message.deactivate = from
|
|
263
|
+
messages.push(message)
|
|
264
|
+
steps.push({ type: "message", message })
|
|
265
|
+
continue
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
throw new MermaidSyntaxError("sequence", source.lineNumber, line)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const unclosedBlock = blockStack[blockStack.length - 1]
|
|
272
|
+
if (unclosedBlock) {
|
|
273
|
+
throw new MermaidSyntaxError(
|
|
274
|
+
"sequence",
|
|
275
|
+
unclosedBlock.lineNumber,
|
|
276
|
+
unclosedBlock.sourceLine,
|
|
277
|
+
`Unclosed ${unclosedBlock.kind} block; expected "end"`,
|
|
278
|
+
)
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return { participants, messages, steps, groups }
|
|
282
|
+
}
|