@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,59 @@
1
+ import type { DiagramPoint } from "../core/geometry.js"
2
+ import type { StateDiagramNote } from "./types.js"
3
+
4
+ interface NoteBounds {
5
+ left: number
6
+ top: number
7
+ width: number
8
+ height: number
9
+ centerY: number
10
+ note: StateDiagramNote
11
+ connector?: StateDiagramNoteConnector
12
+ }
13
+
14
+ interface TargetBounds {
15
+ left: number
16
+ top: number
17
+ width: number
18
+ height: number
19
+ centerY: number
20
+ }
21
+
22
+ export interface StateDiagramNoteConnector {
23
+ points: readonly DiagramPoint[]
24
+ connectorY: number
25
+ }
26
+
27
+ export function stateDiagramNoteConnector(
28
+ bounds: NoteBounds,
29
+ target: TargetBounds,
30
+ ): StateDiagramNoteConnector {
31
+ if (bounds.connector) return bounds.connector
32
+ const noteX = bounds.note.position === "right" ? bounds.left - 1 : bounds.left + bounds.width
33
+ const targetX = bounds.note.position === "right" ? target.left + target.width : target.left - 1
34
+ const targetBottom = target.top + target.height - 1
35
+ const noteBottom = bounds.top + bounds.height - 1
36
+ const noteAbove = noteBottom < target.top
37
+ const noteBelow = bounds.top > targetBottom
38
+
39
+ if (noteAbove || noteBelow) {
40
+ const connectorY = bounds.centerY
41
+ return {
42
+ connectorY,
43
+ points: [
44
+ { x: targetX, y: noteAbove ? target.top - 1 : targetBottom + 1 },
45
+ { x: targetX, y: connectorY },
46
+ { x: noteX, y: connectorY },
47
+ ],
48
+ }
49
+ }
50
+
51
+ const connectorY = Math.max(bounds.top + 1, Math.min(target.centerY, bounds.top + bounds.height - 2))
52
+ return {
53
+ connectorY,
54
+ points: [
55
+ { x: targetX, y: connectorY },
56
+ { x: noteX, y: connectorY },
57
+ ],
58
+ }
59
+ }
@@ -0,0 +1,11 @@
1
+ import type { BorderStyle } from "@opentui/core"
2
+ import type { StateDiagramArrowHeadStyle } from "./types.js"
3
+
4
+ export const DEFAULT_STATE_DIAGRAM_MIN_STATE_GAP = 5
5
+ export const DEFAULT_STATE_BORDER_STYLE = "rounded" satisfies BorderStyle
6
+ export const DEFAULT_STATE_ARROW_HEAD_STYLE = "filled" satisfies StateDiagramArrowHeadStyle
7
+
8
+ export function normalizeStateMinStateGap(value: number | undefined): number {
9
+ if (value === undefined || !Number.isFinite(value)) return DEFAULT_STATE_DIAGRAM_MIN_STATE_GAP
10
+ return Math.max(1, Math.trunc(value))
11
+ }
@@ -0,0 +1,224 @@
1
+ import { decodeMermaidText, firstMeaningfulMermaidLine, numberedMermaidLines } from "../core/mermaid.js"
2
+ import { splitDiagramLines } from "../core/text-lines.js"
3
+ import { MermaidSyntaxError } from "../diagnostics.js"
4
+ import { normalizeStateDiagramEndpoint, stateDiagramEndMarkerId, stateDiagramStartMarkerId } from "./endpoint.js"
5
+ import type {
6
+ StateDiagram,
7
+ StateDiagramCompositeState,
8
+ StateDiagramDirection,
9
+ StateDiagramNote,
10
+ StateDiagramState,
11
+ StateDiagramTransition,
12
+ } from "./types.js"
13
+
14
+ const DEFAULT_DIRECTION = "LR" satisfies StateDiagramDirection
15
+ const STATE_RE = /^state\s+"([^"]+)"\s+as\s+(\S+)$/i
16
+ const COMPOSITE_STATE_RE = /^state\s+(?:"([^"]+)"\s+as\s+)?(\S+)\s*\{$/i
17
+ const CHOICE_STATE_RE = /^state\s+(\S+)\s+<<choice>>$/i
18
+ const TRANSITION_RE = /^(\[\*\]|[^\s:]+)\s*-->\s*(\[\*\]|[^\s:]+)(?:\s*:\s*(.*))?$/
19
+ const DIRECTION_RE = /^direction\s+(TB|TD|BT|LR|RL)$/i
20
+ const NOTE_INLINE_RE = /^note\s+(left|right)\s+of\s+(\S+)\s*:\s*(.*)$/i
21
+ const NOTE_START_RE = /^note\s+(left|right)\s+of\s+(\S+)\s*$/i
22
+ const NOTE_END_RE = /^end\s+note$/i
23
+
24
+ function normalizeDirection(value?: string): StateDiagramDirection {
25
+ const upper = value?.toUpperCase()
26
+ if (upper === "TB" || upper === "TD" || upper === "BT" || upper === "LR" || upper === "RL") return upper
27
+ return DEFAULT_DIRECTION
28
+ }
29
+
30
+ function isMermaidHeader(line: string): boolean {
31
+ return line.toLowerCase() === "statediagram-v2" || line.toLowerCase() === "statediagram"
32
+ }
33
+
34
+ function ensureState(
35
+ states: Map<string, StateDiagramState>,
36
+ id: string,
37
+ label = id,
38
+ kind: StateDiagramState["kind"] = "state",
39
+ parentId?: string,
40
+ ): void {
41
+ const existing = states.get(id)
42
+ if (existing) {
43
+ if (existing.label === existing.id && label !== id) existing.label = label
44
+ if (parentId && !existing.parentId) existing.parentId = parentId
45
+ if (kind !== "state") {
46
+ existing.kind = kind
47
+ existing.label = label
48
+ }
49
+ return
50
+ }
51
+ states.set(id, parentId ? { id, label, kind, parentId } : { id, label, kind })
52
+ }
53
+
54
+ function resolveCompositeTransitionEndpoint(
55
+ id: string,
56
+ markerId: (scope?: string) => string,
57
+ compositeIds: ReadonlySet<string>,
58
+ states: Map<string, StateDiagramState>,
59
+ ): string {
60
+ if (!compositeIds.has(id)) return id
61
+ const marker = markerId(id)
62
+ return states.has(marker) ? marker : id
63
+ }
64
+
65
+ function resolveCompositeTransitions(
66
+ transitions: readonly StateDiagramTransition[],
67
+ compositeIds: ReadonlySet<string>,
68
+ states: Map<string, StateDiagramState>,
69
+ ): StateDiagramTransition[] {
70
+ return transitions.map((transition) => ({
71
+ from: resolveCompositeTransitionEndpoint(transition.from, stateDiagramEndMarkerId, compositeIds, states),
72
+ to: resolveCompositeTransitionEndpoint(transition.to, stateDiagramStartMarkerId, compositeIds, states),
73
+ label: transition.label,
74
+ }))
75
+ }
76
+
77
+ export function isMermaidStateDiagram(content: string): boolean {
78
+ return isMermaidHeader(firstMeaningfulMermaidLine(content) ?? "")
79
+ }
80
+
81
+ export function parseMermaidStateDiagram(content: string): StateDiagram {
82
+ const states = new Map<string, StateDiagramState>()
83
+ const transitions: StateDiagramTransition[] = []
84
+ const composites: StateDiagramCompositeState[] = []
85
+ const notes: StateDiagramNote[] = []
86
+ const parentStack: Array<{ id: string; lineNumber: number; sourceLine: string }> = []
87
+ let pendingNote:
88
+ | { target: string; position: "left" | "right"; lines: string[]; lineNumber: number; sourceLine: string }
89
+ | undefined
90
+ let direction: StateDiagramDirection = DEFAULT_DIRECTION
91
+
92
+ for (const source of numberedMermaidLines(content)) {
93
+ const line = source.text
94
+ if (pendingNote) {
95
+ if (NOTE_END_RE.test(line)) {
96
+ notes.push({
97
+ target: pendingNote.target,
98
+ position: pendingNote.position,
99
+ lines: pendingNote.lines.map(decodeMermaidText),
100
+ })
101
+ pendingNote = undefined
102
+ } else if (line || pendingNote.lines.length > 0) {
103
+ pendingNote.lines.push(line)
104
+ }
105
+ continue
106
+ }
107
+
108
+ if (!line || line.startsWith("%%") || isMermaidHeader(line)) continue
109
+
110
+ if (line === "}") {
111
+ if (parentStack.length === 0) {
112
+ throw new MermaidSyntaxError("state", source.lineNumber, line, 'Unexpected "}" without an open composite state')
113
+ }
114
+ parentStack.pop()
115
+ continue
116
+ }
117
+
118
+ const parentId = parentStack[parentStack.length - 1]?.id
119
+
120
+ const directionMatch = line.match(DIRECTION_RE)
121
+ if (directionMatch) {
122
+ if (parentStack.length > 0) {
123
+ throw new MermaidSyntaxError("state", source.lineNumber, line, "Composite-local direction is not supported")
124
+ }
125
+ direction = normalizeDirection(directionMatch[1])
126
+ continue
127
+ }
128
+
129
+ const inlineNoteMatch = line.match(NOTE_INLINE_RE)
130
+ if (inlineNoteMatch) {
131
+ notes.push({
132
+ position: inlineNoteMatch[1]!.toLowerCase() as "left" | "right",
133
+ target: inlineNoteMatch[2]!,
134
+ lines: splitDiagramLines(decodeMermaidText(inlineNoteMatch[3]!.trim())),
135
+ })
136
+ continue
137
+ }
138
+
139
+ const noteMatch = line.match(NOTE_START_RE)
140
+ if (noteMatch) {
141
+ pendingNote = {
142
+ position: noteMatch[1]!.toLowerCase() as "left" | "right",
143
+ target: noteMatch[2]!,
144
+ lines: [],
145
+ lineNumber: source.lineNumber,
146
+ sourceLine: line,
147
+ }
148
+ continue
149
+ }
150
+
151
+ const compositeMatch = line.match(COMPOSITE_STATE_RE)
152
+ if (compositeMatch) {
153
+ const id = compositeMatch[2]!
154
+ composites.push({
155
+ id,
156
+ label: decodeMermaidText(compositeMatch[1] ?? id),
157
+ ...(parentId ? { parentId } : {}),
158
+ })
159
+ parentStack.push({ id, lineNumber: source.lineNumber, sourceLine: line })
160
+ continue
161
+ }
162
+
163
+ const stateMatch = line.match(STATE_RE)
164
+ if (stateMatch) {
165
+ ensureState(states, stateMatch[2]!, decodeMermaidText(stateMatch[1]!), "state", parentId)
166
+ continue
167
+ }
168
+
169
+ const choiceMatch = line.match(CHOICE_STATE_RE)
170
+ if (choiceMatch) {
171
+ ensureState(states, choiceMatch[1]!, "", "choice", parentId)
172
+ continue
173
+ }
174
+
175
+ const transitionMatch = line.match(TRANSITION_RE)
176
+ if (transitionMatch) {
177
+ const rawFrom = transitionMatch[1]!
178
+ const rawTo = transitionMatch[2]!
179
+ if (rawFrom.includes("-->") || rawTo.includes("-->")) {
180
+ throw new MermaidSyntaxError("state", source.lineNumber, line)
181
+ }
182
+ const from = normalizeStateDiagramEndpoint(rawFrom, "from", parentId)
183
+ const to = normalizeStateDiagramEndpoint(rawTo, "to", parentId)
184
+ ensureState(states, from, rawFrom === "[*]" ? "●" : from, rawFrom === "[*]" ? "start" : "state", parentId)
185
+ ensureState(states, to, rawTo === "[*]" ? "◎" : to, rawTo === "[*]" ? "end" : "state", parentId)
186
+ transitions.push({ from, to, label: decodeMermaidText(transitionMatch[3]?.trim() ?? "") })
187
+ continue
188
+ }
189
+
190
+ throw new MermaidSyntaxError("state", source.lineNumber, line)
191
+ }
192
+
193
+ if (pendingNote) {
194
+ throw new MermaidSyntaxError(
195
+ "state",
196
+ pendingNote.lineNumber,
197
+ pendingNote.sourceLine,
198
+ 'Unclosed note; expected "end note"',
199
+ )
200
+ }
201
+
202
+ const unclosedComposite = parentStack[parentStack.length - 1]
203
+ if (unclosedComposite) {
204
+ throw new MermaidSyntaxError(
205
+ "state",
206
+ unclosedComposite.lineNumber,
207
+ unclosedComposite.sourceLine,
208
+ 'Unclosed composite state; expected "}"',
209
+ )
210
+ }
211
+
212
+ if (composites.length === 0) {
213
+ return { direction, states: [...states.values()], transitions, composites, notes }
214
+ }
215
+
216
+ const compositeIds = new Set(composites.map((composite) => composite.id))
217
+ return {
218
+ direction,
219
+ states: [...states.values()].filter((state) => !compositeIds.has(state.id)),
220
+ transitions: resolveCompositeTransitions(transitions, compositeIds, states),
221
+ composites,
222
+ notes,
223
+ }
224
+ }
@@ -0,0 +1,22 @@
1
+ import type { RGBA, StyledText } from "@opentui/core"
2
+ import type { DiagramCanvas } from "../core/canvas.js"
3
+ import { renderDiagramGridStyledTextByStyle } from "../core/render-grid.js"
4
+ import type { StateStyleColors } from "./style.js"
5
+ import type { StateCellStyle } from "./types.js"
6
+
7
+ export type StateGrid = DiagramCanvas<StateCellStyle>
8
+
9
+ export function renderStateGridText(grid: StateGrid): string {
10
+ return grid.toString({ trimTop: true, trimBottom: true })
11
+ }
12
+
13
+ export function renderStateGridStyledText(
14
+ grid: StateGrid,
15
+ colors: StateStyleColors,
16
+ backgrounds?: Partial<Record<StateCellStyle, RGBA>>,
17
+ ): StyledText {
18
+ return renderDiagramGridStyledTextByStyle(grid, colors, backgrounds, {
19
+ trimTop: true,
20
+ trimBottom: true,
21
+ })
22
+ }