@zyro-sec/merman 0.0.0-stage → 1.0.2
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.test.ts +203 -0
- package/src/core/canvas.ts +228 -0
- package/src/core/color/style.test.ts +24 -0
- package/src/core/color/style.ts +56 -0
- package/src/core/drawing.test.ts +116 -0
- package/src/core/drawing.ts +301 -0
- package/src/core/geometry.test.ts +112 -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.test.ts +87 -0
- package/src/core/spatial.ts +243 -0
- package/src/core/text-lines.ts +32 -0
- package/src/core/text.test.ts +33 -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/flowchart.test.ts +2800 -0
- package/src/flowchart/labels.test.ts +127 -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.test.ts +368 -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.test.ts +124 -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.test.ts +183 -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/layout-audit.test.ts +92 -0
- package/src/markdown.ts +336 -0
- package/src/palette.test.ts +103 -0
- package/src/palette.ts +75 -0
- package/src/plugin.ts +15 -0
- package/src/sequence/diagram.test.ts +757 -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.test.ts +219 -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.test.ts +1386 -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.test.ts +314 -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.test.ts +693 -0
- package/src/state/routing.ts +1522 -0
- package/src/state/search.ts +103 -0
- package/src/state/style.test.ts +113 -0
- package/src/state/style.ts +52 -0
- package/src/state/types.ts +63 -0
- package/src/state/visible-model.test.ts +84 -0
- package/src/state/visible-model.ts +77 -0
- package/src/test/diagnostics.test.ts +129 -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/test/markdown.test.ts +474 -0
- package/src/timeline/diagram.test.ts +188 -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,347 @@
|
|
|
1
|
+
export type DiagramAxis = "x" | "y"
|
|
2
|
+
export type DiagramDirection = "up" | "down" | "left" | "right"
|
|
3
|
+
export type DiagramSide = "left" | "right" | "top" | "bottom"
|
|
4
|
+
|
|
5
|
+
export interface DiagramPoint {
|
|
6
|
+
x: number
|
|
7
|
+
y: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface DiagramBounds {
|
|
11
|
+
left: number
|
|
12
|
+
top: number
|
|
13
|
+
width: number
|
|
14
|
+
height: number
|
|
15
|
+
centerX: number
|
|
16
|
+
centerY: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface DiagramSegment {
|
|
20
|
+
from: DiagramPoint
|
|
21
|
+
to: DiagramPoint
|
|
22
|
+
axis: DiagramAxis
|
|
23
|
+
direction: DiagramDirection
|
|
24
|
+
length: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface DiagramLane {
|
|
28
|
+
axis: DiagramAxis
|
|
29
|
+
coordinate: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface DiagramSpan {
|
|
33
|
+
start: number
|
|
34
|
+
end: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const DIRECTION_AXIS = {
|
|
38
|
+
left: "x",
|
|
39
|
+
right: "x",
|
|
40
|
+
up: "y",
|
|
41
|
+
down: "y",
|
|
42
|
+
} as const satisfies Record<DiagramDirection, DiagramAxis>
|
|
43
|
+
|
|
44
|
+
const DIRECTION_SIGN = {
|
|
45
|
+
left: -1,
|
|
46
|
+
right: 1,
|
|
47
|
+
up: -1,
|
|
48
|
+
down: 1,
|
|
49
|
+
} as const satisfies Record<DiagramDirection, -1 | 1>
|
|
50
|
+
|
|
51
|
+
const DIRECTION_SIDE = {
|
|
52
|
+
left: "left",
|
|
53
|
+
right: "right",
|
|
54
|
+
up: "top",
|
|
55
|
+
down: "bottom",
|
|
56
|
+
} as const satisfies Record<DiagramDirection, DiagramSide>
|
|
57
|
+
|
|
58
|
+
const OPPOSITE_SIDE = {
|
|
59
|
+
left: "right",
|
|
60
|
+
right: "left",
|
|
61
|
+
top: "bottom",
|
|
62
|
+
bottom: "top",
|
|
63
|
+
} as const satisfies Record<DiagramSide, DiagramSide>
|
|
64
|
+
|
|
65
|
+
export function point(x: number, y: number): DiagramPoint {
|
|
66
|
+
return { x, y }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function diagramBoundsFromRect(left: number, top: number, width: number, height: number): DiagramBounds {
|
|
70
|
+
return {
|
|
71
|
+
left,
|
|
72
|
+
top,
|
|
73
|
+
width,
|
|
74
|
+
height,
|
|
75
|
+
centerX: left + Math.floor(width / 2),
|
|
76
|
+
centerY: top + Math.floor(height / 2),
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function translateDiagramBounds(bounds: DiagramBounds, dx: number, dy: number): void {
|
|
81
|
+
bounds.left += dx
|
|
82
|
+
bounds.top += dy
|
|
83
|
+
bounds.centerX += dx
|
|
84
|
+
bounds.centerY += dy
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function diagramBoundsFromBounds(bounds: readonly DiagramBounds[]): DiagramBounds | undefined {
|
|
88
|
+
if (bounds.length === 0) return undefined
|
|
89
|
+
const left = Math.min(...bounds.map((bound) => bound.left))
|
|
90
|
+
const top = Math.min(...bounds.map((bound) => bound.top))
|
|
91
|
+
const right = Math.max(...bounds.map((bound) => bound.left + bound.width))
|
|
92
|
+
const bottom = Math.max(...bounds.map((bound) => bound.top + bound.height))
|
|
93
|
+
return diagramBoundsFromRect(left, top, right - left, bottom - top)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function diagramBoundsFromPoints(points: readonly DiagramPoint[]): DiagramBounds | undefined {
|
|
97
|
+
if (points.length === 0) return undefined
|
|
98
|
+
const left = Math.min(...points.map((point) => point.x))
|
|
99
|
+
const top = Math.min(...points.map((point) => point.y))
|
|
100
|
+
const right = Math.max(...points.map((point) => point.x))
|
|
101
|
+
const bottom = Math.max(...points.map((point) => point.y))
|
|
102
|
+
return diagramBoundsFromRect(left, top, right - left + 1, bottom - top + 1)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function coordinate(point: DiagramPoint, axis: DiagramAxis): number {
|
|
106
|
+
return point[axis]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function withCoordinate(point: DiagramPoint, axis: DiagramAxis, value: number): DiagramPoint {
|
|
110
|
+
return axis === "x" ? { x: value, y: point.y } : { x: point.x, y: value }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function shiftPoint(point: DiagramPoint, direction: DiagramDirection, distance = 1): DiagramPoint {
|
|
114
|
+
switch (direction) {
|
|
115
|
+
case "left":
|
|
116
|
+
return { x: point.x - distance, y: point.y }
|
|
117
|
+
case "right":
|
|
118
|
+
return { x: point.x + distance, y: point.y }
|
|
119
|
+
case "up":
|
|
120
|
+
return { x: point.x, y: point.y - distance }
|
|
121
|
+
case "down":
|
|
122
|
+
return { x: point.x, y: point.y + distance }
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function clampPoint(point: DiagramPoint, min: DiagramPoint = { x: 0, y: 0 }): DiagramPoint {
|
|
127
|
+
return { x: Math.max(min.x, point.x), y: Math.max(min.y, point.y) }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function samePoint(left: DiagramPoint, right: DiagramPoint): boolean {
|
|
131
|
+
return left.x === right.x && left.y === right.y
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function directionAxis(direction: DiagramDirection): DiagramAxis {
|
|
135
|
+
return DIRECTION_AXIS[direction]
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function directionSign(direction: DiagramDirection): -1 | 1 {
|
|
139
|
+
return DIRECTION_SIGN[direction]
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function sideForDirection(direction: DiagramDirection): DiagramSide {
|
|
143
|
+
return DIRECTION_SIDE[direction]
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function oppositeSide(side: DiagramSide): DiagramSide {
|
|
147
|
+
return OPPOSITE_SIDE[side]
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function directionBetween(from: DiagramPoint, to: DiagramPoint): DiagramDirection | undefined {
|
|
151
|
+
if (from.y === to.y) {
|
|
152
|
+
if (to.x > from.x) return "right"
|
|
153
|
+
if (to.x < from.x) return "left"
|
|
154
|
+
}
|
|
155
|
+
if (from.x === to.x) {
|
|
156
|
+
if (to.y > from.y) return "down"
|
|
157
|
+
if (to.y < from.y) return "up"
|
|
158
|
+
}
|
|
159
|
+
return undefined
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function boundsCenter(bounds: DiagramBounds): DiagramPoint {
|
|
163
|
+
return point(bounds.centerX, bounds.centerY)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function boundsSidePoint(
|
|
167
|
+
bounds: DiagramBounds,
|
|
168
|
+
side: DiagramSide,
|
|
169
|
+
surface: "border" | "outside" = "outside",
|
|
170
|
+
): DiagramPoint {
|
|
171
|
+
switch (side) {
|
|
172
|
+
case "left":
|
|
173
|
+
return point(bounds.left - (surface === "outside" ? 1 : 0), bounds.centerY)
|
|
174
|
+
case "right":
|
|
175
|
+
return point(bounds.left + bounds.width - (surface === "outside" ? 0 : 1), bounds.centerY)
|
|
176
|
+
case "top":
|
|
177
|
+
return point(bounds.centerX, bounds.top - (surface === "outside" ? 1 : 0))
|
|
178
|
+
case "bottom":
|
|
179
|
+
return point(bounds.centerX, bounds.top + bounds.height - (surface === "outside" ? 0 : 1))
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function centerCoordinate(bounds: DiagramBounds, axis: DiagramAxis): number {
|
|
184
|
+
return axis === "x" ? bounds.centerX : bounds.centerY
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function snapCoordinate(source: number, target: number, tolerance: number): number {
|
|
188
|
+
return Math.abs(source - target) <= tolerance ? target : source
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function pathThrough(points: readonly DiagramPoint[]): DiagramPoint[] {
|
|
192
|
+
const path: DiagramPoint[] = []
|
|
193
|
+
for (const next of points) {
|
|
194
|
+
if (!path.length || !samePoint(path[path.length - 1]!, next)) path.push(point(next.x, next.y))
|
|
195
|
+
}
|
|
196
|
+
return path
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function lane(axis: DiagramAxis, coordinate: number): DiagramLane {
|
|
200
|
+
return { axis, coordinate }
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function pathViaLane(start: DiagramPoint, routeLane: DiagramLane, end: DiagramPoint): DiagramPoint[] {
|
|
204
|
+
return pathThrough([
|
|
205
|
+
start,
|
|
206
|
+
withCoordinate(start, routeLane.axis, routeLane.coordinate),
|
|
207
|
+
withCoordinate(end, routeLane.axis, routeLane.coordinate),
|
|
208
|
+
end,
|
|
209
|
+
])
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function dominantAxis(start: DiagramPoint, end: DiagramPoint): DiagramAxis {
|
|
213
|
+
return Math.abs(end.x - start.x) >= Math.abs(end.y - start.y) ? "x" : "y"
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function terminalLane(start: DiagramPoint, end: DiagramPoint, axis: DiagramAxis, terminalClearance: number): number {
|
|
217
|
+
const startCoordinate = coordinate(start, axis)
|
|
218
|
+
const endCoordinate = coordinate(end, axis)
|
|
219
|
+
const delta = endCoordinate - startCoordinate
|
|
220
|
+
const sign = Math.sign(delta)
|
|
221
|
+
if (sign === 0) return endCoordinate
|
|
222
|
+
return endCoordinate - sign * Math.min(terminalClearance, Math.max(1, Math.abs(delta) - 1))
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function orthogonalPath(
|
|
226
|
+
start: DiagramPoint,
|
|
227
|
+
end: DiagramPoint,
|
|
228
|
+
options: { preferredAxis?: DiagramAxis; terminalClearance?: number } = {},
|
|
229
|
+
): DiagramPoint[] {
|
|
230
|
+
if (start.x === end.x || start.y === end.y) return pathThrough([start, end])
|
|
231
|
+
|
|
232
|
+
const laneAxis = options.preferredAxis ?? dominantAxis(start, end)
|
|
233
|
+
return pathViaLane(start, lane(laneAxis, terminalLane(start, end, laneAxis, options.terminalClearance ?? 4)), end)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function segmentBetween(from: DiagramPoint, to: DiagramPoint): DiagramSegment | undefined {
|
|
237
|
+
const direction = directionBetween(from, to)
|
|
238
|
+
if (!direction) return undefined
|
|
239
|
+
return {
|
|
240
|
+
from,
|
|
241
|
+
to,
|
|
242
|
+
axis: directionAxis(direction),
|
|
243
|
+
direction,
|
|
244
|
+
length: Math.abs(coordinate(to, directionAxis(direction)) - coordinate(from, directionAxis(direction))),
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function walkOrthogonalSegment(
|
|
249
|
+
from: DiagramPoint,
|
|
250
|
+
to: DiagramPoint,
|
|
251
|
+
includeStart: boolean,
|
|
252
|
+
visit: (point: DiagramPoint) => boolean | void,
|
|
253
|
+
): void {
|
|
254
|
+
const direction = directionBetween(from, to)
|
|
255
|
+
if (!direction) return
|
|
256
|
+
|
|
257
|
+
const dx = direction === "right" ? 1 : direction === "left" ? -1 : 0
|
|
258
|
+
const dy = direction === "down" ? 1 : direction === "up" ? -1 : 0
|
|
259
|
+
let cursor = includeStart ? from : point(from.x + dx, from.y + dy)
|
|
260
|
+
|
|
261
|
+
while (!samePoint(cursor, to)) {
|
|
262
|
+
if (visit(cursor) === false) return
|
|
263
|
+
cursor = point(cursor.x + dx, cursor.y + dy)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function orthogonalPathPoints(points: readonly DiagramPoint[]): DiagramPoint[] {
|
|
268
|
+
const path: DiagramPoint[] = []
|
|
269
|
+
for (let index = 1; index < points.length; index++) {
|
|
270
|
+
const from = points[index - 1]!
|
|
271
|
+
const to = points[index]!
|
|
272
|
+
const direction = directionBetween(from, to)
|
|
273
|
+
if (!direction) continue
|
|
274
|
+
|
|
275
|
+
const dx = direction === "right" ? 1 : direction === "left" ? -1 : 0
|
|
276
|
+
const dy = direction === "down" ? 1 : direction === "up" ? -1 : 0
|
|
277
|
+
let cursor = path.length === 0 ? point(from.x, from.y) : point(from.x + dx, from.y + dy)
|
|
278
|
+
while (true) {
|
|
279
|
+
path.push(cursor)
|
|
280
|
+
if (samePoint(cursor, to)) break
|
|
281
|
+
cursor = point(cursor.x + dx, cursor.y + dy)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return path
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function orderedSpan(left: number, right: number): DiagramSpan {
|
|
288
|
+
return left <= right ? { start: left, end: right } : { start: right, end: left }
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function segmentSpan(segment: DiagramSegment): DiagramSpan {
|
|
292
|
+
return orderedSpan(coordinate(segment.from, segment.axis), coordinate(segment.to, segment.axis))
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function pointOnSegment(segment: DiagramSegment, coordinateValue: number): DiagramPoint {
|
|
296
|
+
return withCoordinate(segment.from, segment.axis, coordinateValue)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function insetSpan(span: DiagramSpan, amount: number): DiagramSpan {
|
|
300
|
+
return { start: span.start + amount, end: span.end - amount }
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function spanCapacity(span: DiagramSpan): number {
|
|
304
|
+
return Math.max(0, span.end - span.start + 1)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function centeredSpanStart(span: DiagramSpan, width: number): number {
|
|
308
|
+
return span.start + Math.floor((spanCapacity(span) - width) / 2)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function midpoint(span: DiagramSpan): number {
|
|
312
|
+
return Math.round((span.start + span.end) / 2)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function advanceCoordinate(origin: number, direction: DiagramDirection, distance: number): number {
|
|
316
|
+
return origin + directionSign(direction) * distance
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function beforeNearestCoordinate(
|
|
320
|
+
points: readonly DiagramPoint[],
|
|
321
|
+
axis: DiagramAxis,
|
|
322
|
+
direction: DiagramDirection,
|
|
323
|
+
clearance: number,
|
|
324
|
+
): number {
|
|
325
|
+
const coordinates = points.map((point) => coordinate(point, axis))
|
|
326
|
+
const nearest = directionSign(direction) > 0 ? Math.min(...coordinates) : Math.max(...coordinates)
|
|
327
|
+
return advanceCoordinate(nearest, direction, -clearance)
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function afterFarthestCoordinate(
|
|
331
|
+
points: readonly DiagramPoint[],
|
|
332
|
+
axis: DiagramAxis,
|
|
333
|
+
direction: DiagramDirection,
|
|
334
|
+
clearance: number,
|
|
335
|
+
): number {
|
|
336
|
+
const coordinates = points.map((point) => coordinate(point, axis))
|
|
337
|
+
const farthest = directionSign(direction) > 0 ? Math.max(...coordinates) : Math.min(...coordinates)
|
|
338
|
+
return advanceCoordinate(farthest, direction, clearance)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export function keepBefore(preferred: number, boundary: number, direction: DiagramDirection): number {
|
|
342
|
+
return directionSign(direction) > 0 ? Math.min(preferred, boundary) : Math.max(preferred, boundary)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export function keepAfter(preferred: number, boundary: number, direction: DiagramDirection): number {
|
|
346
|
+
return directionSign(direction) > 0 ? Math.max(preferred, boundary) : Math.min(preferred, boundary)
|
|
347
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export interface NumberedMermaidLine {
|
|
2
|
+
readonly lineNumber: number
|
|
3
|
+
readonly text: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function* numberedMermaidLines(content: string): Generator<NumberedMermaidLine> {
|
|
7
|
+
const lines = content.split(/\r?\n/)
|
|
8
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
9
|
+
yield { lineNumber: index + 1, text: lines[index]!.trim() }
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function* meaningfulNumberedMermaidLines(content: string): Generator<NumberedMermaidLine> {
|
|
14
|
+
for (const line of numberedMermaidLines(content)) {
|
|
15
|
+
if (line.text && !line.text.startsWith("%%")) yield line
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function firstMeaningfulMermaidLine(content: string): string | undefined {
|
|
20
|
+
for (const rawLine of content.split(/\r?\n/)) {
|
|
21
|
+
const line = rawLine.trim()
|
|
22
|
+
if (line && !line.startsWith("%%")) return line
|
|
23
|
+
}
|
|
24
|
+
return undefined
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function stripMermaidQuotes(value: string): string {
|
|
28
|
+
const trimmed = value.trim()
|
|
29
|
+
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
30
|
+
return decodeMermaidText(trimmed.slice(1, -1))
|
|
31
|
+
}
|
|
32
|
+
return decodeMermaidText(trimmed)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function decodeMermaidText(value: string): string {
|
|
36
|
+
return decodeHTMLStrict(value)
|
|
37
|
+
}
|
|
38
|
+
import { decodeHTMLStrict } from "entities"
|
|
@@ -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,87 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { SpatialIndex, spatialPathClaim, spatialRectClaim } from "./spatial.js"
|
|
3
|
+
|
|
4
|
+
const body = spatialRectClaim("body", "node:A", "body", { left: 2, top: 1, width: 4, height: 3 })
|
|
5
|
+
const label = spatialRectClaim("label", "edge:A-B", "label", { left: 8, top: 1, width: 5, height: 1 })
|
|
6
|
+
const route = spatialPathClaim("route", "edge:A-B", "route", [
|
|
7
|
+
{ x: 5, y: 2 },
|
|
8
|
+
{ x: 10, y: 2 },
|
|
9
|
+
])
|
|
10
|
+
|
|
11
|
+
describe("SpatialIndex", () => {
|
|
12
|
+
test("composition is associative, commutative, idempotent, and has an identity", () => {
|
|
13
|
+
const a = SpatialIndex.empty().add(body)
|
|
14
|
+
const b = SpatialIndex.empty().add(label)
|
|
15
|
+
const c = SpatialIndex.empty().add(route)
|
|
16
|
+
|
|
17
|
+
expect(SpatialIndex.empty().overlay(a).claims).toEqual(a.claims)
|
|
18
|
+
expect(a.overlay(b).claims).toEqual(b.overlay(a).claims)
|
|
19
|
+
expect(a.overlay(b).overlay(c).claims).toEqual(a.overlay(b.overlay(c)).claims)
|
|
20
|
+
expect(a.overlay(a).claims).toEqual(a.claims)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test("routes may share routes but cannot cross unrelated semantic bodies", () => {
|
|
24
|
+
const index = SpatialIndex.empty().add(body, route)
|
|
25
|
+
const crossingBody = spatialPathClaim("cross-body", "edge:C-D", "route", [
|
|
26
|
+
{ x: 0, y: 2 },
|
|
27
|
+
{ x: 8, y: 2 },
|
|
28
|
+
])
|
|
29
|
+
const crossingRoute = spatialPathClaim("cross-route", "edge:C-D", "route", [
|
|
30
|
+
{ x: 7, y: 0 },
|
|
31
|
+
{ x: 7, y: 4 },
|
|
32
|
+
])
|
|
33
|
+
|
|
34
|
+
expect(index.isFree(crossingBody)).toBe(false)
|
|
35
|
+
expect(index.isFree(crossingRoute)).toBe(true)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test("declared endpoint contacts do not permit contact elsewhere", () => {
|
|
39
|
+
const index = SpatialIndex.empty().add(body)
|
|
40
|
+
const candidate = spatialPathClaim("candidate", "edge:B-A", "route", [
|
|
41
|
+
{ x: 0, y: 2 },
|
|
42
|
+
{ x: 2, y: 2 },
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
expect(index.isFree(candidate)).toBe(false)
|
|
46
|
+
expect(index.isFree(candidate, { contacts: [{ owner: "node:A", points: [{ x: 2, y: 2 }] }] })).toBe(true)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test("firstFit chooses the first collision-free candidate", () => {
|
|
50
|
+
const index = SpatialIndex.empty().add(body)
|
|
51
|
+
const blocked = spatialRectClaim("blocked", "label:B", "label", { left: 3, top: 2, width: 2, height: 1 })
|
|
52
|
+
const clear = spatialRectClaim("clear", "label:B", "label", { left: 7, top: 2, width: 2, height: 1 })
|
|
53
|
+
|
|
54
|
+
expect(index.firstFit([{ claim: blocked }, { claim: clear }])?.claim.id).toBe("clear")
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test("clearance is symmetric in both axes", () => {
|
|
58
|
+
const index = SpatialIndex.empty().add(body)
|
|
59
|
+
const touchingRight = spatialRectClaim("right", "label:B", "label", { left: 6, top: 1, width: 2, height: 1 })
|
|
60
|
+
const touchingBelow = spatialRectClaim("below", "label:C", "label", { left: 2, top: 4, width: 2, height: 1 })
|
|
61
|
+
|
|
62
|
+
expect(index.isFree(touchingRight)).toBe(true)
|
|
63
|
+
expect(index.isFree(touchingRight, { clearance: 1 })).toBe(false)
|
|
64
|
+
expect(index.isFree(touchingBelow)).toBe(true)
|
|
65
|
+
expect(index.isFree(touchingBelow, { clearance: 1 })).toBe(false)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test("axis-specific clearance does not move unrelated rows", () => {
|
|
69
|
+
const index = SpatialIndex.empty().add(body)
|
|
70
|
+
const touchingRight = spatialRectClaim("right", "label:B", "label", { left: 6, top: 1, width: 2, height: 1 })
|
|
71
|
+
const touchingBelow = spatialRectClaim("below", "label:C", "label", { left: 2, top: 4, width: 2, height: 1 })
|
|
72
|
+
|
|
73
|
+
expect(index.isFree(touchingRight, { clearance: { x: 1, y: 0 } })).toBe(false)
|
|
74
|
+
expect(index.isFree(touchingBelow, { clearance: { x: 1, y: 0 } })).toBe(true)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test("rejects malformed geometry instead of weakening collision checks", () => {
|
|
78
|
+
expect(() => spatialRectClaim("zero", "node", "body", { left: 0, top: 0, width: 0, height: 1 })).toThrow()
|
|
79
|
+
expect(() =>
|
|
80
|
+
spatialPathClaim("diagonal", "edge", "route", [
|
|
81
|
+
{ x: 0, y: 0 },
|
|
82
|
+
{ x: 1, y: 1 },
|
|
83
|
+
]),
|
|
84
|
+
).toThrow()
|
|
85
|
+
expect(() => SpatialIndex.empty().add(body).isFree(label, { clearance: Number.POSITIVE_INFINITY })).toThrow()
|
|
86
|
+
})
|
|
87
|
+
})
|