@oh-my-pi/pi-utils 16.0.7 → 16.0.9
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/CHANGELOG.md +10 -0
- package/dist/types/mermaid-ascii.d.ts +1 -1
- package/dist/types/vendor/mermaid-ascii/ascii/ansi.d.ts +41 -0
- package/dist/types/vendor/mermaid-ascii/ascii/canvas.d.ts +89 -0
- package/dist/types/vendor/mermaid-ascii/ascii/class-diagram.d.ts +7 -0
- package/dist/types/vendor/mermaid-ascii/ascii/converter.d.ts +12 -0
- package/dist/types/vendor/mermaid-ascii/ascii/draw.d.ts +66 -0
- package/dist/types/vendor/mermaid-ascii/ascii/edge-bundling.d.ts +48 -0
- package/dist/types/vendor/mermaid-ascii/ascii/edge-routing.d.ts +43 -0
- package/dist/types/vendor/mermaid-ascii/ascii/er-diagram.d.ts +7 -0
- package/dist/types/vendor/mermaid-ascii/ascii/grid.d.ts +56 -0
- package/dist/types/vendor/mermaid-ascii/ascii/index.d.ts +65 -0
- package/dist/types/vendor/mermaid-ascii/ascii/multiline-utils.d.ts +27 -0
- package/dist/types/vendor/mermaid-ascii/ascii/pathfinder.d.ts +17 -0
- package/dist/types/vendor/mermaid-ascii/ascii/sequence.d.ts +7 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/circle.d.ts +11 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/corners.d.ts +34 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/diamond.d.ts +11 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/hexagon.d.ts +11 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/index.d.ts +26 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/rectangle.d.ts +31 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/rounded.d.ts +11 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/special.d.ts +59 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/stadium.d.ts +17 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/state.d.ts +30 -0
- package/dist/types/vendor/mermaid-ascii/ascii/shapes/types.d.ts +55 -0
- package/dist/types/vendor/mermaid-ascii/ascii/types.d.ts +206 -0
- package/dist/types/vendor/mermaid-ascii/ascii/validate.d.ts +51 -0
- package/dist/types/vendor/mermaid-ascii/ascii/xychart.d.ts +2 -0
- package/dist/types/vendor/mermaid-ascii/class/parser.d.ts +6 -0
- package/dist/types/vendor/mermaid-ascii/class/types.d.ts +102 -0
- package/dist/types/vendor/mermaid-ascii/er/parser.d.ts +6 -0
- package/dist/types/vendor/mermaid-ascii/er/types.d.ts +76 -0
- package/dist/types/vendor/mermaid-ascii/index.d.ts +1 -0
- package/dist/types/vendor/mermaid-ascii/multiline-utils.d.ts +9 -0
- package/dist/types/vendor/mermaid-ascii/parser.d.ts +7 -0
- package/dist/types/vendor/mermaid-ascii/sequence/parser.d.ts +6 -0
- package/dist/types/vendor/mermaid-ascii/sequence/types.d.ts +130 -0
- package/dist/types/vendor/mermaid-ascii/text-metrics.d.ts +21 -0
- package/dist/types/vendor/mermaid-ascii/types.d.ts +114 -0
- package/dist/types/vendor/mermaid-ascii/xychart/colors.d.ts +25 -0
- package/dist/types/vendor/mermaid-ascii/xychart/parser.d.ts +6 -0
- package/dist/types/vendor/mermaid-ascii/xychart/types.d.ts +145 -0
- package/package.json +2 -3
- package/src/mermaid-ascii.ts +1 -1
- package/src/vendor/mermaid-ascii/NOTICE +33 -0
- package/src/vendor/mermaid-ascii/ascii/ansi.ts +409 -0
- package/src/vendor/mermaid-ascii/ascii/canvas.ts +476 -0
- package/src/vendor/mermaid-ascii/ascii/class-diagram.ts +699 -0
- package/src/vendor/mermaid-ascii/ascii/converter.ts +271 -0
- package/src/vendor/mermaid-ascii/ascii/draw.ts +1382 -0
- package/src/vendor/mermaid-ascii/ascii/edge-bundling.ts +328 -0
- package/src/vendor/mermaid-ascii/ascii/edge-routing.ts +297 -0
- package/src/vendor/mermaid-ascii/ascii/er-diagram.ts +441 -0
- package/src/vendor/mermaid-ascii/ascii/grid.ts +578 -0
- package/src/vendor/mermaid-ascii/ascii/index.ts +187 -0
- package/src/vendor/mermaid-ascii/ascii/multiline-utils.ts +78 -0
- package/src/vendor/mermaid-ascii/ascii/pathfinder.ts +215 -0
- package/src/vendor/mermaid-ascii/ascii/sequence.ts +460 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/circle.ts +27 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/corners.ts +127 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/diamond.ts +27 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/hexagon.ts +27 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/index.ts +101 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/rectangle.ts +175 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/rounded.ts +27 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/special.ts +296 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/stadium.ts +114 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/state.ts +192 -0
- package/src/vendor/mermaid-ascii/ascii/shapes/types.ts +73 -0
- package/src/vendor/mermaid-ascii/ascii/types.ts +273 -0
- package/src/vendor/mermaid-ascii/ascii/validate.ts +120 -0
- package/src/vendor/mermaid-ascii/ascii/xychart.ts +875 -0
- package/src/vendor/mermaid-ascii/class/parser.ts +290 -0
- package/src/vendor/mermaid-ascii/class/types.ts +121 -0
- package/src/vendor/mermaid-ascii/er/parser.ts +181 -0
- package/src/vendor/mermaid-ascii/er/types.ts +91 -0
- package/src/vendor/mermaid-ascii/index.ts +14 -0
- package/src/vendor/mermaid-ascii/multiline-utils.ts +30 -0
- package/src/vendor/mermaid-ascii/parser.ts +645 -0
- package/src/vendor/mermaid-ascii/sequence/parser.ts +207 -0
- package/src/vendor/mermaid-ascii/sequence/types.ts +146 -0
- package/src/vendor/mermaid-ascii/text-metrics.ts +71 -0
- package/src/vendor/mermaid-ascii/types.ts +164 -0
- package/src/vendor/mermaid-ascii/xychart/colors.ts +140 -0
- package/src/vendor/mermaid-ascii/xychart/parser.ts +115 -0
- package/src/vendor/mermaid-ascii/xychart/types.ts +150 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// ASCII renderer — MermaidGraph → AsciiGraph converter
|
|
3
|
+
//
|
|
4
|
+
// Bridges the existing TypeScript parser output to the ASCII renderer's
|
|
5
|
+
// internal graph structure. This avoids maintaining a separate parser
|
|
6
|
+
// for ASCII rendering — we reuse parseMermaid() and convert its output.
|
|
7
|
+
// ============================================================================
|
|
8
|
+
|
|
9
|
+
import type { MermaidGraph, MermaidSubgraph } from '../types'
|
|
10
|
+
import type {
|
|
11
|
+
AsciiGraph, AsciiNode, AsciiEdge, AsciiSubgraph, AsciiConfig,
|
|
12
|
+
} from './types'
|
|
13
|
+
import { EMPTY_STYLE } from './types'
|
|
14
|
+
import { mkCanvas, mkRoleCanvas } from './canvas'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Convert a parsed MermaidGraph into an AsciiGraph ready for grid layout.
|
|
18
|
+
*
|
|
19
|
+
* Key mappings:
|
|
20
|
+
* - MermaidGraph.nodes (Map) → ordered AsciiNode[] preserving insertion order
|
|
21
|
+
* - MermaidGraph.edges → AsciiEdge[] with resolved node references
|
|
22
|
+
* - MermaidGraph.subgraphs → AsciiSubgraph[] with parent/child tree
|
|
23
|
+
* - Node labels are used as display names (not raw IDs)
|
|
24
|
+
*/
|
|
25
|
+
export function convertToAsciiGraph(parsed: MermaidGraph, config: AsciiConfig): AsciiGraph {
|
|
26
|
+
// Build node list preserving Map insertion order
|
|
27
|
+
const nodeMap = new Map<string, AsciiNode>()
|
|
28
|
+
let index = 0
|
|
29
|
+
|
|
30
|
+
for (const [id, mNode] of parsed.nodes) {
|
|
31
|
+
const asciiNode: AsciiNode = {
|
|
32
|
+
// Use the parser ID as the unique identity key to avoid collisions
|
|
33
|
+
// when multiple nodes share the same label (e.g. A[Web Server], C[Web Server]).
|
|
34
|
+
name: id,
|
|
35
|
+
// The label is used for rendering inside the box.
|
|
36
|
+
displayLabel: mNode.label,
|
|
37
|
+
// Preserve shape from parser for shape-aware rendering
|
|
38
|
+
shape: mNode.shape,
|
|
39
|
+
index,
|
|
40
|
+
gridCoord: null,
|
|
41
|
+
drawingCoord: null,
|
|
42
|
+
drawing: null,
|
|
43
|
+
drawn: false,
|
|
44
|
+
styleClassName: '',
|
|
45
|
+
styleClass: EMPTY_STYLE,
|
|
46
|
+
}
|
|
47
|
+
nodeMap.set(id, asciiNode)
|
|
48
|
+
index++
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const nodes = [...nodeMap.values()]
|
|
52
|
+
|
|
53
|
+
// Build edges with resolved node references
|
|
54
|
+
const edges: AsciiEdge[] = []
|
|
55
|
+
for (const mEdge of parsed.edges) {
|
|
56
|
+
const from = nodeMap.get(mEdge.source)
|
|
57
|
+
const to = nodeMap.get(mEdge.target)
|
|
58
|
+
if (!from || !to) continue
|
|
59
|
+
|
|
60
|
+
edges.push({
|
|
61
|
+
from,
|
|
62
|
+
to,
|
|
63
|
+
text: mEdge.label ?? '',
|
|
64
|
+
path: [],
|
|
65
|
+
labelLine: [],
|
|
66
|
+
startDir: { x: 0, y: 0 },
|
|
67
|
+
endDir: { x: 0, y: 0 },
|
|
68
|
+
style: mEdge.style,
|
|
69
|
+
hasArrowStart: mEdge.hasArrowStart,
|
|
70
|
+
hasArrowEnd: mEdge.hasArrowEnd,
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Convert subgraphs recursively
|
|
75
|
+
const subgraphs: AsciiSubgraph[] = []
|
|
76
|
+
for (const mSg of parsed.subgraphs) {
|
|
77
|
+
convertSubgraph(mSg, null, nodeMap, subgraphs)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Deduplicate subgraph node membership to match Go parser behavior.
|
|
81
|
+
// In Go, a node belongs only to the subgraph where it was FIRST DEFINED.
|
|
82
|
+
// The TS parser adds referenced nodes to all subgraphs they appear in,
|
|
83
|
+
// which causes incorrect bounding boxes when nodes span subgraph boundaries.
|
|
84
|
+
deduplicateSubgraphNodes(parsed.subgraphs, subgraphs, nodeMap, parsed)
|
|
85
|
+
|
|
86
|
+
// Apply class definitions
|
|
87
|
+
for (const [nodeId, className] of parsed.classAssignments) {
|
|
88
|
+
const node = nodeMap.get(nodeId)
|
|
89
|
+
const classDef = parsed.classDefs.get(className)
|
|
90
|
+
if (node && classDef) {
|
|
91
|
+
node.styleClassName = className
|
|
92
|
+
node.styleClass = { name: className, styles: classDef }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
nodes,
|
|
98
|
+
edges,
|
|
99
|
+
canvas: mkCanvas(0, 0),
|
|
100
|
+
roleCanvas: mkRoleCanvas(0, 0),
|
|
101
|
+
grid: new Map(),
|
|
102
|
+
columnWidth: new Map(),
|
|
103
|
+
rowHeight: new Map(),
|
|
104
|
+
subgraphs,
|
|
105
|
+
config,
|
|
106
|
+
offsetX: 0,
|
|
107
|
+
offsetY: 0,
|
|
108
|
+
bundles: [], // Populated by analyzeEdgeBundles() during layout
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Recursively convert a MermaidSubgraph to AsciiSubgraph.
|
|
114
|
+
* Flattens the tree into the subgraphs array while maintaining parent/child references.
|
|
115
|
+
* This matches the Go implementation where all subgraphs are in a flat list
|
|
116
|
+
* but linked via parent/children pointers.
|
|
117
|
+
*/
|
|
118
|
+
function convertSubgraph(
|
|
119
|
+
mSg: MermaidSubgraph,
|
|
120
|
+
parent: AsciiSubgraph | null,
|
|
121
|
+
nodeMap: Map<string, AsciiNode>,
|
|
122
|
+
allSubgraphs: AsciiSubgraph[],
|
|
123
|
+
): AsciiSubgraph {
|
|
124
|
+
// Normalize subgraph direction: BT→TD, RL→LR (same as root graph normalization)
|
|
125
|
+
let normalizedDirection: 'LR' | 'TD' | undefined
|
|
126
|
+
if (mSg.direction) {
|
|
127
|
+
normalizedDirection = (mSg.direction === 'LR' || mSg.direction === 'RL') ? 'LR' : 'TD'
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const sg: AsciiSubgraph = {
|
|
131
|
+
name: mSg.label,
|
|
132
|
+
nodes: [],
|
|
133
|
+
parent,
|
|
134
|
+
children: [],
|
|
135
|
+
minX: 0, minY: 0, maxX: 0, maxY: 0,
|
|
136
|
+
direction: normalizedDirection,
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Resolve node references
|
|
140
|
+
for (const nodeId of mSg.nodeIds) {
|
|
141
|
+
const node = nodeMap.get(nodeId)
|
|
142
|
+
if (node) sg.nodes.push(node)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
allSubgraphs.push(sg)
|
|
146
|
+
|
|
147
|
+
// Recurse into children
|
|
148
|
+
for (const childMSg of mSg.children) {
|
|
149
|
+
const child = convertSubgraph(childMSg, sg, nodeMap, allSubgraphs)
|
|
150
|
+
sg.children.push(child)
|
|
151
|
+
|
|
152
|
+
// Child nodes are also part of parent subgraphs (Go behavior).
|
|
153
|
+
// The Go parser adds nodes to ALL subgraphs in the stack, so a nested
|
|
154
|
+
// node belongs to both the inner and outer subgraph.
|
|
155
|
+
for (const childNode of child.nodes) {
|
|
156
|
+
if (!sg.nodes.includes(childNode)) {
|
|
157
|
+
sg.nodes.push(childNode)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return sg
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Deduplicate subgraph node membership to match Go parser behavior.
|
|
167
|
+
*
|
|
168
|
+
* The Go parser only adds a node to the subgraph that was active when the node
|
|
169
|
+
* was FIRST CREATED. If a node is later referenced inside a different subgraph,
|
|
170
|
+
* it is NOT added to that subgraph. The TS parser is more permissive — it adds
|
|
171
|
+
* referenced nodes to whichever subgraph they appear in.
|
|
172
|
+
*
|
|
173
|
+
* This function fixes the discrepancy by:
|
|
174
|
+
* 1. Walking the edges to determine which nodes were first created inside each subgraph
|
|
175
|
+
* 2. Removing nodes from subgraphs where they weren't first created
|
|
176
|
+
*/
|
|
177
|
+
function deduplicateSubgraphNodes(
|
|
178
|
+
mermaidSubgraphs: MermaidSubgraph[],
|
|
179
|
+
asciiSubgraphs: AsciiSubgraph[],
|
|
180
|
+
nodeMap: Map<string, AsciiNode>,
|
|
181
|
+
parsed: MermaidGraph,
|
|
182
|
+
): void {
|
|
183
|
+
// Build a map from MermaidSubgraph to its corresponding AsciiSubgraph.
|
|
184
|
+
// The ordering matches since we convert them in the same order.
|
|
185
|
+
const sgMap = new Map<MermaidSubgraph, AsciiSubgraph>()
|
|
186
|
+
buildSgMap(mermaidSubgraphs, asciiSubgraphs, sgMap)
|
|
187
|
+
|
|
188
|
+
// Determine which subgraph each node was "first defined" in.
|
|
189
|
+
// A node is first defined in the subgraph where it first appears as a NEW node
|
|
190
|
+
// in the ordered edge/node list. We approximate this by checking the global
|
|
191
|
+
// node insertion order against subgraph membership.
|
|
192
|
+
const nodeOwner = new Map<string, AsciiSubgraph>() // nodeId → owning subgraph
|
|
193
|
+
|
|
194
|
+
// Walk all mermaid subgraphs in document order. For each subgraph,
|
|
195
|
+
// claim nodes that haven't been claimed yet by any previous subgraph.
|
|
196
|
+
function claimNodes(mSg: MermaidSubgraph): void {
|
|
197
|
+
const asciiSg = sgMap.get(mSg)
|
|
198
|
+
if (!asciiSg) return
|
|
199
|
+
|
|
200
|
+
// Recurse into children first (they appear before parent in the Go parser stack,
|
|
201
|
+
// but nodes defined in children are added to parent too — this is handled by
|
|
202
|
+
// the convertSubgraph function which propagates child nodes to parents).
|
|
203
|
+
// For dedup, we process children first so their claims propagate up correctly.
|
|
204
|
+
for (const child of mSg.children) {
|
|
205
|
+
claimNodes(child)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Claim unclaimed nodes in this subgraph
|
|
209
|
+
for (const nodeId of mSg.nodeIds) {
|
|
210
|
+
if (!nodeOwner.has(nodeId)) {
|
|
211
|
+
nodeOwner.set(nodeId, asciiSg)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
for (const mSg of mermaidSubgraphs) {
|
|
217
|
+
claimNodes(mSg)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Now remove nodes from subgraphs that don't own them.
|
|
221
|
+
// A node should remain in: its owner subgraph + all ancestors of the owner.
|
|
222
|
+
for (const asciiSg of asciiSubgraphs) {
|
|
223
|
+
asciiSg.nodes = asciiSg.nodes.filter(node => {
|
|
224
|
+
// Find this node's ID in the nodeMap
|
|
225
|
+
let nodeId: string | undefined
|
|
226
|
+
for (const [id, n] of nodeMap) {
|
|
227
|
+
if (n === node) { nodeId = id; break }
|
|
228
|
+
}
|
|
229
|
+
if (!nodeId) return false
|
|
230
|
+
|
|
231
|
+
const owner = nodeOwner.get(nodeId)
|
|
232
|
+
if (!owner) return true // not in any subgraph claim — keep as-is
|
|
233
|
+
|
|
234
|
+
// Keep the node if this subgraph is the owner or an ancestor of the owner
|
|
235
|
+
return isAncestorOrSelf(asciiSg, owner)
|
|
236
|
+
})
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Check if `candidate` is the same as or an ancestor of `target`. */
|
|
241
|
+
function isAncestorOrSelf(candidate: AsciiSubgraph, target: AsciiSubgraph): boolean {
|
|
242
|
+
let current: AsciiSubgraph | null = target
|
|
243
|
+
while (current !== null) {
|
|
244
|
+
if (current === candidate) return true
|
|
245
|
+
current = current.parent
|
|
246
|
+
}
|
|
247
|
+
return false
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Build a mapping from MermaidSubgraph → AsciiSubgraph (matching by position). */
|
|
251
|
+
function buildSgMap(
|
|
252
|
+
mSgs: MermaidSubgraph[],
|
|
253
|
+
aSgs: AsciiSubgraph[],
|
|
254
|
+
result: Map<MermaidSubgraph, AsciiSubgraph>,
|
|
255
|
+
): void {
|
|
256
|
+
// The asciiSubgraphs array is flat (all subgraphs including nested ones),
|
|
257
|
+
// while mermaidSubgraphs is hierarchical. We need to flatten the mermaid tree
|
|
258
|
+
// in the same order the converter processes them (pre-order DFS).
|
|
259
|
+
const flatMermaid: MermaidSubgraph[] = []
|
|
260
|
+
function flatten(sgs: MermaidSubgraph[]): void {
|
|
261
|
+
for (const sg of sgs) {
|
|
262
|
+
flatMermaid.push(sg)
|
|
263
|
+
flatten(sg.children)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
flatten(mSgs)
|
|
267
|
+
|
|
268
|
+
for (let i = 0; i < flatMermaid.length && i < aSgs.length; i++) {
|
|
269
|
+
result.set(flatMermaid[i]!, aSgs[i]!)
|
|
270
|
+
}
|
|
271
|
+
}
|