@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.
Files changed (91) hide show
  1. package/package.json +2 -2
  2. package/script/layout-audit.ts +98 -98
  3. package/src/core/canvas.ts +228 -228
  4. package/src/core/color/style.ts +56 -56
  5. package/src/core/drawing.ts +301 -301
  6. package/src/core/geometry.ts +347 -347
  7. package/src/core/mermaid.ts +38 -38
  8. package/src/core/render-grid.ts +51 -51
  9. package/src/core/spatial.ts +243 -243
  10. package/src/core/text-lines.ts +32 -32
  11. package/src/core/text.ts +44 -44
  12. package/src/detect.ts +17 -17
  13. package/src/diagnostics.ts +16 -16
  14. package/src/flowchart/drawing.ts +340 -340
  15. package/src/flowchart/labels.ts +144 -144
  16. package/src/flowchart/layout.ts +1427 -1427
  17. package/src/flowchart/options.ts +12 -12
  18. package/src/flowchart/parser.ts +467 -467
  19. package/src/flowchart/render.ts +10 -10
  20. package/src/flowchart/routing.ts +1542 -1542
  21. package/src/flowchart/style.ts +79 -79
  22. package/src/flowchart/types.ts +65 -65
  23. package/src/gantt/diagram.ts +8 -8
  24. package/src/gantt/drawing.ts +191 -191
  25. package/src/gantt/parser.ts +161 -161
  26. package/src/gantt/render-grid.ts +17 -17
  27. package/src/gantt/style.ts +36 -36
  28. package/src/gantt/types.ts +44 -44
  29. package/src/gitgraph/diagram.ts +8 -8
  30. package/src/gitgraph/drawing.ts +234 -234
  31. package/src/gitgraph/parser.ts +219 -219
  32. package/src/gitgraph/render-grid.ts +17 -17
  33. package/src/gitgraph/style.ts +37 -37
  34. package/src/gitgraph/types.ts +36 -36
  35. package/src/markdown.ts +336 -336
  36. package/src/palette.ts +75 -75
  37. package/src/plugin.ts +15 -15
  38. package/src/sequence/diagram.ts +8 -8
  39. package/src/sequence/drawing.ts +287 -287
  40. package/src/sequence/options.ts +8 -8
  41. package/src/sequence/parser.ts +282 -282
  42. package/src/sequence/placement.ts +684 -684
  43. package/src/sequence/render-grid.ts +22 -22
  44. package/src/sequence/style.ts +81 -81
  45. package/src/sequence/types.ts +74 -74
  46. package/src/state/diagram.ts +8 -8
  47. package/src/state/drawing.ts +360 -360
  48. package/src/state/endpoint.ts +15 -15
  49. package/src/state/layout.ts +1134 -1134
  50. package/src/state/note.ts +59 -59
  51. package/src/state/options.ts +11 -11
  52. package/src/state/parser.ts +224 -224
  53. package/src/state/render-grid.ts +22 -22
  54. package/src/state/routing.ts +1522 -1522
  55. package/src/state/search.ts +103 -103
  56. package/src/state/style.ts +52 -52
  57. package/src/state/types.ts +63 -63
  58. package/src/state/visible-model.ts +77 -77
  59. package/src/test/diagram.ts +32 -32
  60. package/src/test/layout-audit/fixtures.ts +538 -538
  61. package/src/test/layout-audit/harness.ts +522 -522
  62. package/src/timeline/diagram.ts +8 -8
  63. package/src/timeline/drawing.ts +109 -109
  64. package/src/timeline/parser.ts +119 -119
  65. package/src/timeline/render-grid.ts +17 -17
  66. package/src/timeline/style.ts +36 -36
  67. package/src/timeline/types.ts +31 -31
  68. package/tsconfig.json +10 -10
  69. package/src/core/canvas.test.ts +0 -203
  70. package/src/core/color/style.test.ts +0 -24
  71. package/src/core/drawing.test.ts +0 -116
  72. package/src/core/geometry.test.ts +0 -112
  73. package/src/core/spatial.test.ts +0 -87
  74. package/src/core/text.test.ts +0 -33
  75. package/src/flowchart/flowchart.test.ts +0 -2800
  76. package/src/flowchart/labels.test.ts +0 -127
  77. package/src/flowchart/routing.test.ts +0 -368
  78. package/src/gantt/diagram.test.ts +0 -124
  79. package/src/gitgraph/diagram.test.ts +0 -183
  80. package/src/layout-audit.test.ts +0 -92
  81. package/src/palette.test.ts +0 -103
  82. package/src/sequence/diagram.test.ts +0 -757
  83. package/src/sequence/placement.test.ts +0 -219
  84. package/src/state/diagram.test.ts +0 -1386
  85. package/src/state/layout.test.ts +0 -314
  86. package/src/state/routing.test.ts +0 -693
  87. package/src/state/style.test.ts +0 -113
  88. package/src/state/visible-model.test.ts +0 -84
  89. package/src/test/diagnostics.test.ts +0 -129
  90. package/src/test/markdown.test.ts +0 -474
  91. package/src/timeline/diagram.test.ts +0 -188
@@ -1,219 +1,219 @@
1
- import { firstMeaningfulMermaidLine, meaningfulNumberedMermaidLines, stripMermaidQuotes } from "../core/mermaid.js"
2
- import { MermaidSyntaxError } from "../diagnostics.js"
3
- import type { GitGraphBranch, GitGraphCommit, GitGraphCommitType, GitGraphDiagram, GitGraphDirection } from "./types.js"
4
-
5
- const HEADER_RE = /^gitGraph(?:\s+(LR|TB|BT))?\s*:?$/i
6
- const ACCESSIBILITY_RE = /^acc(?:Title|Descr)(?::|\s|$)/i
7
-
8
- export function isMermaidGitGraphDiagram(content: string): boolean {
9
- return HEADER_RE.test(firstMeaningfulMermaidLine(content) ?? "")
10
- }
11
-
12
- export function parseMermaidGitGraphDiagram(content: string): GitGraphDiagram {
13
- const firstLine = firstMeaningfulMermaidLine(content)
14
- if (!HEADER_RE.test(firstLine ?? "")) throw syntaxError(1, firstLine ?? "", "GitGraph header is required")
15
- const branches: GitGraphBranch[] = [{ name: "main", order: 0 }]
16
- const commits: GitGraphCommit[] = []
17
- const heads = new Map<string, string | undefined>([["main", undefined]])
18
- const ids = new Set<string>()
19
- let direction: GitGraphDirection = "LR"
20
- let currentBranch = "main"
21
- let generatedId = 1
22
- let inAccessibilityDescription = false
23
- let headerSeen = false
24
-
25
- for (const source of meaningfulNumberedMermaidLines(content)) {
26
- const line = stripComment(source.text)
27
- if (inAccessibilityDescription) {
28
- if (line === "}") inAccessibilityDescription = false
29
- continue
30
- }
31
- if (/^accDescr\s*\{$/i.test(line)) {
32
- inAccessibilityDescription = true
33
- continue
34
- }
35
- if (!line || ACCESSIBILITY_RE.test(line) || /^title(?:\s|$)/i.test(line)) continue
36
-
37
- const header = line.match(HEADER_RE)
38
- if (header) {
39
- if (headerSeen) throw syntaxError(source.lineNumber, line, "GitGraph header can only appear once")
40
- headerSeen = true
41
- direction = (header[1]?.toUpperCase() as GitGraphDirection | undefined) ?? "LR"
42
- continue
43
- }
44
-
45
- const [command = "", ...rest] = tokenize(line)
46
- const operation = command.toLowerCase()
47
- if (operation === "commit") {
48
- const shorthandMessage = rest[0]?.match(/^(["']).*\1$/) ? stripMermaidQuotes(rest.shift()!) : undefined
49
- const attributes = parseAttributes(rest, source.lineNumber, line, ["id", "msg", "tag", "type"])
50
- const id = single(attributes, "id", source.lineNumber, line) ?? `commit-${generatedId++}`
51
- if (!id) throw syntaxError(source.lineNumber, line, "GitGraph commit id cannot be empty")
52
- if (ids.has(id)) throw syntaxError(source.lineNumber, line, `Duplicate commit id "${id}"`)
53
- const type = parseCommitType(single(attributes, "type", source.lineNumber, line), source.lineNumber, line)
54
- const parent = heads.get(currentBranch)
55
- const message = single(attributes, "msg", source.lineNumber, line) ?? shorthandMessage
56
- const commit: GitGraphCommit = {
57
- id,
58
- ...(message === undefined ? {} : { message }),
59
- tags: attributes.get("tag") ?? [],
60
- type,
61
- branch: currentBranch,
62
- parents: parent === undefined ? [] : [parent],
63
- }
64
- commits.push(commit)
65
- ids.add(id)
66
- heads.set(currentBranch, id)
67
- continue
68
- }
69
-
70
- if (operation === "branch") {
71
- if (rest.length === 0) throw syntaxError(source.lineNumber, line, "GitGraph branch name cannot be empty")
72
- const name = stripMermaidQuotes(rest[0]!)
73
- if (!name) throw syntaxError(source.lineNumber, line, "GitGraph branch name cannot be empty")
74
- if (heads.has(name)) throw syntaxError(source.lineNumber, line, `Duplicate branch "${name}"`)
75
- const attributes = parseAttributes(rest.slice(1), source.lineNumber, line, ["order"])
76
- const orderValue = single(attributes, "order", source.lineNumber, line)
77
- const order = orderValue === undefined ? undefined : Number(orderValue)
78
- if (order !== undefined && (!Number.isInteger(order) || order < 0)) {
79
- throw syntaxError(source.lineNumber, line, "GitGraph branch order must be a non-negative integer")
80
- }
81
- branches.push({ name, ...(order === undefined ? {} : { order }) })
82
- heads.set(name, heads.get(currentBranch))
83
- currentBranch = name
84
- continue
85
- }
86
-
87
- if (operation === "checkout" || operation === "switch") {
88
- if (rest.length !== 1) throw syntaxError(source.lineNumber, line, `GitGraph ${operation} requires one branch`)
89
- const name = stripMermaidQuotes(rest[0]!)
90
- if (!heads.has(name)) throw syntaxError(source.lineNumber, line, `Unknown branch "${name}"`)
91
- currentBranch = name
92
- continue
93
- }
94
-
95
- if (operation === "merge") {
96
- if (rest.length === 0) throw syntaxError(source.lineNumber, line, "GitGraph merge requires a branch")
97
- const branch = stripMermaidQuotes(rest[0]!)
98
- if (!heads.has(branch)) throw syntaxError(source.lineNumber, line, `Unknown branch "${branch}"`)
99
- if (branch === currentBranch)
100
- throw syntaxError(source.lineNumber, line, "GitGraph cannot merge a branch into itself")
101
- const currentHead = heads.get(currentBranch)
102
- const mergedHead = heads.get(branch)
103
- if (currentHead === undefined)
104
- throw syntaxError(source.lineNumber, line, `Branch "${currentBranch}" has no commits`)
105
- if (mergedHead === undefined) throw syntaxError(source.lineNumber, line, `Branch "${branch}" has no commits`)
106
- if (currentHead === mergedHead)
107
- throw syntaxError(source.lineNumber, line, `Branches already share head "${mergedHead}"`)
108
- const attributes = parseAttributes(rest.slice(1), source.lineNumber, line, ["id", "tag", "type"])
109
- const id = single(attributes, "id", source.lineNumber, line) ?? `commit-${generatedId++}`
110
- if (ids.has(id)) throw syntaxError(source.lineNumber, line, `Duplicate commit id "${id}"`)
111
- const commit: GitGraphCommit = {
112
- id,
113
- tags: attributes.get("tag") ?? [],
114
- type: parseCommitType(single(attributes, "type", source.lineNumber, line), source.lineNumber, line),
115
- branch: currentBranch,
116
- parents: [currentHead, mergedHead],
117
- }
118
- commits.push(commit)
119
- ids.add(id)
120
- heads.set(currentBranch, id)
121
- continue
122
- }
123
-
124
- if (operation === "cherry-pick") {
125
- throw syntaxError(source.lineNumber, line, "Cherry-pick is not supported")
126
- }
127
- throw syntaxError(source.lineNumber, line)
128
- }
129
-
130
- const resolvedBranches = branches.map((branch) => {
131
- const head = heads.get(branch.name)
132
- return { ...branch, ...(head === undefined ? {} : { head }) }
133
- })
134
- return { direction, branches: orderBranches(resolvedBranches), commits }
135
- }
136
-
137
- function tokenize(line: string): string[] {
138
- const tokens: string[] = []
139
- let token = ""
140
- let quote: '"' | "'" | undefined
141
- for (const char of line) {
142
- if ((char === '"' || char === "'") && (quote === undefined || quote === char)) {
143
- quote = quote === char ? undefined : char
144
- token += char
145
- continue
146
- }
147
- if (/\s/.test(char) && quote === undefined) {
148
- if (token) tokens.push(token)
149
- token = ""
150
- continue
151
- }
152
- token += char
153
- }
154
- if (quote !== undefined) return [line]
155
- if (token) tokens.push(token)
156
- return tokens
157
- }
158
-
159
- function parseAttributes(
160
- tokens: string[],
161
- lineNumber: number,
162
- line: string,
163
- allowed: readonly string[],
164
- ): Map<string, string[]> {
165
- const result = new Map<string, string[]>()
166
- for (let index = 0; index < tokens.length; index += 1) {
167
- const keyToken = tokens[index]!
168
- const separator = keyToken.indexOf(":")
169
- const key = (separator < 0 ? keyToken : keyToken.slice(0, separator)).toLowerCase()
170
- if (!allowed.includes(key)) throw syntaxError(lineNumber, line, `Unsupported GitGraph attribute "${key}"`)
171
- const inline = separator < 0 ? "" : keyToken.slice(separator + 1)
172
- const valueToken = inline || tokens[++index]
173
- if (valueToken === undefined) throw syntaxError(lineNumber, line, `GitGraph attribute "${key}" requires a value`)
174
- const values = result.get(key) ?? []
175
- values.push(stripMermaidQuotes(valueToken))
176
- result.set(key, values)
177
- }
178
- return result
179
- }
180
-
181
- function single(attributes: Map<string, string[]>, key: string, lineNumber: number, line: string): string | undefined {
182
- const values = attributes.get(key)
183
- if (values && values.length > 1) throw syntaxError(lineNumber, line, `GitGraph attribute "${key}" cannot repeat`)
184
- return values?.[0]
185
- }
186
-
187
- function parseCommitType(value: string | undefined, lineNumber: number, line: string): GitGraphCommitType {
188
- if (value === undefined) return "NORMAL"
189
- const type = value.toUpperCase()
190
- if (type === "NORMAL" || type === "REVERSE" || type === "HIGHLIGHT") return type
191
- throw syntaxError(lineNumber, line, `Unknown GitGraph commit type "${value}"`)
192
- }
193
-
194
- function orderBranches(branches: GitGraphBranch[]): GitGraphBranch[] {
195
- const main = branches[0]!
196
- const rest = branches.slice(1).map((branch, index) => ({ branch, index }))
197
- const unordered = rest.filter(({ branch }) => branch.order === undefined)
198
- const ordered = rest
199
- .filter(({ branch }) => branch.order !== undefined)
200
- .sort((left, right) => left.branch.order! - right.branch.order! || left.index - right.index)
201
- return [main, ...unordered.map(({ branch }) => branch), ...ordered.map(({ branch }) => branch)]
202
- }
203
-
204
- function stripComment(value: string): string {
205
- let quote: '"' | "'" | undefined
206
- for (let index = 0; index < value.length - 1; index += 1) {
207
- const char = value[index]
208
- if ((char === '"' || char === "'") && (quote === undefined || quote === char)) {
209
- quote = quote === char ? undefined : char
210
- continue
211
- }
212
- if (quote === undefined && char === "%" && value[index + 1] === "%") return value.slice(0, index).trim()
213
- }
214
- return value.trim()
215
- }
216
-
217
- function syntaxError(lineNumber: number, sourceLine: string, reason?: string): MermaidSyntaxError {
218
- return new MermaidSyntaxError("gitGraph", lineNumber, sourceLine, reason)
219
- }
1
+ import { firstMeaningfulMermaidLine, meaningfulNumberedMermaidLines, stripMermaidQuotes } from "../core/mermaid.js"
2
+ import { MermaidSyntaxError } from "../diagnostics.js"
3
+ import type { GitGraphBranch, GitGraphCommit, GitGraphCommitType, GitGraphDiagram, GitGraphDirection } from "./types.js"
4
+
5
+ const HEADER_RE = /^gitGraph(?:\s+(LR|TB|BT))?\s*:?$/i
6
+ const ACCESSIBILITY_RE = /^acc(?:Title|Descr)(?::|\s|$)/i
7
+
8
+ export function isMermaidGitGraphDiagram(content: string): boolean {
9
+ return HEADER_RE.test(firstMeaningfulMermaidLine(content) ?? "")
10
+ }
11
+
12
+ export function parseMermaidGitGraphDiagram(content: string): GitGraphDiagram {
13
+ const firstLine = firstMeaningfulMermaidLine(content)
14
+ if (!HEADER_RE.test(firstLine ?? "")) throw syntaxError(1, firstLine ?? "", "GitGraph header is required")
15
+ const branches: GitGraphBranch[] = [{ name: "main", order: 0 }]
16
+ const commits: GitGraphCommit[] = []
17
+ const heads = new Map<string, string | undefined>([["main", undefined]])
18
+ const ids = new Set<string>()
19
+ let direction: GitGraphDirection = "LR"
20
+ let currentBranch = "main"
21
+ let generatedId = 1
22
+ let inAccessibilityDescription = false
23
+ let headerSeen = false
24
+
25
+ for (const source of meaningfulNumberedMermaidLines(content)) {
26
+ const line = stripComment(source.text)
27
+ if (inAccessibilityDescription) {
28
+ if (line === "}") inAccessibilityDescription = false
29
+ continue
30
+ }
31
+ if (/^accDescr\s*\{$/i.test(line)) {
32
+ inAccessibilityDescription = true
33
+ continue
34
+ }
35
+ if (!line || ACCESSIBILITY_RE.test(line) || /^title(?:\s|$)/i.test(line)) continue
36
+
37
+ const header = line.match(HEADER_RE)
38
+ if (header) {
39
+ if (headerSeen) throw syntaxError(source.lineNumber, line, "GitGraph header can only appear once")
40
+ headerSeen = true
41
+ direction = (header[1]?.toUpperCase() as GitGraphDirection | undefined) ?? "LR"
42
+ continue
43
+ }
44
+
45
+ const [command = "", ...rest] = tokenize(line)
46
+ const operation = command.toLowerCase()
47
+ if (operation === "commit") {
48
+ const shorthandMessage = rest[0]?.match(/^(["']).*\1$/) ? stripMermaidQuotes(rest.shift()!) : undefined
49
+ const attributes = parseAttributes(rest, source.lineNumber, line, ["id", "msg", "tag", "type"])
50
+ const id = single(attributes, "id", source.lineNumber, line) ?? `commit-${generatedId++}`
51
+ if (!id) throw syntaxError(source.lineNumber, line, "GitGraph commit id cannot be empty")
52
+ if (ids.has(id)) throw syntaxError(source.lineNumber, line, `Duplicate commit id "${id}"`)
53
+ const type = parseCommitType(single(attributes, "type", source.lineNumber, line), source.lineNumber, line)
54
+ const parent = heads.get(currentBranch)
55
+ const message = single(attributes, "msg", source.lineNumber, line) ?? shorthandMessage
56
+ const commit: GitGraphCommit = {
57
+ id,
58
+ ...(message === undefined ? {} : { message }),
59
+ tags: attributes.get("tag") ?? [],
60
+ type,
61
+ branch: currentBranch,
62
+ parents: parent === undefined ? [] : [parent],
63
+ }
64
+ commits.push(commit)
65
+ ids.add(id)
66
+ heads.set(currentBranch, id)
67
+ continue
68
+ }
69
+
70
+ if (operation === "branch") {
71
+ if (rest.length === 0) throw syntaxError(source.lineNumber, line, "GitGraph branch name cannot be empty")
72
+ const name = stripMermaidQuotes(rest[0]!)
73
+ if (!name) throw syntaxError(source.lineNumber, line, "GitGraph branch name cannot be empty")
74
+ if (heads.has(name)) throw syntaxError(source.lineNumber, line, `Duplicate branch "${name}"`)
75
+ const attributes = parseAttributes(rest.slice(1), source.lineNumber, line, ["order"])
76
+ const orderValue = single(attributes, "order", source.lineNumber, line)
77
+ const order = orderValue === undefined ? undefined : Number(orderValue)
78
+ if (order !== undefined && (!Number.isInteger(order) || order < 0)) {
79
+ throw syntaxError(source.lineNumber, line, "GitGraph branch order must be a non-negative integer")
80
+ }
81
+ branches.push({ name, ...(order === undefined ? {} : { order }) })
82
+ heads.set(name, heads.get(currentBranch))
83
+ currentBranch = name
84
+ continue
85
+ }
86
+
87
+ if (operation === "checkout" || operation === "switch") {
88
+ if (rest.length !== 1) throw syntaxError(source.lineNumber, line, `GitGraph ${operation} requires one branch`)
89
+ const name = stripMermaidQuotes(rest[0]!)
90
+ if (!heads.has(name)) throw syntaxError(source.lineNumber, line, `Unknown branch "${name}"`)
91
+ currentBranch = name
92
+ continue
93
+ }
94
+
95
+ if (operation === "merge") {
96
+ if (rest.length === 0) throw syntaxError(source.lineNumber, line, "GitGraph merge requires a branch")
97
+ const branch = stripMermaidQuotes(rest[0]!)
98
+ if (!heads.has(branch)) throw syntaxError(source.lineNumber, line, `Unknown branch "${branch}"`)
99
+ if (branch === currentBranch)
100
+ throw syntaxError(source.lineNumber, line, "GitGraph cannot merge a branch into itself")
101
+ const currentHead = heads.get(currentBranch)
102
+ const mergedHead = heads.get(branch)
103
+ if (currentHead === undefined)
104
+ throw syntaxError(source.lineNumber, line, `Branch "${currentBranch}" has no commits`)
105
+ if (mergedHead === undefined) throw syntaxError(source.lineNumber, line, `Branch "${branch}" has no commits`)
106
+ if (currentHead === mergedHead)
107
+ throw syntaxError(source.lineNumber, line, `Branches already share head "${mergedHead}"`)
108
+ const attributes = parseAttributes(rest.slice(1), source.lineNumber, line, ["id", "tag", "type"])
109
+ const id = single(attributes, "id", source.lineNumber, line) ?? `commit-${generatedId++}`
110
+ if (ids.has(id)) throw syntaxError(source.lineNumber, line, `Duplicate commit id "${id}"`)
111
+ const commit: GitGraphCommit = {
112
+ id,
113
+ tags: attributes.get("tag") ?? [],
114
+ type: parseCommitType(single(attributes, "type", source.lineNumber, line), source.lineNumber, line),
115
+ branch: currentBranch,
116
+ parents: [currentHead, mergedHead],
117
+ }
118
+ commits.push(commit)
119
+ ids.add(id)
120
+ heads.set(currentBranch, id)
121
+ continue
122
+ }
123
+
124
+ if (operation === "cherry-pick") {
125
+ throw syntaxError(source.lineNumber, line, "Cherry-pick is not supported")
126
+ }
127
+ throw syntaxError(source.lineNumber, line)
128
+ }
129
+
130
+ const resolvedBranches = branches.map((branch) => {
131
+ const head = heads.get(branch.name)
132
+ return { ...branch, ...(head === undefined ? {} : { head }) }
133
+ })
134
+ return { direction, branches: orderBranches(resolvedBranches), commits }
135
+ }
136
+
137
+ function tokenize(line: string): string[] {
138
+ const tokens: string[] = []
139
+ let token = ""
140
+ let quote: '"' | "'" | undefined
141
+ for (const char of line) {
142
+ if ((char === '"' || char === "'") && (quote === undefined || quote === char)) {
143
+ quote = quote === char ? undefined : char
144
+ token += char
145
+ continue
146
+ }
147
+ if (/\s/.test(char) && quote === undefined) {
148
+ if (token) tokens.push(token)
149
+ token = ""
150
+ continue
151
+ }
152
+ token += char
153
+ }
154
+ if (quote !== undefined) return [line]
155
+ if (token) tokens.push(token)
156
+ return tokens
157
+ }
158
+
159
+ function parseAttributes(
160
+ tokens: string[],
161
+ lineNumber: number,
162
+ line: string,
163
+ allowed: readonly string[],
164
+ ): Map<string, string[]> {
165
+ const result = new Map<string, string[]>()
166
+ for (let index = 0; index < tokens.length; index += 1) {
167
+ const keyToken = tokens[index]!
168
+ const separator = keyToken.indexOf(":")
169
+ const key = (separator < 0 ? keyToken : keyToken.slice(0, separator)).toLowerCase()
170
+ if (!allowed.includes(key)) throw syntaxError(lineNumber, line, `Unsupported GitGraph attribute "${key}"`)
171
+ const inline = separator < 0 ? "" : keyToken.slice(separator + 1)
172
+ const valueToken = inline || tokens[++index]
173
+ if (valueToken === undefined) throw syntaxError(lineNumber, line, `GitGraph attribute "${key}" requires a value`)
174
+ const values = result.get(key) ?? []
175
+ values.push(stripMermaidQuotes(valueToken))
176
+ result.set(key, values)
177
+ }
178
+ return result
179
+ }
180
+
181
+ function single(attributes: Map<string, string[]>, key: string, lineNumber: number, line: string): string | undefined {
182
+ const values = attributes.get(key)
183
+ if (values && values.length > 1) throw syntaxError(lineNumber, line, `GitGraph attribute "${key}" cannot repeat`)
184
+ return values?.[0]
185
+ }
186
+
187
+ function parseCommitType(value: string | undefined, lineNumber: number, line: string): GitGraphCommitType {
188
+ if (value === undefined) return "NORMAL"
189
+ const type = value.toUpperCase()
190
+ if (type === "NORMAL" || type === "REVERSE" || type === "HIGHLIGHT") return type
191
+ throw syntaxError(lineNumber, line, `Unknown GitGraph commit type "${value}"`)
192
+ }
193
+
194
+ function orderBranches(branches: GitGraphBranch[]): GitGraphBranch[] {
195
+ const main = branches[0]!
196
+ const rest = branches.slice(1).map((branch, index) => ({ branch, index }))
197
+ const unordered = rest.filter(({ branch }) => branch.order === undefined)
198
+ const ordered = rest
199
+ .filter(({ branch }) => branch.order !== undefined)
200
+ .sort((left, right) => left.branch.order! - right.branch.order! || left.index - right.index)
201
+ return [main, ...unordered.map(({ branch }) => branch), ...ordered.map(({ branch }) => branch)]
202
+ }
203
+
204
+ function stripComment(value: string): string {
205
+ let quote: '"' | "'" | undefined
206
+ for (let index = 0; index < value.length - 1; index += 1) {
207
+ const char = value[index]
208
+ if ((char === '"' || char === "'") && (quote === undefined || quote === char)) {
209
+ quote = quote === char ? undefined : char
210
+ continue
211
+ }
212
+ if (quote === undefined && char === "%" && value[index + 1] === "%") return value.slice(0, index).trim()
213
+ }
214
+ return value.trim()
215
+ }
216
+
217
+ function syntaxError(lineNumber: number, sourceLine: string, reason?: string): MermaidSyntaxError {
218
+ return new MermaidSyntaxError("gitGraph", lineNumber, sourceLine, reason)
219
+ }
@@ -1,17 +1,17 @@
1
- import type { StyledText } from "@opentui/core"
2
- import type { DiagramCanvas } from "../core/canvas.js"
3
- import { renderDiagramGridStyledText } from "../core/render-grid.js"
4
- import type { GitGraphStyleColors } from "./style.js"
5
- import type { GitGraphCellStyle } from "./types.js"
6
-
7
- export type GitGraphGrid = DiagramCanvas<GitGraphCellStyle>
8
-
9
- export function renderGitGraphGridText(grid: GitGraphGrid): string {
10
- return grid.toString({ trimBottom: true })
11
- }
12
-
13
- export function renderGitGraphGridStyledText(grid: GitGraphGrid, colors: GitGraphStyleColors): StyledText {
14
- return renderDiagramGridStyledText(grid, (run) => (run.style ? colors[run.style] : undefined), undefined, {
15
- trimBottom: true,
16
- })
17
- }
1
+ import type { StyledText } from "@opentui/core"
2
+ import type { DiagramCanvas } from "../core/canvas.js"
3
+ import { renderDiagramGridStyledText } from "../core/render-grid.js"
4
+ import type { GitGraphStyleColors } from "./style.js"
5
+ import type { GitGraphCellStyle } from "./types.js"
6
+
7
+ export type GitGraphGrid = DiagramCanvas<GitGraphCellStyle>
8
+
9
+ export function renderGitGraphGridText(grid: GitGraphGrid): string {
10
+ return grid.toString({ trimBottom: true })
11
+ }
12
+
13
+ export function renderGitGraphGridStyledText(grid: GitGraphGrid, colors: GitGraphStyleColors): StyledText {
14
+ return renderDiagramGridStyledText(grid, (run) => (run.style ? colors[run.style] : undefined), undefined, {
15
+ trimBottom: true,
16
+ })
17
+ }
@@ -1,37 +1,37 @@
1
- import { RGBA } from "@opentui/core"
2
- import { rgba, type DiagramRgb } from "../core/color/style.js"
3
- import type { GitGraphCellStyle } from "./types.js"
4
-
5
- const BRANCH_RGB = [
6
- [134, 225, 200],
7
- [230, 177, 126],
8
- [154, 184, 169],
9
- [198, 160, 246],
10
- [126, 189, 230],
11
- [225, 134, 166],
12
- [190, 210, 120],
13
- [180, 180, 210],
14
- ] as const satisfies readonly DiagramRgb[]
15
-
16
- export type GitGraphStyleColors = Required<Record<GitGraphCellStyle, RGBA>>
17
-
18
- export function resolveGitGraphStyleColors(
19
- colors: Partial<Record<"primary" | "secondary" | "muted" | "warning" | "text", RGBA | undefined>> = {},
20
- ): GitGraphStyleColors {
21
- const rail = colors.muted ?? rgba([111, 138, 126])
22
- return {
23
- branch0: rail,
24
- branch1: rail,
25
- branch2: rail,
26
- branch3: rail,
27
- branch4: rail,
28
- branch5: rail,
29
- branch6: rail,
30
- branch7: rail,
31
- commit: colors.primary ?? rgba(BRANCH_RGB[0]),
32
- merge: colors.secondary ?? rgba(BRANCH_RGB[2]),
33
- highlight: colors.warning ?? rgba(BRANCH_RGB[1]),
34
- reverse: colors.warning ?? rgba(BRANCH_RGB[5]),
35
- label: colors.text ?? rgba([228, 239, 232]),
36
- }
37
- }
1
+ import { RGBA } from "@opentui/core"
2
+ import { rgba, type DiagramRgb } from "../core/color/style.js"
3
+ import type { GitGraphCellStyle } from "./types.js"
4
+
5
+ const BRANCH_RGB = [
6
+ [134, 225, 200],
7
+ [230, 177, 126],
8
+ [154, 184, 169],
9
+ [198, 160, 246],
10
+ [126, 189, 230],
11
+ [225, 134, 166],
12
+ [190, 210, 120],
13
+ [180, 180, 210],
14
+ ] as const satisfies readonly DiagramRgb[]
15
+
16
+ export type GitGraphStyleColors = Required<Record<GitGraphCellStyle, RGBA>>
17
+
18
+ export function resolveGitGraphStyleColors(
19
+ colors: Partial<Record<"primary" | "secondary" | "muted" | "warning" | "text", RGBA | undefined>> = {},
20
+ ): GitGraphStyleColors {
21
+ const rail = colors.muted ?? rgba([111, 138, 126])
22
+ return {
23
+ branch0: rail,
24
+ branch1: rail,
25
+ branch2: rail,
26
+ branch3: rail,
27
+ branch4: rail,
28
+ branch5: rail,
29
+ branch6: rail,
30
+ branch7: rail,
31
+ commit: colors.primary ?? rgba(BRANCH_RGB[0]),
32
+ merge: colors.secondary ?? rgba(BRANCH_RGB[2]),
33
+ highlight: colors.warning ?? rgba(BRANCH_RGB[1]),
34
+ reverse: colors.warning ?? rgba(BRANCH_RGB[5]),
35
+ label: colors.text ?? rgba([228, 239, 232]),
36
+ }
37
+ }
@@ -1,36 +1,36 @@
1
- export type GitGraphDirection = "LR" | "TB" | "BT"
2
- export type GitGraphCommitType = "NORMAL" | "REVERSE" | "HIGHLIGHT"
3
-
4
- export interface GitGraphBranch {
5
- name: string
6
- order?: number
7
- head?: string
8
- }
9
-
10
- export interface GitGraphCommit {
11
- id: string
12
- message?: string
13
- tags: string[]
14
- type: GitGraphCommitType
15
- branch: string
16
- parents: string[]
17
- }
18
-
19
- export interface GitGraphDiagram {
20
- direction: GitGraphDirection
21
- branches: GitGraphBranch[]
22
- commits: GitGraphCommit[]
23
- }
24
-
25
- export interface GitGraphDiagramRenderOptions {
26
- /** Parsed for Mermaid compatibility. Git graphs always use a vertical terminal layout. */
27
- direction?: GitGraphDirection
28
- }
29
-
30
- export type GitGraphCellStyle =
31
- | `branch${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7}`
32
- | "commit"
33
- | "merge"
34
- | "highlight"
35
- | "reverse"
36
- | "label"
1
+ export type GitGraphDirection = "LR" | "TB" | "BT"
2
+ export type GitGraphCommitType = "NORMAL" | "REVERSE" | "HIGHLIGHT"
3
+
4
+ export interface GitGraphBranch {
5
+ name: string
6
+ order?: number
7
+ head?: string
8
+ }
9
+
10
+ export interface GitGraphCommit {
11
+ id: string
12
+ message?: string
13
+ tags: string[]
14
+ type: GitGraphCommitType
15
+ branch: string
16
+ parents: string[]
17
+ }
18
+
19
+ export interface GitGraphDiagram {
20
+ direction: GitGraphDirection
21
+ branches: GitGraphBranch[]
22
+ commits: GitGraphCommit[]
23
+ }
24
+
25
+ export interface GitGraphDiagramRenderOptions {
26
+ /** Parsed for Mermaid compatibility. Git graphs always use a vertical terminal layout. */
27
+ direction?: GitGraphDirection
28
+ }
29
+
30
+ export type GitGraphCellStyle =
31
+ | `branch${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7}`
32
+ | "commit"
33
+ | "merge"
34
+ | "highlight"
35
+ | "reverse"
36
+ | "label"