@ktrysmt/beautiful-mermaid 1.5.0 → 1.5.1
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/dist/index.js +35 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/parser.test.ts +37 -0
- package/src/parser.ts +62 -0
package/dist/index.js
CHANGED
|
@@ -481,8 +481,43 @@ function parseFlowchart(lines) {
|
|
|
481
481
|
}
|
|
482
482
|
parseEdgeLine(line, graph, subgraphStack);
|
|
483
483
|
}
|
|
484
|
+
resolveSubgraphEdgeEndpoints(graph);
|
|
484
485
|
return graph;
|
|
485
486
|
}
|
|
487
|
+
function resolveSubgraphEdgeEndpoints(graph) {
|
|
488
|
+
const representatives = /* @__PURE__ */ new Map();
|
|
489
|
+
function pickRepresentative(sg) {
|
|
490
|
+
for (const id of sg.nodeIds) {
|
|
491
|
+
if (graph.nodes.has(id)) return id;
|
|
492
|
+
}
|
|
493
|
+
for (const child of sg.children) {
|
|
494
|
+
const childRep = representatives.get(child.id) ?? pickRepresentative(child);
|
|
495
|
+
if (childRep) return childRep;
|
|
496
|
+
}
|
|
497
|
+
return void 0;
|
|
498
|
+
}
|
|
499
|
+
function visit(sg) {
|
|
500
|
+
for (const child of sg.children) visit(child);
|
|
501
|
+
const rep = pickRepresentative(sg);
|
|
502
|
+
if (rep) representatives.set(sg.id, rep);
|
|
503
|
+
}
|
|
504
|
+
for (const sg of graph.subgraphs) visit(sg);
|
|
505
|
+
if (representatives.size === 0) return;
|
|
506
|
+
for (const edge of graph.edges) {
|
|
507
|
+
const newSource = representatives.get(edge.source);
|
|
508
|
+
if (newSource) edge.source = newSource;
|
|
509
|
+
const newTarget = representatives.get(edge.target);
|
|
510
|
+
if (newTarget) edge.target = newTarget;
|
|
511
|
+
}
|
|
512
|
+
for (const sgId of representatives.keys()) {
|
|
513
|
+
graph.nodes.delete(sgId);
|
|
514
|
+
}
|
|
515
|
+
function scrubMembership(sg) {
|
|
516
|
+
sg.nodeIds = sg.nodeIds.filter((id) => !representatives.has(id));
|
|
517
|
+
for (const child of sg.children) scrubMembership(child);
|
|
518
|
+
}
|
|
519
|
+
for (const sg of graph.subgraphs) scrubMembership(sg);
|
|
520
|
+
}
|
|
486
521
|
function parseStateDiagram(lines) {
|
|
487
522
|
const graph = {
|
|
488
523
|
direction: "TD",
|