@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
package/src/markdown.ts CHANGED
@@ -1,336 +1,336 @@
1
- import {
2
- TextRenderable,
3
- RenderableEvents,
4
- createMarkdownCodeBlockRenderer,
5
- parseColor,
6
- type ColorInput,
7
- type MarkdownOptions,
8
- type MarkdownCodeBlockRenderer,
9
- type MouseEvent,
10
- type RenderContext,
11
- type RGBA,
12
- type StyledText,
13
- } from "@opentui/core"
14
- import { MermaidSyntaxError } from "./diagnostics.js"
15
- import type { OpenCodeDiagramPalette } from "./palette.js"
16
- import { DiagramCanvasSizeError } from "./core/canvas.js"
17
- import { detectMermaidDiagram } from "./detect.js"
18
- import { drawFlowchartDiagramGrid } from "./flowchart/drawing.js"
19
- import { parseMermaidFlowchartDiagram } from "./flowchart/parser.js"
20
- import { renderGridStyledText, resolveFlowchartStyleColors } from "./flowchart/style.js"
21
- import { drawGanttDiagramGrid } from "./gantt/drawing.js"
22
- import { parseMermaidGanttDiagram } from "./gantt/parser.js"
23
- import { renderGanttGridStyledText } from "./gantt/render-grid.js"
24
- import { resolveGanttStyleColors } from "./gantt/style.js"
25
- import type { GanttDiagramRenderOptions } from "./gantt/types.js"
26
- import { drawGitGraphDiagramGrid } from "./gitgraph/drawing.js"
27
- import { parseMermaidGitGraphDiagram } from "./gitgraph/parser.js"
28
- import { renderGitGraphGridStyledText } from "./gitgraph/render-grid.js"
29
- import { resolveGitGraphStyleColors } from "./gitgraph/style.js"
30
- import { drawSequenceDiagramGrid } from "./sequence/drawing.js"
31
- import { parseMermaidSequenceDiagram } from "./sequence/parser.js"
32
- import { renderSequenceGridStyledText } from "./sequence/render-grid.js"
33
- import { resolveSequenceStyleColors } from "./sequence/style.js"
34
- import { drawStateDiagramGrid } from "./state/drawing.js"
35
- import { parseMermaidStateDiagram } from "./state/parser.js"
36
- import { renderStateGridStyledText } from "./state/render-grid.js"
37
- import { resolveStateStyleColors } from "./state/style.js"
38
- import { drawTimelineDiagramGrid } from "./timeline/drawing.js"
39
- import { parseMermaidTimelineDiagram } from "./timeline/parser.js"
40
- import { renderTimelineGridStyledText } from "./timeline/render-grid.js"
41
- import { resolveTimelineStyleColors } from "./timeline/style.js"
42
-
43
- type DiagramKind = NonNullable<ReturnType<typeof detectMermaidDiagram>>
44
-
45
- interface PreparedDiagram {
46
- readonly kind: DiagramKind
47
- readonly source: string
48
- readonly text: StyledText
49
- readonly height: number
50
- }
51
-
52
- export interface MermaidMarkdownRendererOptions {
53
- /** Use terminal-optimized diagram spacing. Defaults to true. */
54
- compact?: boolean
55
- /** Fold responsive horizontal diagrams that exceed this width. Defaults to 120 columns. */
56
- layoutMaxWidth?: number
57
- /** Gantt-specific terminal rendering options. */
58
- gantt?: Omit<GanttDiagramRenderOptions, "layoutMaxWidth">
59
- colors?: Partial<Record<keyof OpenCodeDiagramPalette, ColorInput>>
60
- }
61
-
62
- function color(value: ColorInput | undefined): RGBA | undefined {
63
- return value === undefined ? undefined : parseColor(value)
64
- }
65
-
66
- class StaticDiagramRenderable extends TextRenderable {
67
- constructor(ctx: RenderContext, prepared: PreparedDiagram) {
68
- super(ctx, {
69
- content: prepared.text,
70
- width: "100%",
71
- height: prepared.height,
72
- wrapMode: "none",
73
- selectable: false,
74
- marginTop: 1,
75
- })
76
- let dragX: number | undefined
77
- this.onMouseDown = (event: MouseEvent) => {
78
- if (event.button !== 0) return
79
- ctx.clearSelection()
80
- dragX = event.x
81
- event.preventDefault()
82
- event.stopPropagation()
83
- }
84
- this.onMouseDrag = (event: MouseEvent) => {
85
- event.preventDefault()
86
- event.stopPropagation()
87
- if (dragX === undefined) return
88
- const dx = event.x - dragX
89
- dragX = event.x
90
- if (dx) this.scrollX -= dx
91
- }
92
- this.onMouseDragEnd = (event: MouseEvent) => {
93
- dragX = undefined
94
- event.preventDefault()
95
- event.stopPropagation()
96
- }
97
- this.onMouseUp = (event: MouseEvent) => {
98
- if (event.button !== 0) return
99
- dragX = undefined
100
- event.preventDefault()
101
- event.stopPropagation()
102
- }
103
- this.onMouseScroll = (event: MouseEvent) => {
104
- const scroll = event.scroll
105
- if (!scroll || (scroll.direction !== "left" && scroll.direction !== "right")) return
106
- event.preventDefault()
107
- event.stopPropagation()
108
- }
109
- }
110
- }
111
-
112
- function prepareDiagram(
113
- kind: DiagramKind,
114
- source: string,
115
- options: MermaidMarkdownRendererOptions,
116
- layoutMaxWidth: number,
117
- ): PreparedDiagram {
118
- const colors = options.colors ?? {}
119
- const compact = options.compact ?? true
120
- switch (kind) {
121
- case "flowchart": {
122
- const grid = drawFlowchartDiagramGrid(parseMermaidFlowchartDiagram(source), {
123
- compact,
124
- layoutMaxWidth,
125
- })
126
- const size = grid.getTextSize({ trimTop: true, trimBottom: true })
127
- return {
128
- kind,
129
- source,
130
- text: renderGridStyledText(
131
- grid,
132
- resolveFlowchartStyleColors({
133
- node: color(colors.boxText ?? colors.primary),
134
- nodeBorder: color(colors.boxBorder ?? colors.muted),
135
- database: color(colors.boxText ?? colors.primary),
136
- databaseBorder: color(colors.boxBorder ?? colors.muted),
137
- edge: color(colors.line ?? colors.secondary),
138
- label: color(colors.text),
139
- group: color(colors.group ?? colors.muted),
140
- groupLabel: color(colors.groupText ?? colors.group ?? colors.muted),
141
- }),
142
- { label: color(colors.labelBackground) },
143
- ),
144
- height: size.height,
145
- }
146
- }
147
- case "gitGraph": {
148
- const grid = drawGitGraphDiagramGrid(parseMermaidGitGraphDiagram(source))
149
- const size = grid.getTextSize({ trimBottom: true })
150
- return {
151
- kind,
152
- source,
153
- text: renderGitGraphGridStyledText(
154
- grid,
155
- resolveGitGraphStyleColors({
156
- primary: color(colors.primary),
157
- secondary: color(colors.secondary),
158
- muted: color(colors.muted),
159
- warning: color(colors.warning),
160
- text: color(colors.text),
161
- }),
162
- ),
163
- height: size.height,
164
- }
165
- }
166
- case "gantt": {
167
- const grid = drawGanttDiagramGrid(parseMermaidGanttDiagram(source), { ...options.gantt, layoutMaxWidth })
168
- const size = grid.getTextSize({ trimBottom: true })
169
- return {
170
- kind,
171
- source,
172
- text: renderGanttGridStyledText(
173
- grid,
174
- resolveGanttStyleColors({
175
- title: color(colors.text),
176
- axis: color(colors.muted),
177
- background: color(colors.background),
178
- section: color(colors.secondary),
179
- task: color(colors.primary),
180
- active: color(colors.primary),
181
- critical: color(colors.warning),
182
- done: color(colors.muted),
183
- }),
184
- ),
185
- height: size.height,
186
- }
187
- }
188
- case "sequence": {
189
- const grid = drawSequenceDiagramGrid(parseMermaidSequenceDiagram(source), { compact })
190
- const size = grid.getTextSize()
191
- return {
192
- kind,
193
- source,
194
- text: renderSequenceGridStyledText(
195
- grid,
196
- resolveSequenceStyleColors({
197
- participant: color(colors.primary),
198
- lifeline: color(colors.muted),
199
- group: color(colors.secondary),
200
- request: color(colors.request ?? colors.primary),
201
- response: color(colors.response ?? colors.primary),
202
- fragment: color(colors.secondary),
203
- fragmentLabelBg: color(colors.background),
204
- note: color(colors.note ?? colors.warning),
205
- noteBg: color(colors.noteBackground ?? colors.background),
206
- }),
207
- ),
208
- height: size.height,
209
- }
210
- }
211
- case "state": {
212
- const grid = drawStateDiagramGrid(parseMermaidStateDiagram(source), { layoutMaxWidth })
213
- const size = grid.getTextSize({ trimTop: true, trimBottom: true })
214
- return {
215
- kind,
216
- source,
217
- text: renderStateGridStyledText(
218
- grid,
219
- resolveStateStyleColors({
220
- state: color(colors.boxText ?? colors.primary),
221
- stateBorder: color(colors.boxBorder ?? colors.primary),
222
- composite: color(colors.group ?? colors.muted),
223
- compositeLabel: color(colors.groupText ?? colors.group ?? colors.muted),
224
- transition: color(colors.line ?? colors.secondary),
225
- label: color(colors.text),
226
- noteBorder: color(colors.noteBorder ?? colors.warning),
227
- noteText: color(colors.noteText ?? colors.warning),
228
- noteConnector: color(colors.noteConnector ?? colors.muted),
229
- start: color(colors.marker ?? colors.muted),
230
- end: color(colors.marker ?? colors.muted),
231
- choice: color(colors.marker ?? colors.secondary),
232
- }),
233
- { label: color(colors.labelBackground) },
234
- ),
235
- height: size.height,
236
- }
237
- }
238
- case "timeline": {
239
- const grid = drawTimelineDiagramGrid(parseMermaidTimelineDiagram(source))
240
- const size = grid.getTextSize({ trimBottom: true })
241
- return {
242
- kind,
243
- source,
244
- text: renderTimelineGridStyledText(
245
- grid,
246
- resolveTimelineStyleColors({
247
- title: color(colors.text),
248
- section: color(colors.secondary),
249
- period: color(colors.warning),
250
- spine: color(colors.muted),
251
- event: color(colors.primary),
252
- }),
253
- ),
254
- height: size.height,
255
- }
256
- }
257
- }
258
- }
259
-
260
- /** Create an OpenTUI Markdown node renderer for fenced Mermaid diagrams. */
261
- export function createMermaidMarkdownRenderer(
262
- ctx: RenderContext,
263
- input: MermaidMarkdownRendererOptions | (() => MermaidMarkdownRendererOptions) = {},
264
- ): NonNullable<MarkdownOptions["renderBlock"]> {
265
- return createMarkdownCodeBlockRenderer({ mermaid: createMermaidCodeBlockRenderer(ctx, input) })!
266
- }
267
-
268
- export function createMermaidCodeBlockRenderer(
269
- ctx: RenderContext,
270
- input: MermaidMarkdownRendererOptions | (() => MermaidMarkdownRendererOptions) = {},
271
- ): MarkdownCodeBlockRenderer {
272
- const lastGood = new Map<string, PreparedDiagram>()
273
- return (token, context) => {
274
- const kind = detectMermaidDiagram(token.text)
275
- if (!kind) return undefined
276
- // OpenTUI's default block ID is the stable identity available for this fence across streaming updates.
277
- const key = context.defaultRender()?.id
278
- const options = typeof input === "function" ? input() : input
279
- const configuredMaxWidth =
280
- options.layoutMaxWidth === undefined ? 120 : Math.max(1, Math.trunc(options.layoutMaxWidth))
281
- const layoutMaxWidth = Math.min(configuredMaxWidth, Math.max(1, Math.trunc(ctx.width)))
282
-
283
- try {
284
- const prepared = prepareDiagram(kind, token.text, options, layoutMaxWidth)
285
- const diagram = new StaticDiagramRenderable(ctx, prepared)
286
- if (key) claimLastGood(key, prepared, diagram, lastGood)
287
- return diagram
288
- } catch (error) {
289
- if (error instanceof MermaidSyntaxError) {
290
- const previous = key ? lastGood.get(key) : undefined
291
- if (previous?.kind === kind) {
292
- const diagram = new StaticDiagramRenderable(ctx, previous)
293
- claimLastGood(key!, previous, diagram, lastGood)
294
- return diagram
295
- }
296
-
297
- const lines = token.text.split("\n")
298
- if (error.lineNumber <= 2 || lines.slice(error.lineNumber).some((line) => line.trim())) return undefined
299
-
300
- try {
301
- const prepared = prepareDiagram(
302
- kind,
303
- lines.slice(0, error.lineNumber - 1).join("\n"),
304
- options,
305
- layoutMaxWidth,
306
- )
307
- if (!prepared.height) return undefined
308
- const diagram = new StaticDiagramRenderable(ctx, prepared)
309
- if (key) claimLastGood(key, prepared, diagram, lastGood)
310
- return diagram
311
- } catch (error) {
312
- if (error instanceof MermaidSyntaxError || error instanceof DiagramCanvasSizeError) return undefined
313
- throw error
314
- }
315
- }
316
- if (error instanceof DiagramCanvasSizeError) return undefined
317
- throw error
318
- }
319
- }
320
- }
321
-
322
- function claimLastGood(
323
- key: string,
324
- value: PreparedDiagram,
325
- owner: StaticDiagramRenderable,
326
- cache: Map<string, PreparedDiagram>,
327
- ): void {
328
- const claim = { ...value }
329
- cache.set(key, claim)
330
- owner.once(RenderableEvents.DESTROYED, () => {
331
- // Reconciliation destroys the old block before synchronously creating its replacement.
332
- queueMicrotask(() => {
333
- if (cache.get(key) === claim) cache.delete(key)
334
- })
335
- })
336
- }
1
+ import {
2
+ TextRenderable,
3
+ RenderableEvents,
4
+ createMarkdownCodeBlockRenderer,
5
+ parseColor,
6
+ type ColorInput,
7
+ type MarkdownOptions,
8
+ type MarkdownCodeBlockRenderer,
9
+ type MouseEvent,
10
+ type RenderContext,
11
+ type RGBA,
12
+ type StyledText,
13
+ } from "@opentui/core"
14
+ import { MermaidSyntaxError } from "./diagnostics.js"
15
+ import type { OpenCodeDiagramPalette } from "./palette.js"
16
+ import { DiagramCanvasSizeError } from "./core/canvas.js"
17
+ import { detectMermaidDiagram } from "./detect.js"
18
+ import { drawFlowchartDiagramGrid } from "./flowchart/drawing.js"
19
+ import { parseMermaidFlowchartDiagram } from "./flowchart/parser.js"
20
+ import { renderGridStyledText, resolveFlowchartStyleColors } from "./flowchart/style.js"
21
+ import { drawGanttDiagramGrid } from "./gantt/drawing.js"
22
+ import { parseMermaidGanttDiagram } from "./gantt/parser.js"
23
+ import { renderGanttGridStyledText } from "./gantt/render-grid.js"
24
+ import { resolveGanttStyleColors } from "./gantt/style.js"
25
+ import type { GanttDiagramRenderOptions } from "./gantt/types.js"
26
+ import { drawGitGraphDiagramGrid } from "./gitgraph/drawing.js"
27
+ import { parseMermaidGitGraphDiagram } from "./gitgraph/parser.js"
28
+ import { renderGitGraphGridStyledText } from "./gitgraph/render-grid.js"
29
+ import { resolveGitGraphStyleColors } from "./gitgraph/style.js"
30
+ import { drawSequenceDiagramGrid } from "./sequence/drawing.js"
31
+ import { parseMermaidSequenceDiagram } from "./sequence/parser.js"
32
+ import { renderSequenceGridStyledText } from "./sequence/render-grid.js"
33
+ import { resolveSequenceStyleColors } from "./sequence/style.js"
34
+ import { drawStateDiagramGrid } from "./state/drawing.js"
35
+ import { parseMermaidStateDiagram } from "./state/parser.js"
36
+ import { renderStateGridStyledText } from "./state/render-grid.js"
37
+ import { resolveStateStyleColors } from "./state/style.js"
38
+ import { drawTimelineDiagramGrid } from "./timeline/drawing.js"
39
+ import { parseMermaidTimelineDiagram } from "./timeline/parser.js"
40
+ import { renderTimelineGridStyledText } from "./timeline/render-grid.js"
41
+ import { resolveTimelineStyleColors } from "./timeline/style.js"
42
+
43
+ type DiagramKind = NonNullable<ReturnType<typeof detectMermaidDiagram>>
44
+
45
+ interface PreparedDiagram {
46
+ readonly kind: DiagramKind
47
+ readonly source: string
48
+ readonly text: StyledText
49
+ readonly height: number
50
+ }
51
+
52
+ export interface MermaidMarkdownRendererOptions {
53
+ /** Use terminal-optimized diagram spacing. Defaults to true. */
54
+ compact?: boolean
55
+ /** Fold responsive horizontal diagrams that exceed this width. Defaults to 120 columns. */
56
+ layoutMaxWidth?: number
57
+ /** Gantt-specific terminal rendering options. */
58
+ gantt?: Omit<GanttDiagramRenderOptions, "layoutMaxWidth">
59
+ colors?: Partial<Record<keyof OpenCodeDiagramPalette, ColorInput>>
60
+ }
61
+
62
+ function color(value: ColorInput | undefined): RGBA | undefined {
63
+ return value === undefined ? undefined : parseColor(value)
64
+ }
65
+
66
+ class StaticDiagramRenderable extends TextRenderable {
67
+ constructor(ctx: RenderContext, prepared: PreparedDiagram) {
68
+ super(ctx, {
69
+ content: prepared.text,
70
+ width: "100%",
71
+ height: prepared.height,
72
+ wrapMode: "none",
73
+ selectable: false,
74
+ marginTop: 1,
75
+ })
76
+ let dragX: number | undefined
77
+ this.onMouseDown = (event: MouseEvent) => {
78
+ if (event.button !== 0) return
79
+ ctx.clearSelection()
80
+ dragX = event.x
81
+ event.preventDefault()
82
+ event.stopPropagation()
83
+ }
84
+ this.onMouseDrag = (event: MouseEvent) => {
85
+ event.preventDefault()
86
+ event.stopPropagation()
87
+ if (dragX === undefined) return
88
+ const dx = event.x - dragX
89
+ dragX = event.x
90
+ if (dx) this.scrollX -= dx
91
+ }
92
+ this.onMouseDragEnd = (event: MouseEvent) => {
93
+ dragX = undefined
94
+ event.preventDefault()
95
+ event.stopPropagation()
96
+ }
97
+ this.onMouseUp = (event: MouseEvent) => {
98
+ if (event.button !== 0) return
99
+ dragX = undefined
100
+ event.preventDefault()
101
+ event.stopPropagation()
102
+ }
103
+ this.onMouseScroll = (event: MouseEvent) => {
104
+ const scroll = event.scroll
105
+ if (!scroll || (scroll.direction !== "left" && scroll.direction !== "right")) return
106
+ event.preventDefault()
107
+ event.stopPropagation()
108
+ }
109
+ }
110
+ }
111
+
112
+ function prepareDiagram(
113
+ kind: DiagramKind,
114
+ source: string,
115
+ options: MermaidMarkdownRendererOptions,
116
+ layoutMaxWidth: number,
117
+ ): PreparedDiagram {
118
+ const colors = options.colors ?? {}
119
+ const compact = options.compact ?? true
120
+ switch (kind) {
121
+ case "flowchart": {
122
+ const grid = drawFlowchartDiagramGrid(parseMermaidFlowchartDiagram(source), {
123
+ compact,
124
+ layoutMaxWidth,
125
+ })
126
+ const size = grid.getTextSize({ trimTop: true, trimBottom: true })
127
+ return {
128
+ kind,
129
+ source,
130
+ text: renderGridStyledText(
131
+ grid,
132
+ resolveFlowchartStyleColors({
133
+ node: color(colors.boxText ?? colors.primary),
134
+ nodeBorder: color(colors.boxBorder ?? colors.muted),
135
+ database: color(colors.boxText ?? colors.primary),
136
+ databaseBorder: color(colors.boxBorder ?? colors.muted),
137
+ edge: color(colors.line ?? colors.secondary),
138
+ label: color(colors.text),
139
+ group: color(colors.group ?? colors.muted),
140
+ groupLabel: color(colors.groupText ?? colors.group ?? colors.muted),
141
+ }),
142
+ { label: color(colors.labelBackground) },
143
+ ),
144
+ height: size.height,
145
+ }
146
+ }
147
+ case "gitGraph": {
148
+ const grid = drawGitGraphDiagramGrid(parseMermaidGitGraphDiagram(source))
149
+ const size = grid.getTextSize({ trimBottom: true })
150
+ return {
151
+ kind,
152
+ source,
153
+ text: renderGitGraphGridStyledText(
154
+ grid,
155
+ resolveGitGraphStyleColors({
156
+ primary: color(colors.primary),
157
+ secondary: color(colors.secondary),
158
+ muted: color(colors.muted),
159
+ warning: color(colors.warning),
160
+ text: color(colors.text),
161
+ }),
162
+ ),
163
+ height: size.height,
164
+ }
165
+ }
166
+ case "gantt": {
167
+ const grid = drawGanttDiagramGrid(parseMermaidGanttDiagram(source), { ...options.gantt, layoutMaxWidth })
168
+ const size = grid.getTextSize({ trimBottom: true })
169
+ return {
170
+ kind,
171
+ source,
172
+ text: renderGanttGridStyledText(
173
+ grid,
174
+ resolveGanttStyleColors({
175
+ title: color(colors.text),
176
+ axis: color(colors.muted),
177
+ background: color(colors.background),
178
+ section: color(colors.secondary),
179
+ task: color(colors.primary),
180
+ active: color(colors.primary),
181
+ critical: color(colors.warning),
182
+ done: color(colors.muted),
183
+ }),
184
+ ),
185
+ height: size.height,
186
+ }
187
+ }
188
+ case "sequence": {
189
+ const grid = drawSequenceDiagramGrid(parseMermaidSequenceDiagram(source), { compact })
190
+ const size = grid.getTextSize()
191
+ return {
192
+ kind,
193
+ source,
194
+ text: renderSequenceGridStyledText(
195
+ grid,
196
+ resolveSequenceStyleColors({
197
+ participant: color(colors.primary),
198
+ lifeline: color(colors.muted),
199
+ group: color(colors.secondary),
200
+ request: color(colors.request ?? colors.primary),
201
+ response: color(colors.response ?? colors.primary),
202
+ fragment: color(colors.secondary),
203
+ fragmentLabelBg: color(colors.background),
204
+ note: color(colors.note ?? colors.warning),
205
+ noteBg: color(colors.noteBackground ?? colors.background),
206
+ }),
207
+ ),
208
+ height: size.height,
209
+ }
210
+ }
211
+ case "state": {
212
+ const grid = drawStateDiagramGrid(parseMermaidStateDiagram(source), { layoutMaxWidth })
213
+ const size = grid.getTextSize({ trimTop: true, trimBottom: true })
214
+ return {
215
+ kind,
216
+ source,
217
+ text: renderStateGridStyledText(
218
+ grid,
219
+ resolveStateStyleColors({
220
+ state: color(colors.boxText ?? colors.primary),
221
+ stateBorder: color(colors.boxBorder ?? colors.primary),
222
+ composite: color(colors.group ?? colors.muted),
223
+ compositeLabel: color(colors.groupText ?? colors.group ?? colors.muted),
224
+ transition: color(colors.line ?? colors.secondary),
225
+ label: color(colors.text),
226
+ noteBorder: color(colors.noteBorder ?? colors.warning),
227
+ noteText: color(colors.noteText ?? colors.warning),
228
+ noteConnector: color(colors.noteConnector ?? colors.muted),
229
+ start: color(colors.marker ?? colors.muted),
230
+ end: color(colors.marker ?? colors.muted),
231
+ choice: color(colors.marker ?? colors.secondary),
232
+ }),
233
+ { label: color(colors.labelBackground) },
234
+ ),
235
+ height: size.height,
236
+ }
237
+ }
238
+ case "timeline": {
239
+ const grid = drawTimelineDiagramGrid(parseMermaidTimelineDiagram(source))
240
+ const size = grid.getTextSize({ trimBottom: true })
241
+ return {
242
+ kind,
243
+ source,
244
+ text: renderTimelineGridStyledText(
245
+ grid,
246
+ resolveTimelineStyleColors({
247
+ title: color(colors.text),
248
+ section: color(colors.secondary),
249
+ period: color(colors.warning),
250
+ spine: color(colors.muted),
251
+ event: color(colors.primary),
252
+ }),
253
+ ),
254
+ height: size.height,
255
+ }
256
+ }
257
+ }
258
+ }
259
+
260
+ /** Create an OpenTUI Markdown node renderer for fenced Mermaid diagrams. */
261
+ export function createMermaidMarkdownRenderer(
262
+ ctx: RenderContext,
263
+ input: MermaidMarkdownRendererOptions | (() => MermaidMarkdownRendererOptions) = {},
264
+ ): NonNullable<MarkdownOptions["renderBlock"]> {
265
+ return createMarkdownCodeBlockRenderer({ mermaid: createMermaidCodeBlockRenderer(ctx, input) })!
266
+ }
267
+
268
+ export function createMermaidCodeBlockRenderer(
269
+ ctx: RenderContext,
270
+ input: MermaidMarkdownRendererOptions | (() => MermaidMarkdownRendererOptions) = {},
271
+ ): MarkdownCodeBlockRenderer {
272
+ const lastGood = new Map<string, PreparedDiagram>()
273
+ return (token, context) => {
274
+ const kind = detectMermaidDiagram(token.text)
275
+ if (!kind) return undefined
276
+ // OpenTUI's default block ID is the stable identity available for this fence across streaming updates.
277
+ const key = context.defaultRender()?.id
278
+ const options = typeof input === "function" ? input() : input
279
+ const configuredMaxWidth =
280
+ options.layoutMaxWidth === undefined ? 120 : Math.max(1, Math.trunc(options.layoutMaxWidth))
281
+ const layoutMaxWidth = Math.min(configuredMaxWidth, Math.max(1, Math.trunc(ctx.width)))
282
+
283
+ try {
284
+ const prepared = prepareDiagram(kind, token.text, options, layoutMaxWidth)
285
+ const diagram = new StaticDiagramRenderable(ctx, prepared)
286
+ if (key) claimLastGood(key, prepared, diagram, lastGood)
287
+ return diagram
288
+ } catch (error) {
289
+ if (error instanceof MermaidSyntaxError) {
290
+ const previous = key ? lastGood.get(key) : undefined
291
+ if (previous?.kind === kind) {
292
+ const diagram = new StaticDiagramRenderable(ctx, previous)
293
+ claimLastGood(key!, previous, diagram, lastGood)
294
+ return diagram
295
+ }
296
+
297
+ const lines = token.text.split("\n")
298
+ if (error.lineNumber <= 2 || lines.slice(error.lineNumber).some((line) => line.trim())) return undefined
299
+
300
+ try {
301
+ const prepared = prepareDiagram(
302
+ kind,
303
+ lines.slice(0, error.lineNumber - 1).join("\n"),
304
+ options,
305
+ layoutMaxWidth,
306
+ )
307
+ if (!prepared.height) return undefined
308
+ const diagram = new StaticDiagramRenderable(ctx, prepared)
309
+ if (key) claimLastGood(key, prepared, diagram, lastGood)
310
+ return diagram
311
+ } catch (error) {
312
+ if (error instanceof MermaidSyntaxError || error instanceof DiagramCanvasSizeError) return undefined
313
+ throw error
314
+ }
315
+ }
316
+ if (error instanceof DiagramCanvasSizeError) return undefined
317
+ throw error
318
+ }
319
+ }
320
+ }
321
+
322
+ function claimLastGood(
323
+ key: string,
324
+ value: PreparedDiagram,
325
+ owner: StaticDiagramRenderable,
326
+ cache: Map<string, PreparedDiagram>,
327
+ ): void {
328
+ const claim = { ...value }
329
+ cache.set(key, claim)
330
+ owner.once(RenderableEvents.DESTROYED, () => {
331
+ // Reconciliation destroys the old block before synchronously creating its replacement.
332
+ queueMicrotask(() => {
333
+ if (cache.get(key) === claim) cache.delete(key)
334
+ })
335
+ })
336
+ }