@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/timeline/diagram.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { drawTimelineDiagramGrid } from "./drawing.js"
|
|
2
|
-
import { parseMermaidTimelineDiagram } from "./parser.js"
|
|
3
|
-
import { renderTimelineGridText } from "./render-grid.js"
|
|
4
|
-
import type { TimelineDiagramRenderOptions } from "./types.js"
|
|
5
|
-
|
|
6
|
-
export function renderTimelineDiagram(content: string, options: TimelineDiagramRenderOptions = {}): string {
|
|
7
|
-
return renderTimelineGridText(drawTimelineDiagramGrid(parseMermaidTimelineDiagram(content), options))
|
|
8
|
-
}
|
|
1
|
+
import { drawTimelineDiagramGrid } from "./drawing.js"
|
|
2
|
+
import { parseMermaidTimelineDiagram } from "./parser.js"
|
|
3
|
+
import { renderTimelineGridText } from "./render-grid.js"
|
|
4
|
+
import type { TimelineDiagramRenderOptions } from "./types.js"
|
|
5
|
+
|
|
6
|
+
export function renderTimelineDiagram(content: string, options: TimelineDiagramRenderOptions = {}): string {
|
|
7
|
+
return renderTimelineGridText(drawTimelineDiagramGrid(parseMermaidTimelineDiagram(content), options))
|
|
8
|
+
}
|
package/src/timeline/drawing.ts
CHANGED
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
import { DiagramCanvas } from "../core/canvas.js"
|
|
2
|
-
import { splitDiagramLines } from "../core/text-lines.js"
|
|
3
|
-
import { diagramTextWidth } from "../core/text.js"
|
|
4
|
-
import type { TimelineGrid } from "./render-grid.js"
|
|
5
|
-
import { TIMELINE_PERIOD_FADE_STYLES, TIMELINE_SECTION_FADE_STYLES } from "./style.js"
|
|
6
|
-
import type { TimelineCellStyle, TimelineDiagram, TimelineDiagramRenderOptions, TimelinePeriod } from "./types.js"
|
|
7
|
-
|
|
8
|
-
interface PeriodLayout {
|
|
9
|
-
period: TimelinePeriod
|
|
10
|
-
periodLines: string[]
|
|
11
|
-
eventLines: string[][]
|
|
12
|
-
height: number
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const JOIN_WIDTH = TIMELINE_SECTION_FADE_STYLES.length
|
|
16
|
-
const SPINE_OFFSET = JOIN_WIDTH + 1
|
|
17
|
-
const EVENT_OFFSET = 3
|
|
18
|
-
|
|
19
|
-
export function drawTimelineDiagramGrid(
|
|
20
|
-
diagram: TimelineDiagram,
|
|
21
|
-
_options: TimelineDiagramRenderOptions = {},
|
|
22
|
-
): TimelineGrid {
|
|
23
|
-
const periodLayouts = new Map<TimelinePeriod, PeriodLayout>()
|
|
24
|
-
let leftWidth = 0
|
|
25
|
-
let rightWidth = 0
|
|
26
|
-
let bodyHeight = 0
|
|
27
|
-
|
|
28
|
-
for (const entry of diagram.entries) {
|
|
29
|
-
if (entry.type === "section") {
|
|
30
|
-
const lines = splitDiagramLines(entry.section.label)
|
|
31
|
-
bodyHeight += lines.length + 1
|
|
32
|
-
for (const line of lines) leftWidth = Math.max(leftWidth, diagramTextWidth(line))
|
|
33
|
-
continue
|
|
34
|
-
}
|
|
35
|
-
const periodLines = splitDiagramLines(entry.period.period)
|
|
36
|
-
const eventLines = entry.period.events.map(splitDiagramLines)
|
|
37
|
-
const eventHeight = eventLines.reduce((height, lines) => height + lines.length, 0)
|
|
38
|
-
const height = Math.max(periodLines.length, eventHeight)
|
|
39
|
-
periodLayouts.set(entry.period, { period: entry.period, periodLines, eventLines, height })
|
|
40
|
-
for (const line of periodLines) leftWidth = Math.max(leftWidth, diagramTextWidth(line))
|
|
41
|
-
for (const lines of eventLines) {
|
|
42
|
-
for (const line of lines) rightWidth = Math.max(rightWidth, diagramTextWidth(line))
|
|
43
|
-
}
|
|
44
|
-
bodyHeight += height + 1
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const titleLines = diagram.title ? splitDiagramLines(diagram.title) : []
|
|
48
|
-
const bodyWidth = diagram.entries.length === 0 ? 0 : leftWidth + SPINE_OFFSET + EVENT_OFFSET + rightWidth + 1
|
|
49
|
-
let titleWidth = 0
|
|
50
|
-
for (const line of titleLines) titleWidth = Math.max(titleWidth, diagramTextWidth(line))
|
|
51
|
-
const width = Math.max(bodyWidth, titleWidth)
|
|
52
|
-
const titleHeight = titleLines.length === 0 ? 0 : titleLines.length + (diagram.entries.length === 0 ? 0 : 1)
|
|
53
|
-
if (width === 0) return new DiagramCanvas(0, 0)
|
|
54
|
-
|
|
55
|
-
const grid: TimelineGrid = new DiagramCanvas(width, titleHeight + bodyHeight)
|
|
56
|
-
titleLines.forEach((line, index) =>
|
|
57
|
-
setText(grid, Math.floor((width - diagramTextWidth(line)) / 2), index, line, "title"),
|
|
58
|
-
)
|
|
59
|
-
if (diagram.entries.length === 0) return grid
|
|
60
|
-
|
|
61
|
-
const spineX = leftWidth + SPINE_OFFSET
|
|
62
|
-
let y = titleHeight
|
|
63
|
-
let railStarted = false
|
|
64
|
-
for (const entry of diagram.entries) {
|
|
65
|
-
if (entry.type === "section") {
|
|
66
|
-
const lines = splitDiagramLines(entry.section.label)
|
|
67
|
-
lines.forEach((line, index) => {
|
|
68
|
-
setText(grid, leftWidth - diagramTextWidth(line), y + index, line, "section")
|
|
69
|
-
if (index > 0) setCell(grid, spineX, y + index, "│", "spine")
|
|
70
|
-
})
|
|
71
|
-
drawJoin(grid, leftWidth, y, TIMELINE_SECTION_FADE_STYLES)
|
|
72
|
-
setCell(grid, spineX, y, railStarted ? "┤" : "┐", "spine")
|
|
73
|
-
setCell(grid, spineX, y + lines.length, "│", "spine")
|
|
74
|
-
railStarted = true
|
|
75
|
-
y += lines.length + 1
|
|
76
|
-
continue
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const layout = periodLayouts.get(entry.period)!
|
|
80
|
-
for (let row = 0; row < layout.height + 1; row++) setCell(grid, spineX, y + row, "│", "spine")
|
|
81
|
-
railStarted = true
|
|
82
|
-
setCell(grid, spineX, y, "●", "spine")
|
|
83
|
-
layout.periodLines.forEach((line, index) => {
|
|
84
|
-
const lineWidth = diagramTextWidth(line)
|
|
85
|
-
setText(grid, leftWidth - lineWidth, y + index, line, "period")
|
|
86
|
-
})
|
|
87
|
-
drawJoin(grid, leftWidth, y, TIMELINE_PERIOD_FADE_STYLES)
|
|
88
|
-
|
|
89
|
-
let eventY = y
|
|
90
|
-
for (const lines of layout.eventLines) {
|
|
91
|
-
lines.forEach((line, index) => setText(grid, spineX + EVENT_OFFSET, eventY + index, line, "event"))
|
|
92
|
-
eventY += lines.length
|
|
93
|
-
}
|
|
94
|
-
y += layout.height + 1
|
|
95
|
-
}
|
|
96
|
-
return grid
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function drawJoin(grid: TimelineGrid, x: number, y: number, styles: readonly TimelineCellStyle[]): void {
|
|
100
|
-
styles.forEach((style, index) => setCell(grid, x + index + 1, y, "─", style))
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function setCell(grid: TimelineGrid, x: number, y: number, char: string, style: TimelineCellStyle): void {
|
|
104
|
-
grid.setCell(x, y, char, style)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function setText(grid: TimelineGrid, x: number, y: number, text: string, style: TimelineCellStyle): void {
|
|
108
|
-
grid.setText(x, y, text, style)
|
|
109
|
-
}
|
|
1
|
+
import { DiagramCanvas } from "../core/canvas.js"
|
|
2
|
+
import { splitDiagramLines } from "../core/text-lines.js"
|
|
3
|
+
import { diagramTextWidth } from "../core/text.js"
|
|
4
|
+
import type { TimelineGrid } from "./render-grid.js"
|
|
5
|
+
import { TIMELINE_PERIOD_FADE_STYLES, TIMELINE_SECTION_FADE_STYLES } from "./style.js"
|
|
6
|
+
import type { TimelineCellStyle, TimelineDiagram, TimelineDiagramRenderOptions, TimelinePeriod } from "./types.js"
|
|
7
|
+
|
|
8
|
+
interface PeriodLayout {
|
|
9
|
+
period: TimelinePeriod
|
|
10
|
+
periodLines: string[]
|
|
11
|
+
eventLines: string[][]
|
|
12
|
+
height: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const JOIN_WIDTH = TIMELINE_SECTION_FADE_STYLES.length
|
|
16
|
+
const SPINE_OFFSET = JOIN_WIDTH + 1
|
|
17
|
+
const EVENT_OFFSET = 3
|
|
18
|
+
|
|
19
|
+
export function drawTimelineDiagramGrid(
|
|
20
|
+
diagram: TimelineDiagram,
|
|
21
|
+
_options: TimelineDiagramRenderOptions = {},
|
|
22
|
+
): TimelineGrid {
|
|
23
|
+
const periodLayouts = new Map<TimelinePeriod, PeriodLayout>()
|
|
24
|
+
let leftWidth = 0
|
|
25
|
+
let rightWidth = 0
|
|
26
|
+
let bodyHeight = 0
|
|
27
|
+
|
|
28
|
+
for (const entry of diagram.entries) {
|
|
29
|
+
if (entry.type === "section") {
|
|
30
|
+
const lines = splitDiagramLines(entry.section.label)
|
|
31
|
+
bodyHeight += lines.length + 1
|
|
32
|
+
for (const line of lines) leftWidth = Math.max(leftWidth, diagramTextWidth(line))
|
|
33
|
+
continue
|
|
34
|
+
}
|
|
35
|
+
const periodLines = splitDiagramLines(entry.period.period)
|
|
36
|
+
const eventLines = entry.period.events.map(splitDiagramLines)
|
|
37
|
+
const eventHeight = eventLines.reduce((height, lines) => height + lines.length, 0)
|
|
38
|
+
const height = Math.max(periodLines.length, eventHeight)
|
|
39
|
+
periodLayouts.set(entry.period, { period: entry.period, periodLines, eventLines, height })
|
|
40
|
+
for (const line of periodLines) leftWidth = Math.max(leftWidth, diagramTextWidth(line))
|
|
41
|
+
for (const lines of eventLines) {
|
|
42
|
+
for (const line of lines) rightWidth = Math.max(rightWidth, diagramTextWidth(line))
|
|
43
|
+
}
|
|
44
|
+
bodyHeight += height + 1
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const titleLines = diagram.title ? splitDiagramLines(diagram.title) : []
|
|
48
|
+
const bodyWidth = diagram.entries.length === 0 ? 0 : leftWidth + SPINE_OFFSET + EVENT_OFFSET + rightWidth + 1
|
|
49
|
+
let titleWidth = 0
|
|
50
|
+
for (const line of titleLines) titleWidth = Math.max(titleWidth, diagramTextWidth(line))
|
|
51
|
+
const width = Math.max(bodyWidth, titleWidth)
|
|
52
|
+
const titleHeight = titleLines.length === 0 ? 0 : titleLines.length + (diagram.entries.length === 0 ? 0 : 1)
|
|
53
|
+
if (width === 0) return new DiagramCanvas(0, 0)
|
|
54
|
+
|
|
55
|
+
const grid: TimelineGrid = new DiagramCanvas(width, titleHeight + bodyHeight)
|
|
56
|
+
titleLines.forEach((line, index) =>
|
|
57
|
+
setText(grid, Math.floor((width - diagramTextWidth(line)) / 2), index, line, "title"),
|
|
58
|
+
)
|
|
59
|
+
if (diagram.entries.length === 0) return grid
|
|
60
|
+
|
|
61
|
+
const spineX = leftWidth + SPINE_OFFSET
|
|
62
|
+
let y = titleHeight
|
|
63
|
+
let railStarted = false
|
|
64
|
+
for (const entry of diagram.entries) {
|
|
65
|
+
if (entry.type === "section") {
|
|
66
|
+
const lines = splitDiagramLines(entry.section.label)
|
|
67
|
+
lines.forEach((line, index) => {
|
|
68
|
+
setText(grid, leftWidth - diagramTextWidth(line), y + index, line, "section")
|
|
69
|
+
if (index > 0) setCell(grid, spineX, y + index, "│", "spine")
|
|
70
|
+
})
|
|
71
|
+
drawJoin(grid, leftWidth, y, TIMELINE_SECTION_FADE_STYLES)
|
|
72
|
+
setCell(grid, spineX, y, railStarted ? "┤" : "┐", "spine")
|
|
73
|
+
setCell(grid, spineX, y + lines.length, "│", "spine")
|
|
74
|
+
railStarted = true
|
|
75
|
+
y += lines.length + 1
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const layout = periodLayouts.get(entry.period)!
|
|
80
|
+
for (let row = 0; row < layout.height + 1; row++) setCell(grid, spineX, y + row, "│", "spine")
|
|
81
|
+
railStarted = true
|
|
82
|
+
setCell(grid, spineX, y, "●", "spine")
|
|
83
|
+
layout.periodLines.forEach((line, index) => {
|
|
84
|
+
const lineWidth = diagramTextWidth(line)
|
|
85
|
+
setText(grid, leftWidth - lineWidth, y + index, line, "period")
|
|
86
|
+
})
|
|
87
|
+
drawJoin(grid, leftWidth, y, TIMELINE_PERIOD_FADE_STYLES)
|
|
88
|
+
|
|
89
|
+
let eventY = y
|
|
90
|
+
for (const lines of layout.eventLines) {
|
|
91
|
+
lines.forEach((line, index) => setText(grid, spineX + EVENT_OFFSET, eventY + index, line, "event"))
|
|
92
|
+
eventY += lines.length
|
|
93
|
+
}
|
|
94
|
+
y += layout.height + 1
|
|
95
|
+
}
|
|
96
|
+
return grid
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function drawJoin(grid: TimelineGrid, x: number, y: number, styles: readonly TimelineCellStyle[]): void {
|
|
100
|
+
styles.forEach((style, index) => setCell(grid, x + index + 1, y, "─", style))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function setCell(grid: TimelineGrid, x: number, y: number, char: string, style: TimelineCellStyle): void {
|
|
104
|
+
grid.setCell(x, y, char, style)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function setText(grid: TimelineGrid, x: number, y: number, text: string, style: TimelineCellStyle): void {
|
|
108
|
+
grid.setText(x, y, text, style)
|
|
109
|
+
}
|
package/src/timeline/parser.ts
CHANGED
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
import { firstMeaningfulMermaidLine, meaningfulNumberedMermaidLines, stripMermaidQuotes } from "../core/mermaid.js"
|
|
2
|
-
import { MermaidSyntaxError } from "../diagnostics.js"
|
|
3
|
-
import type { TimelineDiagram, TimelineDirection, TimelineEntry, TimelinePeriod, TimelineSection } from "./types.js"
|
|
4
|
-
|
|
5
|
-
const HEADER_RE = /^timeline(?:\s+(TD|LR))?$/i
|
|
6
|
-
const TITLE_RE = /^title(?:\s+(.+))?$/i
|
|
7
|
-
const SECTION_RE = /^section(?:\s+(.+))?$/i
|
|
8
|
-
const ACCESSIBILITY_RE = /^acc(?:Title|Descr)(?::|\s|$)/i
|
|
9
|
-
|
|
10
|
-
export function isMermaidTimelineDiagram(content: string): boolean {
|
|
11
|
-
return HEADER_RE.test(firstMeaningfulMermaidLine(content) ?? "")
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function parseMermaidTimelineDiagram(content: string): TimelineDiagram {
|
|
15
|
-
const sections: TimelineSection[] = []
|
|
16
|
-
const periods: TimelinePeriod[] = []
|
|
17
|
-
const entries: TimelineEntry[] = []
|
|
18
|
-
let direction: TimelineDirection = "LR"
|
|
19
|
-
let title: string | undefined
|
|
20
|
-
let currentPeriod: TimelinePeriod | undefined
|
|
21
|
-
let inAccessibilityDescription = false
|
|
22
|
-
|
|
23
|
-
for (const source of meaningfulNumberedMermaidLines(content)) {
|
|
24
|
-
const line = stripTimelineComment(source.text)
|
|
25
|
-
if (inAccessibilityDescription) {
|
|
26
|
-
if (line === "}") inAccessibilityDescription = false
|
|
27
|
-
continue
|
|
28
|
-
}
|
|
29
|
-
if (/^accDescr\s*\{$/i.test(line)) {
|
|
30
|
-
inAccessibilityDescription = true
|
|
31
|
-
continue
|
|
32
|
-
}
|
|
33
|
-
if (!line || line.startsWith("#") || ACCESSIBILITY_RE.test(line)) continue
|
|
34
|
-
const header = line.match(HEADER_RE)
|
|
35
|
-
if (header) {
|
|
36
|
-
direction = (header[1]?.toUpperCase() as TimelineDirection | undefined) ?? "LR"
|
|
37
|
-
continue
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const titleMatch = line.match(TITLE_RE)
|
|
41
|
-
if (titleMatch) {
|
|
42
|
-
if (!titleMatch[1]) throw syntaxError(source.lineNumber, line, "Timeline title cannot be empty")
|
|
43
|
-
title = stripMermaidQuotes(titleMatch[1])
|
|
44
|
-
continue
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const sectionMatch = line.match(SECTION_RE)
|
|
48
|
-
if (sectionMatch) {
|
|
49
|
-
if (!sectionMatch[1]) throw syntaxError(source.lineNumber, line, "Timeline section cannot be empty")
|
|
50
|
-
const section = { label: stripMermaidQuotes(sectionMatch[1]) }
|
|
51
|
-
sections.push(section)
|
|
52
|
-
entries.push({ type: "section", section })
|
|
53
|
-
currentPeriod = undefined
|
|
54
|
-
continue
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (line.startsWith(":")) {
|
|
58
|
-
if (!currentPeriod) {
|
|
59
|
-
throw syntaxError(source.lineNumber, line, "Timeline continuation requires a preceding period")
|
|
60
|
-
}
|
|
61
|
-
currentPeriod.events.push(...parseEvents(line.slice(1), source.lineNumber, line))
|
|
62
|
-
continue
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const fields = splitEventFields(line)
|
|
66
|
-
const periodLabel = stripMermaidQuotes(fields.shift()!)
|
|
67
|
-
if (!periodLabel) throw syntaxError(source.lineNumber, line, "Timeline period cannot be empty")
|
|
68
|
-
const period = {
|
|
69
|
-
period: periodLabel,
|
|
70
|
-
events: fields.length === 0 ? [] : parseEventFields(fields, source.lineNumber, line),
|
|
71
|
-
}
|
|
72
|
-
periods.push(period)
|
|
73
|
-
entries.push({ type: "period", period })
|
|
74
|
-
currentPeriod = period
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return { direction, ...(title === undefined ? {} : { title }), sections, periods, entries }
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function parseEvents(value: string, lineNumber: number, sourceLine: string): string[] {
|
|
81
|
-
return parseEventFields(splitEventFields(value), lineNumber, sourceLine)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function parseEventFields(fields: string[], lineNumber: number, sourceLine: string): string[] {
|
|
85
|
-
const events = fields.map(stripMermaidQuotes)
|
|
86
|
-
if (events.length === 0 || events.some((event) => event.length === 0)) {
|
|
87
|
-
throw syntaxError(lineNumber, sourceLine, "Timeline event cannot be empty")
|
|
88
|
-
}
|
|
89
|
-
return events
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function splitEventFields(value: string): string[] {
|
|
93
|
-
const fields: string[] = []
|
|
94
|
-
let quote: '"' | "'" | undefined
|
|
95
|
-
let start = 0
|
|
96
|
-
for (let index = 0; index < value.length; index++) {
|
|
97
|
-
const char = value[index]
|
|
98
|
-
if (char === '"' || char === "'") {
|
|
99
|
-
if (quote === char) quote = undefined
|
|
100
|
-
else if (quote === undefined && value.slice(start, index).trim() === "") quote = char
|
|
101
|
-
continue
|
|
102
|
-
}
|
|
103
|
-
const next = value[index + 1]
|
|
104
|
-
if (char !== ":" || quote !== undefined || (next !== undefined && !/\s/.test(next))) continue
|
|
105
|
-
fields.push(value.slice(start, index))
|
|
106
|
-
start = index + 1
|
|
107
|
-
}
|
|
108
|
-
fields.push(value.slice(start))
|
|
109
|
-
return fields
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function stripTimelineComment(value: string): string {
|
|
113
|
-
const comment = value.indexOf("%%")
|
|
114
|
-
return (comment < 0 ? value : value.slice(0, comment)).trim()
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function syntaxError(lineNumber: number, sourceLine: string, reason?: string): MermaidSyntaxError {
|
|
118
|
-
return new MermaidSyntaxError("timeline", lineNumber, sourceLine, reason)
|
|
119
|
-
}
|
|
1
|
+
import { firstMeaningfulMermaidLine, meaningfulNumberedMermaidLines, stripMermaidQuotes } from "../core/mermaid.js"
|
|
2
|
+
import { MermaidSyntaxError } from "../diagnostics.js"
|
|
3
|
+
import type { TimelineDiagram, TimelineDirection, TimelineEntry, TimelinePeriod, TimelineSection } from "./types.js"
|
|
4
|
+
|
|
5
|
+
const HEADER_RE = /^timeline(?:\s+(TD|LR))?$/i
|
|
6
|
+
const TITLE_RE = /^title(?:\s+(.+))?$/i
|
|
7
|
+
const SECTION_RE = /^section(?:\s+(.+))?$/i
|
|
8
|
+
const ACCESSIBILITY_RE = /^acc(?:Title|Descr)(?::|\s|$)/i
|
|
9
|
+
|
|
10
|
+
export function isMermaidTimelineDiagram(content: string): boolean {
|
|
11
|
+
return HEADER_RE.test(firstMeaningfulMermaidLine(content) ?? "")
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function parseMermaidTimelineDiagram(content: string): TimelineDiagram {
|
|
15
|
+
const sections: TimelineSection[] = []
|
|
16
|
+
const periods: TimelinePeriod[] = []
|
|
17
|
+
const entries: TimelineEntry[] = []
|
|
18
|
+
let direction: TimelineDirection = "LR"
|
|
19
|
+
let title: string | undefined
|
|
20
|
+
let currentPeriod: TimelinePeriod | undefined
|
|
21
|
+
let inAccessibilityDescription = false
|
|
22
|
+
|
|
23
|
+
for (const source of meaningfulNumberedMermaidLines(content)) {
|
|
24
|
+
const line = stripTimelineComment(source.text)
|
|
25
|
+
if (inAccessibilityDescription) {
|
|
26
|
+
if (line === "}") inAccessibilityDescription = false
|
|
27
|
+
continue
|
|
28
|
+
}
|
|
29
|
+
if (/^accDescr\s*\{$/i.test(line)) {
|
|
30
|
+
inAccessibilityDescription = true
|
|
31
|
+
continue
|
|
32
|
+
}
|
|
33
|
+
if (!line || line.startsWith("#") || ACCESSIBILITY_RE.test(line)) continue
|
|
34
|
+
const header = line.match(HEADER_RE)
|
|
35
|
+
if (header) {
|
|
36
|
+
direction = (header[1]?.toUpperCase() as TimelineDirection | undefined) ?? "LR"
|
|
37
|
+
continue
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const titleMatch = line.match(TITLE_RE)
|
|
41
|
+
if (titleMatch) {
|
|
42
|
+
if (!titleMatch[1]) throw syntaxError(source.lineNumber, line, "Timeline title cannot be empty")
|
|
43
|
+
title = stripMermaidQuotes(titleMatch[1])
|
|
44
|
+
continue
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const sectionMatch = line.match(SECTION_RE)
|
|
48
|
+
if (sectionMatch) {
|
|
49
|
+
if (!sectionMatch[1]) throw syntaxError(source.lineNumber, line, "Timeline section cannot be empty")
|
|
50
|
+
const section = { label: stripMermaidQuotes(sectionMatch[1]) }
|
|
51
|
+
sections.push(section)
|
|
52
|
+
entries.push({ type: "section", section })
|
|
53
|
+
currentPeriod = undefined
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (line.startsWith(":")) {
|
|
58
|
+
if (!currentPeriod) {
|
|
59
|
+
throw syntaxError(source.lineNumber, line, "Timeline continuation requires a preceding period")
|
|
60
|
+
}
|
|
61
|
+
currentPeriod.events.push(...parseEvents(line.slice(1), source.lineNumber, line))
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const fields = splitEventFields(line)
|
|
66
|
+
const periodLabel = stripMermaidQuotes(fields.shift()!)
|
|
67
|
+
if (!periodLabel) throw syntaxError(source.lineNumber, line, "Timeline period cannot be empty")
|
|
68
|
+
const period = {
|
|
69
|
+
period: periodLabel,
|
|
70
|
+
events: fields.length === 0 ? [] : parseEventFields(fields, source.lineNumber, line),
|
|
71
|
+
}
|
|
72
|
+
periods.push(period)
|
|
73
|
+
entries.push({ type: "period", period })
|
|
74
|
+
currentPeriod = period
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return { direction, ...(title === undefined ? {} : { title }), sections, periods, entries }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function parseEvents(value: string, lineNumber: number, sourceLine: string): string[] {
|
|
81
|
+
return parseEventFields(splitEventFields(value), lineNumber, sourceLine)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function parseEventFields(fields: string[], lineNumber: number, sourceLine: string): string[] {
|
|
85
|
+
const events = fields.map(stripMermaidQuotes)
|
|
86
|
+
if (events.length === 0 || events.some((event) => event.length === 0)) {
|
|
87
|
+
throw syntaxError(lineNumber, sourceLine, "Timeline event cannot be empty")
|
|
88
|
+
}
|
|
89
|
+
return events
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function splitEventFields(value: string): string[] {
|
|
93
|
+
const fields: string[] = []
|
|
94
|
+
let quote: '"' | "'" | undefined
|
|
95
|
+
let start = 0
|
|
96
|
+
for (let index = 0; index < value.length; index++) {
|
|
97
|
+
const char = value[index]
|
|
98
|
+
if (char === '"' || char === "'") {
|
|
99
|
+
if (quote === char) quote = undefined
|
|
100
|
+
else if (quote === undefined && value.slice(start, index).trim() === "") quote = char
|
|
101
|
+
continue
|
|
102
|
+
}
|
|
103
|
+
const next = value[index + 1]
|
|
104
|
+
if (char !== ":" || quote !== undefined || (next !== undefined && !/\s/.test(next))) continue
|
|
105
|
+
fields.push(value.slice(start, index))
|
|
106
|
+
start = index + 1
|
|
107
|
+
}
|
|
108
|
+
fields.push(value.slice(start))
|
|
109
|
+
return fields
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function stripTimelineComment(value: string): string {
|
|
113
|
+
const comment = value.indexOf("%%")
|
|
114
|
+
return (comment < 0 ? value : value.slice(0, comment)).trim()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function syntaxError(lineNumber: number, sourceLine: string, reason?: string): MermaidSyntaxError {
|
|
118
|
+
return new MermaidSyntaxError("timeline", lineNumber, sourceLine, reason)
|
|
119
|
+
}
|
|
@@ -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 { TimelineStyleColors } from "./style.js"
|
|
5
|
-
import type { TimelineCellStyle } from "./types.js"
|
|
6
|
-
|
|
7
|
-
export type TimelineGrid = DiagramCanvas<TimelineCellStyle>
|
|
8
|
-
|
|
9
|
-
export function renderTimelineGridText(grid: TimelineGrid): string {
|
|
10
|
-
return grid.toString({ trimBottom: true })
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function renderTimelineGridStyledText(grid: TimelineGrid, colors: TimelineStyleColors): 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 { TimelineStyleColors } from "./style.js"
|
|
5
|
+
import type { TimelineCellStyle } from "./types.js"
|
|
6
|
+
|
|
7
|
+
export type TimelineGrid = DiagramCanvas<TimelineCellStyle>
|
|
8
|
+
|
|
9
|
+
export function renderTimelineGridText(grid: TimelineGrid): string {
|
|
10
|
+
return grid.toString({ trimBottom: true })
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function renderTimelineGridStyledText(grid: TimelineGrid, colors: TimelineStyleColors): StyledText {
|
|
14
|
+
return renderDiagramGridStyledText(grid, (run) => (run.style ? colors[run.style] : undefined), undefined, {
|
|
15
|
+
trimBottom: true,
|
|
16
|
+
})
|
|
17
|
+
}
|
package/src/timeline/style.ts
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import { RGBA } from "@opentui/core"
|
|
2
|
-
import { blendColor, numberedStyleKeys, rgba, type DiagramRgb } from "../core/color/style.js"
|
|
3
|
-
import type { TimelineBaseCellStyle, TimelineCellStyle } from "./types.js"
|
|
4
|
-
|
|
5
|
-
const DEFAULT_THEME_RGB = {
|
|
6
|
-
title: [228, 239, 232],
|
|
7
|
-
section: [154, 184, 169],
|
|
8
|
-
period: [230, 177, 126],
|
|
9
|
-
spine: [111, 138, 126],
|
|
10
|
-
event: [134, 225, 200],
|
|
11
|
-
} as const satisfies Record<TimelineBaseCellStyle, DiagramRgb>
|
|
12
|
-
|
|
13
|
-
export type TimelineStyleColors = Required<Record<TimelineCellStyle, RGBA>>
|
|
14
|
-
export const TIMELINE_SECTION_FADE_STYLES = numberedStyleKeys("sectionFade", [1, 2, 3] as const)
|
|
15
|
-
export const TIMELINE_PERIOD_FADE_STYLES = numberedStyleKeys("periodFade", [1, 2, 3] as const)
|
|
16
|
-
|
|
17
|
-
export function resolveTimelineStyleColors(
|
|
18
|
-
colors: Partial<Record<TimelineBaseCellStyle, RGBA | undefined>> = {},
|
|
19
|
-
): TimelineStyleColors {
|
|
20
|
-
const section = colors.section ?? rgba(DEFAULT_THEME_RGB.section)
|
|
21
|
-
const period = colors.period ?? rgba(DEFAULT_THEME_RGB.period)
|
|
22
|
-
const spine = colors.spine ?? rgba(DEFAULT_THEME_RGB.spine)
|
|
23
|
-
return {
|
|
24
|
-
title: colors.title ?? rgba(DEFAULT_THEME_RGB.title),
|
|
25
|
-
section,
|
|
26
|
-
period,
|
|
27
|
-
spine,
|
|
28
|
-
event: colors.event ?? rgba(DEFAULT_THEME_RGB.event),
|
|
29
|
-
sectionFade1: blendColor(section, spine, 0.5),
|
|
30
|
-
sectionFade2: blendColor(section, spine, 0.67),
|
|
31
|
-
sectionFade3: blendColor(section, spine, 0.83),
|
|
32
|
-
periodFade1: blendColor(period, spine, 0.5),
|
|
33
|
-
periodFade2: blendColor(period, spine, 0.67),
|
|
34
|
-
periodFade3: blendColor(period, spine, 0.83),
|
|
35
|
-
}
|
|
36
|
-
}
|
|
1
|
+
import { RGBA } from "@opentui/core"
|
|
2
|
+
import { blendColor, numberedStyleKeys, rgba, type DiagramRgb } from "../core/color/style.js"
|
|
3
|
+
import type { TimelineBaseCellStyle, TimelineCellStyle } from "./types.js"
|
|
4
|
+
|
|
5
|
+
const DEFAULT_THEME_RGB = {
|
|
6
|
+
title: [228, 239, 232],
|
|
7
|
+
section: [154, 184, 169],
|
|
8
|
+
period: [230, 177, 126],
|
|
9
|
+
spine: [111, 138, 126],
|
|
10
|
+
event: [134, 225, 200],
|
|
11
|
+
} as const satisfies Record<TimelineBaseCellStyle, DiagramRgb>
|
|
12
|
+
|
|
13
|
+
export type TimelineStyleColors = Required<Record<TimelineCellStyle, RGBA>>
|
|
14
|
+
export const TIMELINE_SECTION_FADE_STYLES = numberedStyleKeys("sectionFade", [1, 2, 3] as const)
|
|
15
|
+
export const TIMELINE_PERIOD_FADE_STYLES = numberedStyleKeys("periodFade", [1, 2, 3] as const)
|
|
16
|
+
|
|
17
|
+
export function resolveTimelineStyleColors(
|
|
18
|
+
colors: Partial<Record<TimelineBaseCellStyle, RGBA | undefined>> = {},
|
|
19
|
+
): TimelineStyleColors {
|
|
20
|
+
const section = colors.section ?? rgba(DEFAULT_THEME_RGB.section)
|
|
21
|
+
const period = colors.period ?? rgba(DEFAULT_THEME_RGB.period)
|
|
22
|
+
const spine = colors.spine ?? rgba(DEFAULT_THEME_RGB.spine)
|
|
23
|
+
return {
|
|
24
|
+
title: colors.title ?? rgba(DEFAULT_THEME_RGB.title),
|
|
25
|
+
section,
|
|
26
|
+
period,
|
|
27
|
+
spine,
|
|
28
|
+
event: colors.event ?? rgba(DEFAULT_THEME_RGB.event),
|
|
29
|
+
sectionFade1: blendColor(section, spine, 0.5),
|
|
30
|
+
sectionFade2: blendColor(section, spine, 0.67),
|
|
31
|
+
sectionFade3: blendColor(section, spine, 0.83),
|
|
32
|
+
periodFade1: blendColor(period, spine, 0.5),
|
|
33
|
+
periodFade2: blendColor(period, spine, 0.67),
|
|
34
|
+
periodFade3: blendColor(period, spine, 0.83),
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/timeline/types.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
export type TimelineDirection = "TD" | "LR"
|
|
2
|
-
|
|
3
|
-
export interface TimelineSection {
|
|
4
|
-
label: string
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface TimelinePeriod {
|
|
8
|
-
period: string
|
|
9
|
-
events: string[]
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type TimelineEntry = { type: "section"; section: TimelineSection } | { type: "period"; period: TimelinePeriod }
|
|
13
|
-
|
|
14
|
-
export interface TimelineDiagram {
|
|
15
|
-
direction: TimelineDirection
|
|
16
|
-
title?: string
|
|
17
|
-
sections: TimelineSection[]
|
|
18
|
-
periods: TimelinePeriod[]
|
|
19
|
-
entries: TimelineEntry[]
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface TimelineDiagramRenderOptions {
|
|
23
|
-
/** Parsed for Mermaid compatibility. Timeline diagrams always use a vertical terminal layout. */
|
|
24
|
-
direction?: TimelineDirection
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type TimelineBaseCellStyle = "title" | "section" | "period" | "spine" | "event"
|
|
28
|
-
export type TimelineFadeStep = 1 | 2 | 3
|
|
29
|
-
export type TimelineSectionFadeStyle = `sectionFade${TimelineFadeStep}`
|
|
30
|
-
export type TimelinePeriodFadeStyle = `periodFade${TimelineFadeStep}`
|
|
31
|
-
export type TimelineCellStyle = TimelineBaseCellStyle | TimelineSectionFadeStyle | TimelinePeriodFadeStyle
|
|
1
|
+
export type TimelineDirection = "TD" | "LR"
|
|
2
|
+
|
|
3
|
+
export interface TimelineSection {
|
|
4
|
+
label: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface TimelinePeriod {
|
|
8
|
+
period: string
|
|
9
|
+
events: string[]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type TimelineEntry = { type: "section"; section: TimelineSection } | { type: "period"; period: TimelinePeriod }
|
|
13
|
+
|
|
14
|
+
export interface TimelineDiagram {
|
|
15
|
+
direction: TimelineDirection
|
|
16
|
+
title?: string
|
|
17
|
+
sections: TimelineSection[]
|
|
18
|
+
periods: TimelinePeriod[]
|
|
19
|
+
entries: TimelineEntry[]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface TimelineDiagramRenderOptions {
|
|
23
|
+
/** Parsed for Mermaid compatibility. Timeline diagrams always use a vertical terminal layout. */
|
|
24
|
+
direction?: TimelineDirection
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type TimelineBaseCellStyle = "title" | "section" | "period" | "spine" | "event"
|
|
28
|
+
export type TimelineFadeStep = 1 | 2 | 3
|
|
29
|
+
export type TimelineSectionFadeStyle = `sectionFade${TimelineFadeStep}`
|
|
30
|
+
export type TimelinePeriodFadeStyle = `periodFade${TimelineFadeStep}`
|
|
31
|
+
export type TimelineCellStyle = TimelineBaseCellStyle | TimelineSectionFadeStyle | TimelinePeriodFadeStyle
|