@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.
- package/package.json +29 -4
- package/script/layout-audit.ts +98 -0
- package/src/core/canvas.ts +228 -0
- package/src/core/color/style.ts +56 -0
- package/src/core/drawing.ts +301 -0
- package/src/core/geometry.ts +347 -0
- package/src/core/mermaid.ts +38 -0
- package/src/core/render-grid.ts +51 -0
- package/src/core/spatial.ts +243 -0
- package/src/core/text-lines.ts +32 -0
- package/src/core/text.ts +44 -0
- package/src/detect.ts +17 -0
- package/src/diagnostics.ts +16 -0
- package/src/flowchart/drawing.ts +340 -0
- package/src/flowchart/labels.ts +144 -0
- package/src/flowchart/layout.ts +1427 -0
- package/src/flowchart/options.ts +12 -0
- package/src/flowchart/parser.ts +467 -0
- package/src/flowchart/render.ts +10 -0
- package/src/flowchart/routing.ts +1542 -0
- package/src/flowchart/style.ts +79 -0
- package/src/flowchart/types.ts +65 -0
- package/src/gantt/diagram.ts +8 -0
- package/src/gantt/drawing.ts +191 -0
- package/src/gantt/parser.ts +161 -0
- package/src/gantt/render-grid.ts +17 -0
- package/src/gantt/style.ts +36 -0
- package/src/gantt/types.ts +44 -0
- package/src/gitgraph/diagram.ts +8 -0
- package/src/gitgraph/drawing.ts +234 -0
- package/src/gitgraph/parser.ts +219 -0
- package/src/gitgraph/render-grid.ts +17 -0
- package/src/gitgraph/style.ts +37 -0
- package/src/gitgraph/types.ts +36 -0
- package/src/markdown.ts +336 -0
- package/src/palette.ts +75 -0
- package/src/plugin.ts +15 -0
- package/src/sequence/diagram.ts +8 -0
- package/src/sequence/drawing.ts +287 -0
- package/src/sequence/options.ts +8 -0
- package/src/sequence/parser.ts +282 -0
- package/src/sequence/placement.ts +684 -0
- package/src/sequence/render-grid.ts +22 -0
- package/src/sequence/style.ts +81 -0
- package/src/sequence/types.ts +74 -0
- package/src/state/diagram.ts +8 -0
- package/src/state/drawing.ts +360 -0
- package/src/state/endpoint.ts +15 -0
- package/src/state/layout.ts +1134 -0
- package/src/state/note.ts +59 -0
- package/src/state/options.ts +11 -0
- package/src/state/parser.ts +224 -0
- package/src/state/render-grid.ts +22 -0
- package/src/state/routing.ts +1522 -0
- package/src/state/search.ts +103 -0
- package/src/state/style.ts +52 -0
- package/src/state/types.ts +63 -0
- package/src/state/visible-model.ts +77 -0
- package/src/test/diagram.ts +32 -0
- package/src/test/layout-audit/fixtures.ts +538 -0
- package/src/test/layout-audit/harness.ts +522 -0
- package/src/timeline/diagram.ts +8 -0
- package/src/timeline/drawing.ts +109 -0
- package/src/timeline/parser.ts +119 -0
- package/src/timeline/render-grid.ts +17 -0
- package/src/timeline/style.ts +36 -0
- package/src/timeline/types.ts +31 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { StyledText, type TextChunk } from "@opentui/core"
|
|
2
|
+
import type { DiagramCanvas, DiagramCanvasRun, DiagramCanvasRunOptions } from "./canvas.js"
|
|
3
|
+
|
|
4
|
+
export function renderDiagramGridStyledText<Style extends string, Metadata extends object = object>(
|
|
5
|
+
grid: DiagramCanvas<Style, Metadata>,
|
|
6
|
+
fg: (run: DiagramCanvasRun<Style, Metadata>) => TextChunk["fg"],
|
|
7
|
+
bg?: (run: DiagramCanvasRun<Style, Metadata>) => TextChunk["bg"],
|
|
8
|
+
options?: DiagramCanvasRunOptions<Style, Metadata> & {
|
|
9
|
+
attributes?: (run: DiagramCanvasRun<Style, Metadata>) => TextChunk["attributes"]
|
|
10
|
+
},
|
|
11
|
+
): StyledText {
|
|
12
|
+
const chunks: TextChunk[] = []
|
|
13
|
+
|
|
14
|
+
grid.forEachRun(
|
|
15
|
+
(run) => {
|
|
16
|
+
chunks.push({
|
|
17
|
+
__isChunk: true,
|
|
18
|
+
text: run.text,
|
|
19
|
+
fg: fg(run),
|
|
20
|
+
bg: bg?.(run),
|
|
21
|
+
attributes: options?.attributes?.(run),
|
|
22
|
+
})
|
|
23
|
+
},
|
|
24
|
+
() => {
|
|
25
|
+
chunks.push({ __isChunk: true, text: "\n" })
|
|
26
|
+
},
|
|
27
|
+
options,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
return new StyledText(chunks)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function renderDiagramGridStyledTextByStyle<Style extends string, Metadata extends object = object>(
|
|
34
|
+
grid: DiagramCanvas<Style, Metadata>,
|
|
35
|
+
foregrounds: Readonly<Record<Style, TextChunk["fg"]>>,
|
|
36
|
+
backgrounds?: Readonly<Partial<Record<Style, TextChunk["bg"]>>>,
|
|
37
|
+
options?: DiagramCanvasRunOptions<Style, Metadata> & {
|
|
38
|
+
attributes?: (run: DiagramCanvasRun<Style, Metadata>) => TextChunk["attributes"]
|
|
39
|
+
},
|
|
40
|
+
): StyledText {
|
|
41
|
+
const background =
|
|
42
|
+
backgrounds && Object.values(backgrounds).some((value) => value !== undefined)
|
|
43
|
+
? (run: DiagramCanvasRun<Style, Metadata>) => (run.style ? backgrounds[run.style] : undefined)
|
|
44
|
+
: undefined
|
|
45
|
+
return renderDiagramGridStyledText(
|
|
46
|
+
grid,
|
|
47
|
+
(run) => (run.style ? foregrounds[run.style] : undefined),
|
|
48
|
+
background,
|
|
49
|
+
options,
|
|
50
|
+
)
|
|
51
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { orthogonalPathPoints, type DiagramBounds, type DiagramPoint } from "./geometry.js"
|
|
2
|
+
|
|
3
|
+
export type SpatialRole = "body" | "boundary" | "terminal" | "route" | "label"
|
|
4
|
+
|
|
5
|
+
export interface SpatialSpan {
|
|
6
|
+
readonly y: number
|
|
7
|
+
readonly fromX: number
|
|
8
|
+
readonly toX: number
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SpatialClaim {
|
|
12
|
+
readonly id: string
|
|
13
|
+
readonly owner: string
|
|
14
|
+
readonly role: SpatialRole
|
|
15
|
+
readonly spans: readonly SpatialSpan[]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SpatialContact {
|
|
19
|
+
owner: string
|
|
20
|
+
points: readonly DiagramPoint[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SpatialConflict {
|
|
24
|
+
moving: SpatialClaim
|
|
25
|
+
existing: SpatialClaim
|
|
26
|
+
point: DiagramPoint
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface SpatialClearance {
|
|
30
|
+
x: number
|
|
31
|
+
y: number
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SpatialCollisionPolicy {
|
|
35
|
+
contacts?: readonly SpatialContact[]
|
|
36
|
+
clearance?: number | SpatialClearance | Partial<Record<SpatialRole, number | SpatialClearance>>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function normalizedSpan(y: number, fromX: number, toX: number): SpatialSpan {
|
|
40
|
+
return { y, fromX: Math.min(fromX, toX), toX: Math.max(fromX, toX) }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function assertFiniteInteger(value: number, name: string): void {
|
|
44
|
+
if (!Number.isFinite(value) || !Number.isInteger(value)) throw new RangeError(`${name} must be a finite integer`)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function spatialRectSpans(bounds: Pick<DiagramBounds, "left" | "top" | "width" | "height">): SpatialSpan[] {
|
|
48
|
+
assertFiniteInteger(bounds.left, "bounds.left")
|
|
49
|
+
assertFiniteInteger(bounds.top, "bounds.top")
|
|
50
|
+
assertFiniteInteger(bounds.width, "bounds.width")
|
|
51
|
+
assertFiniteInteger(bounds.height, "bounds.height")
|
|
52
|
+
if (bounds.width <= 0 || bounds.height <= 0) throw new RangeError("Spatial bounds must have positive dimensions")
|
|
53
|
+
return Array.from({ length: bounds.height }, (_, offset) =>
|
|
54
|
+
normalizedSpan(bounds.top + offset, bounds.left, bounds.left + bounds.width - 1),
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function spatialPathSpans(points: readonly DiagramPoint[]): SpatialSpan[] {
|
|
59
|
+
for (const [index, point] of points.entries()) {
|
|
60
|
+
assertFiniteInteger(point.x, `points[${index}].x`)
|
|
61
|
+
assertFiniteInteger(point.y, `points[${index}].y`)
|
|
62
|
+
if (index > 0 && point.x !== points[index - 1]!.x && point.y !== points[index - 1]!.y) {
|
|
63
|
+
throw new RangeError("Spatial paths must be orthogonal")
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const cells = new Map<number, Set<number>>()
|
|
67
|
+
const add = (point: DiagramPoint): void => {
|
|
68
|
+
const row = cells.get(point.y) ?? new Set<number>()
|
|
69
|
+
row.add(point.x)
|
|
70
|
+
cells.set(point.y, row)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (points.length === 1) add(points[0]!)
|
|
74
|
+
for (const point of orthogonalPathPoints(points)) add(point)
|
|
75
|
+
|
|
76
|
+
return [...cells.entries()]
|
|
77
|
+
.sort(([left], [right]) => left - right)
|
|
78
|
+
.flatMap(([y, xs]) => {
|
|
79
|
+
const sorted = [...xs].sort((left, right) => left - right)
|
|
80
|
+
const [first, ...rest] = sorted
|
|
81
|
+
if (first === undefined) return []
|
|
82
|
+
const spans: SpatialSpan[] = []
|
|
83
|
+
let start = first
|
|
84
|
+
let end = first
|
|
85
|
+
for (const x of rest) {
|
|
86
|
+
if (x === end + 1) {
|
|
87
|
+
end = x
|
|
88
|
+
continue
|
|
89
|
+
}
|
|
90
|
+
spans.push(normalizedSpan(y, start, end))
|
|
91
|
+
start = x
|
|
92
|
+
end = x
|
|
93
|
+
}
|
|
94
|
+
spans.push(normalizedSpan(y, start, end))
|
|
95
|
+
return spans
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function spatialRectClaim(
|
|
100
|
+
id: string,
|
|
101
|
+
owner: string,
|
|
102
|
+
role: SpatialRole,
|
|
103
|
+
bounds: Pick<DiagramBounds, "left" | "top" | "width" | "height">,
|
|
104
|
+
): SpatialClaim {
|
|
105
|
+
return { id, owner, role, spans: spatialRectSpans(bounds) }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function spatialPathClaim(
|
|
109
|
+
id: string,
|
|
110
|
+
owner: string,
|
|
111
|
+
role: Extract<SpatialRole, "boundary" | "route">,
|
|
112
|
+
points: readonly DiagramPoint[],
|
|
113
|
+
): SpatialClaim {
|
|
114
|
+
return { id, owner, role, spans: spatialPathSpans(points) }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function compareClaims(left: SpatialClaim, right: SpatialClaim): number {
|
|
118
|
+
return left.id < right.id ? -1 : left.id > right.id ? 1 : 0
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function sameClaim(left: SpatialClaim, right: SpatialClaim): boolean {
|
|
122
|
+
return (
|
|
123
|
+
left.id === right.id &&
|
|
124
|
+
left.owner === right.owner &&
|
|
125
|
+
left.role === right.role &&
|
|
126
|
+
left.spans.length === right.spans.length &&
|
|
127
|
+
left.spans.every(
|
|
128
|
+
(span, index) =>
|
|
129
|
+
span.y === right.spans[index]!.y &&
|
|
130
|
+
span.fromX === right.spans[index]!.fromX &&
|
|
131
|
+
span.toX === right.spans[index]!.toX,
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function pointIsContact(point: DiagramPoint, existing: SpatialClaim, contacts: readonly SpatialContact[]): boolean {
|
|
137
|
+
return contacts.some(
|
|
138
|
+
(contact) =>
|
|
139
|
+
contact.owner === existing.owner &&
|
|
140
|
+
contact.points.some((candidate) => candidate.x === point.x && candidate.y === point.y),
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function rolesMayOverlap(moving: SpatialClaim, existing: SpatialClaim): boolean {
|
|
145
|
+
if (moving.owner === existing.owner) return true
|
|
146
|
+
return moving.role === "route" && existing.role === "route"
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function normalizeClearance(clearance: number | SpatialClearance | undefined): SpatialClearance {
|
|
150
|
+
const x = typeof clearance === "number" ? clearance : (clearance?.x ?? 0)
|
|
151
|
+
const y = typeof clearance === "number" ? clearance : (clearance?.y ?? 0)
|
|
152
|
+
assertFiniteInteger(x, "clearance.x")
|
|
153
|
+
assertFiniteInteger(y, "clearance.y")
|
|
154
|
+
if (x < 0 || y < 0) throw new RangeError("Spatial clearance cannot be negative")
|
|
155
|
+
return { x, y }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function inflateSpan(span: SpatialSpan, clearance: SpatialClearance): SpatialSpan {
|
|
159
|
+
return { y: span.y, fromX: span.fromX - clearance.x, toX: span.toX + clearance.x }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export class SpatialIndex {
|
|
163
|
+
static empty(): SpatialIndex {
|
|
164
|
+
return new SpatialIndex([])
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
readonly claims: readonly SpatialClaim[]
|
|
168
|
+
|
|
169
|
+
private constructor(claims: readonly SpatialClaim[]) {
|
|
170
|
+
this.claims = Object.freeze(
|
|
171
|
+
claims.map((claim) =>
|
|
172
|
+
Object.freeze({
|
|
173
|
+
...claim,
|
|
174
|
+
spans: Object.freeze(
|
|
175
|
+
[...claim.spans]
|
|
176
|
+
.map((span) => Object.freeze({ ...span }))
|
|
177
|
+
.sort((left, right) => left.y - right.y || left.fromX - right.fromX || left.toX - right.toX),
|
|
178
|
+
),
|
|
179
|
+
}),
|
|
180
|
+
),
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
add(...claims: readonly SpatialClaim[]): SpatialIndex {
|
|
185
|
+
return this.overlay(new SpatialIndex(claims))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
overlay(other: SpatialIndex): SpatialIndex {
|
|
189
|
+
const claims = new Map(this.claims.map((claim) => [claim.id, claim]))
|
|
190
|
+
for (const claim of other.claims) {
|
|
191
|
+
const existing = claims.get(claim.id)
|
|
192
|
+
if (existing && !sameClaim(existing, claim)) throw new Error(`Conflicting spatial claim id: ${claim.id}`)
|
|
193
|
+
claims.set(claim.id, claim)
|
|
194
|
+
}
|
|
195
|
+
return new SpatialIndex([...claims.values()].sort(compareClaims))
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
conflicts(moving: SpatialClaim, policy: SpatialCollisionPolicy = {}): SpatialConflict[] {
|
|
199
|
+
const contacts = policy.contacts ?? []
|
|
200
|
+
const conflicts: SpatialConflict[] = []
|
|
201
|
+
|
|
202
|
+
for (const existing of this.claims) {
|
|
203
|
+
if (rolesMayOverlap(moving, existing)) continue
|
|
204
|
+
const configuredClearance =
|
|
205
|
+
typeof policy.clearance === "number" || (policy.clearance && "x" in policy.clearance)
|
|
206
|
+
? policy.clearance
|
|
207
|
+
: policy.clearance?.[existing.role]
|
|
208
|
+
const clearance = normalizeClearance(configuredClearance)
|
|
209
|
+
for (const movingSpan of moving.spans) {
|
|
210
|
+
for (let dy = -clearance.y; dy <= clearance.y; dy++) {
|
|
211
|
+
const inflated = inflateSpan({ ...movingSpan, y: movingSpan.y + dy }, clearance)
|
|
212
|
+
for (const existingSpan of existing.spans) {
|
|
213
|
+
if (inflated.y !== existingSpan.y) continue
|
|
214
|
+
const fromX = Math.max(inflated.fromX, existingSpan.fromX)
|
|
215
|
+
const toX = Math.min(inflated.toX, existingSpan.toX)
|
|
216
|
+
for (let x = fromX; x <= toX; x++) {
|
|
217
|
+
const point = { x, y: inflated.y }
|
|
218
|
+
const movingOccupiesPoint = moving.spans.some(
|
|
219
|
+
(span) => span.y === point.y && point.x >= span.fromX && point.x <= span.toX,
|
|
220
|
+
)
|
|
221
|
+
if (!(movingOccupiesPoint && pointIsContact(point, existing, contacts))) {
|
|
222
|
+
conflicts.push({ moving, existing, point })
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return conflicts
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
isFree(claim: SpatialClaim, policy: SpatialCollisionPolicy = {}): boolean {
|
|
234
|
+
return this.conflicts(claim, policy).length === 0
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
firstFit<T extends { claim: SpatialClaim }>(
|
|
238
|
+
candidates: readonly T[],
|
|
239
|
+
policy: SpatialCollisionPolicy = {},
|
|
240
|
+
): T | undefined {
|
|
241
|
+
return candidates.find((candidate) => this.isFree(candidate.claim, policy))
|
|
242
|
+
}
|
|
243
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface DiagramTextRun {
|
|
2
|
+
text: string
|
|
3
|
+
italic: boolean
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface DiagramTextLine {
|
|
7
|
+
text: string
|
|
8
|
+
runs: DiagramTextRun[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function parseDiagramTextLines(value: string): DiagramTextLine[] {
|
|
12
|
+
return value.split(/<br\s*\/?>/i).map((line) => {
|
|
13
|
+
const runs: DiagramTextRun[] = []
|
|
14
|
+
const source = line.trim()
|
|
15
|
+
const tag = /<\/?(?:i|em)\s*>/gi
|
|
16
|
+
let italic = false
|
|
17
|
+
let offset = 0
|
|
18
|
+
|
|
19
|
+
for (const match of source.matchAll(tag)) {
|
|
20
|
+
if (match.index > offset) runs.push({ text: source.slice(offset, match.index), italic })
|
|
21
|
+
italic = !match[0].startsWith("</")
|
|
22
|
+
offset = match.index + match[0].length
|
|
23
|
+
}
|
|
24
|
+
if (offset < source.length) runs.push({ text: source.slice(offset), italic })
|
|
25
|
+
|
|
26
|
+
return { text: runs.map((run) => run.text).join(""), runs }
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function splitDiagramLines(value: string): string[] {
|
|
31
|
+
return parseDiagramTextLines(value).map((line) => line.text)
|
|
32
|
+
}
|
package/src/core/text.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import stringWidth from "string-width"
|
|
2
|
+
import { splitDiagramLines } from "./text-lines.js"
|
|
3
|
+
|
|
4
|
+
export { parseDiagramTextLines, splitDiagramLines } from "./text-lines.js"
|
|
5
|
+
|
|
6
|
+
export interface DiagramTextBoxSize {
|
|
7
|
+
width: number
|
|
8
|
+
height: number
|
|
9
|
+
lines: string[]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function diagramTextWidth(value: string): number {
|
|
13
|
+
return stringWidth(value)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const DIAGRAM_LABEL_PADDING_X = 1
|
|
17
|
+
|
|
18
|
+
export function padDiagramLabelLine(value: string): string {
|
|
19
|
+
const padding = " ".repeat(DIAGRAM_LABEL_PADDING_X)
|
|
20
|
+
return `${padding}${value}${padding}`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function measureDiagramLabelWidth(value: string, measure = diagramTextWidth): number {
|
|
24
|
+
return Math.max(...splitDiagramLines(value).map((line) => measure(line) + DIAGRAM_LABEL_PADDING_X * 2))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" })
|
|
28
|
+
|
|
29
|
+
export function* diagramTextGraphemes(value: string): Generator<string> {
|
|
30
|
+
for (const { segment } of graphemeSegmenter.segment(value)) yield segment
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function measureDiagramTextBox(
|
|
34
|
+
value: string,
|
|
35
|
+
options: { paddingX?: number; paddingY?: number; minInnerWidth?: number } = {},
|
|
36
|
+
): DiagramTextBoxSize {
|
|
37
|
+
const lines = splitDiagramLines(value)
|
|
38
|
+
const innerWidth = Math.max(...lines.map(diagramTextWidth), options.minInnerWidth ?? 1)
|
|
39
|
+
return {
|
|
40
|
+
width: innerWidth + (options.paddingX ?? 0) * 2,
|
|
41
|
+
height: lines.length + (options.paddingY ?? 0) * 2,
|
|
42
|
+
lines,
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/detect.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { MermaidDiagramKind } from "./diagnostics.js"
|
|
2
|
+
import { isMermaidFlowchartDiagram } from "./flowchart/parser.js"
|
|
3
|
+
import { isMermaidGanttDiagram } from "./gantt/parser.js"
|
|
4
|
+
import { isMermaidGitGraphDiagram } from "./gitgraph/parser.js"
|
|
5
|
+
import { isMermaidSequenceDiagram } from "./sequence/parser.js"
|
|
6
|
+
import { isMermaidStateDiagram } from "./state/parser.js"
|
|
7
|
+
import { isMermaidTimelineDiagram } from "./timeline/parser.js"
|
|
8
|
+
|
|
9
|
+
export function detectMermaidDiagram(content: string): MermaidDiagramKind | undefined {
|
|
10
|
+
if (isMermaidFlowchartDiagram(content)) return "flowchart"
|
|
11
|
+
if (isMermaidGanttDiagram(content)) return "gantt"
|
|
12
|
+
if (isMermaidGitGraphDiagram(content)) return "gitGraph"
|
|
13
|
+
if (isMermaidSequenceDiagram(content)) return "sequence"
|
|
14
|
+
if (isMermaidStateDiagram(content)) return "state"
|
|
15
|
+
if (isMermaidTimelineDiagram(content)) return "timeline"
|
|
16
|
+
return undefined
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type MermaidDiagramKind = "flowchart" | "sequence" | "state" | "timeline" | "gitGraph" | "gantt"
|
|
2
|
+
|
|
3
|
+
/** An otherwise valid diagram contains syntax that this renderer does not support. */
|
|
4
|
+
export class MermaidSyntaxError extends Error {
|
|
5
|
+
readonly _tag = "MermaidSyntaxError"
|
|
6
|
+
|
|
7
|
+
constructor(
|
|
8
|
+
readonly kind: MermaidDiagramKind,
|
|
9
|
+
readonly lineNumber: number,
|
|
10
|
+
readonly sourceLine: string,
|
|
11
|
+
reason = "Unsupported syntax",
|
|
12
|
+
) {
|
|
13
|
+
super(`${reason} in ${kind} diagram at line ${lineNumber}: "${sourceLine}"`)
|
|
14
|
+
this.name = "MermaidSyntaxError"
|
|
15
|
+
}
|
|
16
|
+
}
|