@zyro-sec/merman 0.0.0-stage → 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 (69) hide show
  1. package/package.json +29 -4
  2. package/script/layout-audit.ts +98 -0
  3. package/src/core/canvas.ts +228 -0
  4. package/src/core/color/style.ts +56 -0
  5. package/src/core/drawing.ts +301 -0
  6. package/src/core/geometry.ts +347 -0
  7. package/src/core/mermaid.ts +38 -0
  8. package/src/core/render-grid.ts +51 -0
  9. package/src/core/spatial.ts +243 -0
  10. package/src/core/text-lines.ts +32 -0
  11. package/src/core/text.ts +44 -0
  12. package/src/detect.ts +17 -0
  13. package/src/diagnostics.ts +16 -0
  14. package/src/flowchart/drawing.ts +340 -0
  15. package/src/flowchart/labels.ts +144 -0
  16. package/src/flowchart/layout.ts +1427 -0
  17. package/src/flowchart/options.ts +12 -0
  18. package/src/flowchart/parser.ts +467 -0
  19. package/src/flowchart/render.ts +10 -0
  20. package/src/flowchart/routing.ts +1542 -0
  21. package/src/flowchart/style.ts +79 -0
  22. package/src/flowchart/types.ts +65 -0
  23. package/src/gantt/diagram.ts +8 -0
  24. package/src/gantt/drawing.ts +191 -0
  25. package/src/gantt/parser.ts +161 -0
  26. package/src/gantt/render-grid.ts +17 -0
  27. package/src/gantt/style.ts +36 -0
  28. package/src/gantt/types.ts +44 -0
  29. package/src/gitgraph/diagram.ts +8 -0
  30. package/src/gitgraph/drawing.ts +234 -0
  31. package/src/gitgraph/parser.ts +219 -0
  32. package/src/gitgraph/render-grid.ts +17 -0
  33. package/src/gitgraph/style.ts +37 -0
  34. package/src/gitgraph/types.ts +36 -0
  35. package/src/markdown.ts +336 -0
  36. package/src/palette.ts +75 -0
  37. package/src/plugin.ts +15 -0
  38. package/src/sequence/diagram.ts +8 -0
  39. package/src/sequence/drawing.ts +287 -0
  40. package/src/sequence/options.ts +8 -0
  41. package/src/sequence/parser.ts +282 -0
  42. package/src/sequence/placement.ts +684 -0
  43. package/src/sequence/render-grid.ts +22 -0
  44. package/src/sequence/style.ts +81 -0
  45. package/src/sequence/types.ts +74 -0
  46. package/src/state/diagram.ts +8 -0
  47. package/src/state/drawing.ts +360 -0
  48. package/src/state/endpoint.ts +15 -0
  49. package/src/state/layout.ts +1134 -0
  50. package/src/state/note.ts +59 -0
  51. package/src/state/options.ts +11 -0
  52. package/src/state/parser.ts +224 -0
  53. package/src/state/render-grid.ts +22 -0
  54. package/src/state/routing.ts +1522 -0
  55. package/src/state/search.ts +103 -0
  56. package/src/state/style.ts +52 -0
  57. package/src/state/types.ts +63 -0
  58. package/src/state/visible-model.ts +77 -0
  59. package/src/test/diagram.ts +32 -0
  60. package/src/test/layout-audit/fixtures.ts +538 -0
  61. package/src/test/layout-audit/harness.ts +522 -0
  62. package/src/timeline/diagram.ts +8 -0
  63. package/src/timeline/drawing.ts +109 -0
  64. package/src/timeline/parser.ts +119 -0
  65. package/src/timeline/render-grid.ts +17 -0
  66. package/src/timeline/style.ts +36 -0
  67. package/src/timeline/types.ts +31 -0
  68. package/tsconfig.json +10 -0
  69. package/README.md +0 -3
@@ -0,0 +1,79 @@
1
+ import { RGBA, type StyledText } from "@opentui/core"
2
+ import type { DiagramCanvas } from "../core/canvas.js"
3
+ import { renderDiagramGridStyledTextByStyle } from "../core/render-grid.js"
4
+ import {
5
+ createColorRampTheme,
6
+ DIAGRAM_FADE_STEPS,
7
+ numberedStyleKeys,
8
+ rgba,
9
+ type DiagramFadeStep,
10
+ type DiagramRgb,
11
+ } from "../core/color/style.js"
12
+
13
+ export type FlowchartBaseCellStyle =
14
+ | "node"
15
+ | "nodeBorder"
16
+ | "database"
17
+ | "databaseBorder"
18
+ | "edge"
19
+ | "label"
20
+ | "group"
21
+ | "groupLabel"
22
+ export type FlowchartNodeEdgeFadeStyle = `nodeEdgeFade${DiagramFadeStep}`
23
+ export type FlowchartDatabaseEdgeFadeStyle = `databaseEdgeFade${DiagramFadeStep}`
24
+ export type FlowchartEdgeFadeStyle = FlowchartNodeEdgeFadeStyle | FlowchartDatabaseEdgeFadeStyle
25
+ export type FlowchartCellStyle = FlowchartBaseCellStyle | FlowchartEdgeFadeStyle
26
+ export interface FlowchartCellMetadata {
27
+ attributes?: number
28
+ }
29
+ export type FlowchartGrid = DiagramCanvas<FlowchartCellStyle, FlowchartCellMetadata>
30
+ export type FlowchartStyleColors = Required<Record<FlowchartCellStyle, RGBA>>
31
+ export type FlowchartStyleBackgroundColors = Partial<Record<FlowchartCellStyle, RGBA>>
32
+ export const DEFAULT_THEME_RGB = {
33
+ node: [228, 239, 232],
34
+ nodeBorder: [141, 163, 151],
35
+ database: [228, 239, 232],
36
+ databaseBorder: [141, 163, 151],
37
+ edge: [134, 225, 200],
38
+ label: [134, 225, 200],
39
+ group: [76, 99, 89],
40
+ groupLabel: [76, 99, 89],
41
+ } as const satisfies Record<FlowchartBaseCellStyle, DiagramRgb>
42
+
43
+ export const NODE_EDGE_FADE_STYLES = numberedStyleKeys("nodeEdgeFade", DIAGRAM_FADE_STEPS)
44
+ export const DATABASE_EDGE_FADE_STYLES = numberedStyleKeys("databaseEdgeFade", DIAGRAM_FADE_STEPS)
45
+
46
+ export function resolveFlowchartStyleColors(
47
+ colors: Partial<Record<FlowchartCellStyle, RGBA | undefined>> = {},
48
+ ): FlowchartStyleColors {
49
+ const node = colors.node ?? rgba(DEFAULT_THEME_RGB.node)
50
+ const nodeBorder = colors.nodeBorder ?? rgba(DEFAULT_THEME_RGB.nodeBorder)
51
+ const database = colors.database ?? rgba(DEFAULT_THEME_RGB.database)
52
+ const databaseBorder = colors.databaseBorder ?? rgba(DEFAULT_THEME_RGB.databaseBorder)
53
+ const edge = colors.edge ?? rgba(DEFAULT_THEME_RGB.edge)
54
+ return {
55
+ node,
56
+ nodeBorder,
57
+ database,
58
+ databaseBorder,
59
+ edge,
60
+ label: colors.label ?? rgba(DEFAULT_THEME_RGB.label),
61
+ group: colors.group ?? rgba(DEFAULT_THEME_RGB.group),
62
+ groupLabel: colors.groupLabel ?? colors.group ?? rgba(DEFAULT_THEME_RGB.groupLabel),
63
+ ...createColorRampTheme(NODE_EDGE_FADE_STYLES, nodeBorder, edge),
64
+ ...createColorRampTheme(DATABASE_EDGE_FADE_STYLES, databaseBorder, edge),
65
+ }
66
+ }
67
+
68
+ export function renderGridStyledText(
69
+ grid: FlowchartGrid,
70
+ colors: FlowchartStyleColors,
71
+ backgrounds?: FlowchartStyleBackgroundColors,
72
+ ): StyledText {
73
+ return renderDiagramGridStyledTextByStyle(grid, colors, backgrounds, {
74
+ key: (cell) => [cell.style, cell.attributes],
75
+ attributes: (run) => run.cell.attributes,
76
+ trimTop: true,
77
+ trimBottom: true,
78
+ })
79
+ }
@@ -0,0 +1,65 @@
1
+ import type { DiagramAxis, DiagramBounds, DiagramDirection, DiagramPoint } from "../core/geometry.js"
2
+
3
+ export type FlowchartDirection = "TB" | "TD" | "BT" | "LR" | "RL"
4
+ export type FlowchartNodeShape = "box" | "rounded" | "database" | "decision" | "subroutine"
5
+ export type FlowchartEdgeStyle = "thick" | "dashed"
6
+
7
+ export interface FlowchartNode {
8
+ id: string
9
+ label: string
10
+ shape: FlowchartNodeShape
11
+ }
12
+
13
+ export interface FlowchartEdge {
14
+ from: string
15
+ to: string
16
+ label: string
17
+ style?: FlowchartEdgeStyle
18
+ arrowhead?: false
19
+ sourceArrowhead?: true
20
+ orderOnly?: boolean
21
+ }
22
+
23
+ export interface FlowchartSubgraph {
24
+ id: string
25
+ label: string
26
+ nodeIds: string[]
27
+ parentId?: string
28
+ direction?: FlowchartDirection
29
+ }
30
+
31
+ export interface FlowchartDiagram {
32
+ direction: FlowchartDirection
33
+ nodes: FlowchartNode[]
34
+ edges: FlowchartEdge[]
35
+ subgraphs?: FlowchartSubgraph[]
36
+ }
37
+
38
+ export interface FlowchartNodeSize {
39
+ width: number
40
+ height: number
41
+ lines: string[]
42
+ }
43
+
44
+ export interface FlowchartNodeBounds extends FlowchartNodeSize, DiagramBounds {
45
+ id: string
46
+ }
47
+
48
+ export interface FlowchartSubgraphBounds extends DiagramBounds {
49
+ id: string
50
+ label: string
51
+ labelSide: "top" | "bottom"
52
+ }
53
+
54
+ export type FlowchartEndpointBounds = FlowchartNodeBounds | FlowchartSubgraphBounds
55
+
56
+ export type FlowchartPoint = DiagramPoint
57
+
58
+ export interface FlowchartEdgeRoute {
59
+ edge: FlowchartEdge
60
+ points: FlowchartPoint[]
61
+ labelAxis?: DiagramAxis
62
+ labelPoint?: FlowchartPoint
63
+ }
64
+
65
+ export type FlowchartEdgeDirection = DiagramDirection
@@ -0,0 +1,8 @@
1
+ import { drawGanttDiagramGrid } from "./drawing.js"
2
+ import { parseMermaidGanttDiagram } from "./parser.js"
3
+ import { renderGanttGridText } from "./render-grid.js"
4
+ import type { GanttDiagramRenderOptions } from "./types.js"
5
+
6
+ export function renderGanttDiagram(content: string, options: GanttDiagramRenderOptions = {}): string {
7
+ return renderGanttGridText(drawGanttDiagramGrid(parseMermaidGanttDiagram(content), options))
8
+ }
@@ -0,0 +1,191 @@
1
+ import { DiagramCanvas } from "../core/canvas.js"
2
+ import { diagramTextWidth } from "../core/text.js"
3
+ import type { GanttGrid } from "./render-grid.js"
4
+ import type {
5
+ GanttDiagram,
6
+ GanttDiagramRenderOptions,
7
+ GanttLabelLayout,
8
+ GanttLineStyle,
9
+ GanttRenderStyle,
10
+ GanttTask,
11
+ } from "./types.js"
12
+
13
+ const LABEL_GAP = 2
14
+ const MIN_CHART_WIDTH = 24
15
+ const MAX_CHART_WIDTH = 64
16
+
17
+ export function drawGanttDiagramGrid(diagram: GanttDiagram, options: GanttDiagramRenderOptions = {}): GanttGrid {
18
+ if (diagram.entries.length === 0) return new DiagramCanvas(0, 0)
19
+ const labels = diagram.entries.map((_, index) => entryLabel(diagram, index, options.labels ?? "left"))
20
+ const labelWidth = Math.max(...labels.map(diagramTextWidth))
21
+ const entryRows: number[] = []
22
+ let bodyHeight = 0
23
+ diagram.entries.forEach((entry, index) => {
24
+ if ((options.sections ?? "spaced") === "spaced" && entry.type === "section" && index > 0) bodyHeight += 1
25
+ entryRows.push(bodyHeight)
26
+ bodyHeight += 1
27
+ })
28
+ if (diagram.tasks.length === 0) {
29
+ const grid: GanttGrid = new DiagramCanvas(labelWidth, bodyHeight)
30
+ diagram.entries.forEach((entry, index) => {
31
+ if (entry.type === "section") grid.setText(0, entryRows[index]!, entry.section.label, "section")
32
+ })
33
+ return grid
34
+ }
35
+ const available = (options.layoutMaxWidth ?? 120) - labelWidth - LABEL_GAP
36
+ const chartWidth = Math.max(MIN_CHART_WIDTH, Math.min(MAX_CHART_WIDTH, available))
37
+ const starts = diagram.tasks.map((task) => task.start)
38
+ const ends = diagram.tasks.map((task) => task.end)
39
+ const minimum = Math.min(...starts)
40
+ const maximum = Math.max(...ends)
41
+ const span = Math.max(1, maximum - minimum)
42
+ const titleHeight = diagram.title ? 2 : 0
43
+ const axisHeight = 2
44
+ const grid: GanttGrid = new DiagramCanvas(labelWidth + LABEL_GAP + chartWidth, titleHeight + axisHeight + bodyHeight)
45
+ const chartX = labelWidth + LABEL_GAP
46
+
47
+ if (diagram.title)
48
+ grid.setText(
49
+ Math.max(0, chartX + Math.floor((chartWidth - diagramTextWidth(diagram.title)) / 2)),
50
+ 0,
51
+ diagram.title,
52
+ "title",
53
+ )
54
+ drawAxis(grid, chartX, titleHeight, chartWidth, minimum, span, diagram.axisFormat)
55
+
56
+ diagram.entries.forEach((entry, index) => {
57
+ const y = titleHeight + axisHeight + entryRows[index]!
58
+ if (entry.type === "section") {
59
+ grid.setText(0, y, labels[index]!, "section")
60
+ return
61
+ }
62
+ const label = labels[index]!
63
+ const labelX = options.labels === "right" ? labelWidth - diagramTextWidth(label) : 0
64
+ grid.setText(labelX, y, label, entry.task.state)
65
+ drawTask(grid, entry.task, chartX, y, chartWidth, minimum, span, options)
66
+ })
67
+ return grid
68
+ }
69
+
70
+ function drawAxis(
71
+ grid: GanttGrid,
72
+ x: number,
73
+ y: number,
74
+ width: number,
75
+ minimum: number,
76
+ span: number,
77
+ format: string,
78
+ ): void {
79
+ for (let offset = 0; offset < width; offset++) grid.setCell(x + offset, y + 1, "─", "axis")
80
+ const step = tickStep(span)
81
+ const ticks: number[] = []
82
+ for (let value = Math.ceil(minimum / step) * step; value <= minimum + span; value += step) ticks.push(value)
83
+ if (ticks.length === 0) ticks.push(minimum, minimum + span)
84
+ for (const value of ticks) {
85
+ const offset = Math.round(((value - minimum) / span) * (width - 1))
86
+ const label = formatTime(value, format)
87
+ const labelX = Math.max(
88
+ x,
89
+ Math.min(x + width - diagramTextWidth(label), x + offset - Math.floor(diagramTextWidth(label) / 2)),
90
+ )
91
+ grid.setText(labelX, y, label, "axis")
92
+ grid.setCell(x + offset, y + 1, "┬", "axis")
93
+ }
94
+ }
95
+
96
+ function tickStep(span: number): number {
97
+ const steps = [
98
+ 1_000, 5_000, 10_000, 30_000, 60_000, 300_000, 900_000, 3_600_000, 21_600_000, 43_200_000, 86_400_000, 172_800_000,
99
+ 604_800_000, 2_592_000_000, 31_536_000_000,
100
+ ]
101
+ return steps.find((step) => step >= span / 4) ?? steps.at(-1)!
102
+ }
103
+
104
+ function drawTask(
105
+ grid: GanttGrid,
106
+ task: GanttTask,
107
+ x: number,
108
+ y: number,
109
+ width: number,
110
+ minimum: number,
111
+ span: number,
112
+ options: GanttDiagramRenderOptions,
113
+ ): void {
114
+ const style: GanttRenderStyle = options.style ?? "track"
115
+ const line = lineGlyph(options.line ?? "thin")
116
+ const start = Math.round(((task.start - minimum) / span) * (width - 1))
117
+ const end = Math.round(((task.end - minimum) / span) * (width - 1))
118
+ if (task.state === "milestone" || start === end) {
119
+ grid.setCell(x + start, y, "◆", task.state)
120
+ return
121
+ }
122
+ if (style === "track") {
123
+ for (let offset = 0; offset < width; offset++) {
124
+ grid.setCell(x + offset, y, (options.track ?? "line") === "line" ? line : "·", trackCellStyle(options))
125
+ }
126
+ }
127
+ const glyph = style === "block" ? "█" : line
128
+ for (let offset = start; offset <= end; offset++) grid.setCell(x + offset, y, glyph, task.state)
129
+ if (style === "block") return
130
+ if (style === "capsule" || style === "track") {
131
+ if (style === "track") {
132
+ if (options.endpoints === "points") {
133
+ grid.setCell(x + start, y, "●", trackCellStyle(options))
134
+ grid.setCell(x + end, y, "●", trackCellStyle(options))
135
+ }
136
+ return
137
+ }
138
+ const caps = capGlyphs(options.line ?? "thin")
139
+ grid.setCell(x + start, y, caps.start, task.state)
140
+ grid.setCell(x + end, y, caps.end, task.state)
141
+ return
142
+ }
143
+ if (style === "points") {
144
+ grid.setCell(x + start, y, "●", task.state)
145
+ grid.setCell(x + end, y, "●", task.state)
146
+ return
147
+ }
148
+ grid.setCell(x + start, y, "┣", task.state)
149
+ grid.setCell(x + end, y, "┫", task.state)
150
+ }
151
+
152
+ function entryLabel(diagram: GanttDiagram, index: number, layout: GanttLabelLayout): string {
153
+ const entry = diagram.entries[index]!
154
+ if (entry.type === "section" || layout !== "tree") {
155
+ return entry.type === "section" ? entry.section.label : entry.task.label
156
+ }
157
+ const last = diagram.entries[index + 1]?.type !== "task"
158
+ return ` ${last ? "└" : "├"}─ ${entry.task.label}`
159
+ }
160
+
161
+ function lineGlyph(line: GanttLineStyle): string {
162
+ if (line === "thin") return "─"
163
+ if (line === "double") return "═"
164
+ if (line === "dashed") return "╌"
165
+ return "━"
166
+ }
167
+
168
+ function capGlyphs(line: GanttLineStyle): { start: string; end: string } {
169
+ if (line === "thin" || line === "dashed") return { start: "╶", end: "╴" }
170
+ if (line === "double") return { start: "╞", end: "╡" }
171
+ return { start: "╺", end: "╸" }
172
+ }
173
+
174
+ function trackCellStyle(options: GanttDiagramRenderOptions): "trackMedium" | "trackDim" | "trackFaint" {
175
+ if (options.trackTone === "medium") return "trackMedium"
176
+ if (options.trackTone === "dim") return "trackDim"
177
+ return "trackFaint"
178
+ }
179
+
180
+ function formatTime(value: number, format: string): string {
181
+ const date = new Date(value)
182
+ const parts: Record<string, string> = {
183
+ "%Y": String(date.getUTCFullYear()),
184
+ "%m": String(date.getUTCMonth() + 1).padStart(2, "0"),
185
+ "%d": String(date.getUTCDate()).padStart(2, "0"),
186
+ "%H": String(date.getUTCHours()).padStart(2, "0"),
187
+ "%M": String(date.getUTCMinutes()).padStart(2, "0"),
188
+ "%S": String(date.getUTCSeconds()).padStart(2, "0"),
189
+ }
190
+ return Object.entries(parts).reduce((result, [token, replacement]) => result.replaceAll(token, replacement), format)
191
+ }
@@ -0,0 +1,161 @@
1
+ import { firstMeaningfulMermaidLine, meaningfulNumberedMermaidLines, stripMermaidQuotes } from "../core/mermaid.js"
2
+ import { MermaidSyntaxError } from "../diagnostics.js"
3
+ import type { GanttDiagram, GanttEntry, GanttSection, GanttTask, GanttTaskState } from "./types.js"
4
+
5
+ const HEADER_RE = /^gantt$/i
6
+ const DIRECTIVE_RE = /^(title|dateFormat|axisFormat|tickInterval|excludes|todayMarker)\s+(.+)$/i
7
+ const SECTION_RE = /^section(?:\s+(.+))?$/i
8
+ const ACCESSIBILITY_RE = /^acc(?:Title|Descr)(?::|\s|$)/i
9
+ const TASK_STATES = new Set(["active", "done", "crit", "milestone", "vert"])
10
+
11
+ export function isMermaidGanttDiagram(content: string): boolean {
12
+ return HEADER_RE.test(firstMeaningfulMermaidLine(content) ?? "")
13
+ }
14
+
15
+ export function parseMermaidGanttDiagram(content: string): GanttDiagram {
16
+ const tasks: GanttTask[] = []
17
+ const entries: GanttEntry[] = []
18
+ const tasksById = new Map<string, GanttTask>()
19
+ let title: string | undefined
20
+ let dateFormat = "YYYY-MM-DD"
21
+ let axisFormat = "%Y-%m-%d"
22
+ let section: GanttSection | undefined
23
+ let headerSeen = false
24
+ let inAccessibilityDescription = false
25
+
26
+ for (const source of meaningfulNumberedMermaidLines(content)) {
27
+ const line = stripComment(source.text)
28
+ if (inAccessibilityDescription) {
29
+ if (line === "}") inAccessibilityDescription = false
30
+ continue
31
+ }
32
+ if (/^accDescr\s*\{$/i.test(line)) {
33
+ inAccessibilityDescription = true
34
+ continue
35
+ }
36
+ if (ACCESSIBILITY_RE.test(line)) continue
37
+ if (!line) continue
38
+ if (HEADER_RE.test(line)) {
39
+ if (headerSeen) throw syntaxError(source.lineNumber, line, "Gantt header can only appear once")
40
+ headerSeen = true
41
+ continue
42
+ }
43
+ if (!headerSeen) throw syntaxError(source.lineNumber, line, "Gantt header is required")
44
+
45
+ const sectionMatch = line.match(SECTION_RE)
46
+ if (sectionMatch) {
47
+ if (!sectionMatch[1]) throw syntaxError(source.lineNumber, line, "Gantt section cannot be empty")
48
+ section = { label: stripMermaidQuotes(sectionMatch[1]) }
49
+ entries.push({ type: "section", section })
50
+ continue
51
+ }
52
+
53
+ const directive = line.match(DIRECTIVE_RE)
54
+ if (directive) {
55
+ const name = directive[1]!.toLowerCase()
56
+ const value = directive[2]!.trim()
57
+ if (name === "title") title = stripMermaidQuotes(value)
58
+ if (name === "dateformat") dateFormat = value
59
+ if (name === "axisformat") axisFormat = value
60
+ if (!["title", "dateformat", "axisformat"].includes(name)) {
61
+ throw syntaxError(source.lineNumber, line, `${directive[1]} is not supported`)
62
+ }
63
+ continue
64
+ }
65
+
66
+ const separator = line.indexOf(":")
67
+ if (separator < 1) throw syntaxError(source.lineNumber, line)
68
+ const label = stripMermaidQuotes(line.slice(0, separator))
69
+ const fields = line
70
+ .slice(separator + 1)
71
+ .split(",")
72
+ .map((field) => field.trim())
73
+ .filter(Boolean)
74
+ const flags = new Set<string>()
75
+ while (fields[0] && TASK_STATES.has(fields[0].toLowerCase())) flags.add(fields.shift()!.toLowerCase())
76
+ if (fields.length < 2 || fields.length > 3) {
77
+ throw syntaxError(source.lineNumber, line, "Gantt tasks require a start and end or duration")
78
+ }
79
+ const id = fields.length === 3 ? fields.shift() : undefined
80
+ if (id && tasksById.has(id)) throw syntaxError(source.lineNumber, line, `Duplicate task id "${id}"`)
81
+ const start = parseStart(fields[0]!, dateFormat, tasksById, source.lineNumber, line)
82
+ const end = parseEnd(fields[1]!, start, dateFormat, source.lineNumber, line)
83
+ if (end < start) throw syntaxError(source.lineNumber, line, "Gantt task cannot end before it starts")
84
+ const task: GanttTask = {
85
+ label,
86
+ ...(id ? { id } : {}),
87
+ ...(section ? { section } : {}),
88
+ start,
89
+ end,
90
+ state: taskState(flags),
91
+ }
92
+ tasks.push(task)
93
+ entries.push({ type: "task", task })
94
+ if (id) tasksById.set(id, task)
95
+ }
96
+
97
+ if (!headerSeen) throw new MermaidSyntaxError("gantt", 1, "", "Gantt header is required")
98
+ return { ...(title === undefined ? {} : { title }), dateFormat, axisFormat, tasks, entries }
99
+ }
100
+
101
+ function parseStart(
102
+ value: string,
103
+ format: string,
104
+ tasksById: Map<string, GanttTask>,
105
+ lineNumber: number,
106
+ sourceLine: string,
107
+ ): number {
108
+ if (!/^after\s+/i.test(value)) return parseDate(value, format, lineNumber, sourceLine)
109
+ const dependencies = value
110
+ .replace(/^after\s+/i, "")
111
+ .trim()
112
+ .split(/\s+/)
113
+ const tasks = dependencies.map((id) => tasksById.get(id))
114
+ const missing = dependencies.find((_, index) => !tasks[index])
115
+ if (missing) throw syntaxError(lineNumber, sourceLine, `Unknown Gantt task id "${missing}"`)
116
+ return Math.max(...tasks.map((task) => task!.end))
117
+ }
118
+
119
+ function parseEnd(value: string, start: number, format: string, lineNumber: number, sourceLine: string): number {
120
+ const duration = value.match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h|d|w)$/i)
121
+ if (!duration) return parseDate(value, format, lineNumber, sourceLine)
122
+ const units = { ms: 1, s: 1_000, m: 60_000, h: 3_600_000, d: 86_400_000, w: 604_800_000 }
123
+ return start + Number(duration[1]) * units[duration[2]!.toLowerCase() as keyof typeof units]
124
+ }
125
+
126
+ function parseDate(value: string, format: string, lineNumber: number, sourceLine: string): number {
127
+ const numeric = Number(value)
128
+ if (format === "s" && Number.isFinite(numeric)) return numeric * 1_000
129
+ if (format === "X" && Number.isFinite(numeric)) return numeric * 1_000
130
+ if (format === "x" && Number.isFinite(numeric)) return numeric
131
+
132
+ const calendar = value.match(/^(\d{4})-(\d{2})-(\d{2})(?:[ T](\d{2}):(\d{2})(?::(\d{2}))?)?$/)
133
+ if (calendar && ["YYYY-MM-DD", "YYYY-MM-DD HH:mm", "YYYY-MM-DD HH:mm:ss"].includes(format)) {
134
+ return Date.UTC(
135
+ Number(calendar[1]),
136
+ Number(calendar[2]) - 1,
137
+ Number(calendar[3]),
138
+ Number(calendar[4] ?? 0),
139
+ Number(calendar[5] ?? 0),
140
+ Number(calendar[6] ?? 0),
141
+ )
142
+ }
143
+ throw syntaxError(lineNumber, sourceLine, `Unsupported date "${value}" for dateFormat ${format}`)
144
+ }
145
+
146
+ function taskState(flags: Set<string>): GanttTaskState {
147
+ if (flags.has("milestone")) return "milestone"
148
+ if (flags.has("crit")) return "critical"
149
+ if (flags.has("done")) return "done"
150
+ if (flags.has("active")) return "active"
151
+ return "task"
152
+ }
153
+
154
+ function stripComment(value: string): string {
155
+ const comment = value.indexOf("%%")
156
+ return (comment < 0 ? value : value.slice(0, comment)).trim()
157
+ }
158
+
159
+ function syntaxError(lineNumber: number, sourceLine: string, reason?: string): MermaidSyntaxError {
160
+ return new MermaidSyntaxError("gantt", lineNumber, sourceLine, reason)
161
+ }
@@ -0,0 +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 { GanttStyleColors } from "./style.js"
5
+ import type { GanttCellStyle } from "./types.js"
6
+
7
+ export type GanttGrid = DiagramCanvas<GanttCellStyle>
8
+
9
+ export function renderGanttGridText(grid: GanttGrid): string {
10
+ return grid.toString({ trimBottom: true })
11
+ }
12
+
13
+ export function renderGanttGridStyledText(grid: GanttGrid, colors: GanttStyleColors): StyledText {
14
+ return renderDiagramGridStyledText(grid, (run) => (run.style ? colors[run.style] : undefined), undefined, {
15
+ trimBottom: true,
16
+ })
17
+ }
@@ -0,0 +1,36 @@
1
+ import { RGBA } from "@opentui/core"
2
+ import { blendColor, rgba, type DiagramRgb } from "../core/color/style.js"
3
+ import type { GanttBaseCellStyle, GanttCellStyle } from "./types.js"
4
+
5
+ const DEFAULT_THEME_RGB = {
6
+ title: [228, 239, 232],
7
+ axis: [111, 138, 126],
8
+ section: [154, 184, 169],
9
+ task: [134, 225, 200],
10
+ active: [134, 225, 200],
11
+ critical: [230, 177, 126],
12
+ done: [111, 138, 126],
13
+ milestone: [198, 160, 246],
14
+ } as const satisfies Record<GanttBaseCellStyle, DiagramRgb>
15
+
16
+ export type GanttStyleColors = Required<Record<GanttCellStyle, RGBA>>
17
+
18
+ export function resolveGanttStyleColors(
19
+ colors: Partial<Record<GanttBaseCellStyle | "background", RGBA | undefined>> = {},
20
+ ): GanttStyleColors {
21
+ const axis = colors.axis ?? rgba(DEFAULT_THEME_RGB.axis)
22
+ const background = colors.background ?? rgba([13, 17, 23])
23
+ return {
24
+ title: colors.title ?? rgba(DEFAULT_THEME_RGB.title),
25
+ axis,
26
+ section: colors.section ?? rgba(DEFAULT_THEME_RGB.section),
27
+ task: colors.task ?? rgba(DEFAULT_THEME_RGB.task),
28
+ active: colors.active ?? rgba(DEFAULT_THEME_RGB.active),
29
+ critical: colors.critical ?? rgba(DEFAULT_THEME_RGB.critical),
30
+ done: colors.done ?? rgba(DEFAULT_THEME_RGB.done),
31
+ milestone: colors.milestone ?? rgba(DEFAULT_THEME_RGB.milestone),
32
+ trackMedium: blendColor(axis, background, 0.35),
33
+ trackDim: blendColor(axis, background, 0.55),
34
+ trackFaint: blendColor(axis, background, 0.72),
35
+ }
36
+ }
@@ -0,0 +1,44 @@
1
+ export type GanttTaskState = "task" | "active" | "critical" | "done" | "milestone"
2
+
3
+ export interface GanttSection {
4
+ label: string
5
+ }
6
+
7
+ export interface GanttTask {
8
+ label: string
9
+ id?: string
10
+ section?: GanttSection
11
+ start: number
12
+ end: number
13
+ state: GanttTaskState
14
+ }
15
+
16
+ export type GanttEntry = { type: "section"; section: GanttSection } | { type: "task"; task: GanttTask }
17
+
18
+ export interface GanttDiagram {
19
+ title?: string
20
+ dateFormat: string
21
+ axisFormat: string
22
+ tasks: GanttTask[]
23
+ entries: GanttEntry[]
24
+ }
25
+
26
+ export interface GanttDiagramRenderOptions {
27
+ layoutMaxWidth?: number
28
+ style?: GanttRenderStyle
29
+ track?: "dots" | "line"
30
+ endpoints?: "plain" | "points"
31
+ line?: GanttLineStyle
32
+ labels?: GanttLabelLayout
33
+ sections?: "compact" | "spaced"
34
+ trackTone?: GanttTrackTone
35
+ }
36
+
37
+ export type GanttRenderStyle = "rail" | "block" | "capsule" | "points" | "track"
38
+ export type GanttLineStyle = "heavy" | "thin" | "double" | "dashed"
39
+ export type GanttLabelLayout = "right" | "left" | "tree"
40
+ export type GanttTrackTone = "medium" | "dim" | "faint"
41
+
42
+ export type GanttBaseCellStyle = "title" | "axis" | "section" | GanttTaskState
43
+ export type GanttTrackCellStyle = "trackMedium" | "trackDim" | "trackFaint"
44
+ export type GanttCellStyle = GanttBaseCellStyle | GanttTrackCellStyle
@@ -0,0 +1,8 @@
1
+ import { drawGitGraphDiagramGrid } from "./drawing.js"
2
+ import { parseMermaidGitGraphDiagram } from "./parser.js"
3
+ import { renderGitGraphGridText } from "./render-grid.js"
4
+ import type { GitGraphDiagramRenderOptions } from "./types.js"
5
+
6
+ export function renderGitGraphDiagram(content: string, options: GitGraphDiagramRenderOptions = {}): string {
7
+ return renderGitGraphGridText(drawGitGraphDiagramGrid(parseMermaidGitGraphDiagram(content), options))
8
+ }