to_mermaid
Stable function · Graphs
Tier: stable · Kind: function · Topic: Graphs
| Julia | TypeScript | Python | Rust | Go |
|---|---|---|---|---|
to_mermaid | toMermaid | to_mermaid | to_mermaid | ToMermaid |
Julia
to_mermaid(graph::Graph{ComponentNode, CouplingEdge})
to_mermaid(graph::Graph{VariableNode, DependencyEdge})
Export graph to Mermaid format for markdown embedding.
Node ids are sanitized to plain identifiers (model.x → model_x) and
labels are quoted, so dotted/scoped names render correctly. Edges carry a
quoted label — the coupling label (component graph) or dependency
relationship (expression graph) — matching the DOT emitter and Mermaid’s
A -->|"label"| B labeled-edge syntax.
Example (component_graph)
graph = component_graph(doc)
@assert [n.name for n in graph.nodes] == ["Heater", "Box"]
mermaid = to_mermaid(graph)
println(mermaid)
@assert occursin("Heater -.->|\"T\"| Box", mermaid)
@assert startswith(to_dot(graph), "digraph")
TypeScript
function toMermaid<N extends object, E>(graph: Graph<N, E>): string
Export graph as Mermaid flowchart format for Markdown embedding.
The header is graph TD. §4.8.3 requires a Mermaid export and specifies no
syntax for it, so the cross-binding tie-break is the majority, applied to the
keyword and the direction independently: graph beats flowchart 4-1
(Python, Go, Rust and Julia against this binding) and TD beats LR 3-2
(this binding, Python and Julia against Go and Rust). graph is Mermaid’s
legacy spelling of flowchart; both render, and the majority points at
graph. tests/conformance/graph/cases.json pins it.
Example (component_graph)
const doc = loadPath(fixture('coupled.esm'))
const graph = componentGraph(doc)
assert.deepEqual(graph.nodes.map((n) => n.id), ['Heater', 'Box'])
assert.deepEqual(graph.successors('Heater'), ['Box'])
const [edge] = graph.edges
assert.equal(edge.data.type, 'variable_map')
// Render for a Markdown page or for Graphviz.
const mermaid = toMermaid(graph)
assert.match(mermaid, /Heater -\.->\|T\| Box/)
assert.match(toDot(graph), /"Heater" -> "Box"/)
Python
to_mermaid(graph: Graph[Any, Any]) -> str
Render graph as a Mermaid flowchart for Markdown embedding.
Node ids are sanitized to plain identifiers (Chem.O3 → Chem_O3) and
labels are quoted, so scoped names render correctly. Edges carry a quoted
label — the coupling label for a component graph, the dependency
relationship for an expression graph.
Rust
pub fn to_mermaid<G: Graph>(graph: &G) -> String
Render graph as Mermaid (esm-libraries-spec §4.8.3). See to_dot
.
Example (component_graph)
let doc = load_path(fixture("coupled.esm"))?;
let graph = component_graph(&doc);
let ids: Vec<&str> = graph.nodes.iter().map(|n| n.id.as_str()).collect();
assert_eq!(ids, ["Box", "Heater"]);
assert_eq!(graph.edges.len(), 1);
assert_eq!((graph.edges[0].from.as_str(), graph.edges[0].to.as_str()), ("Heater", "Box"));
let dot = to_dot(&graph);
assert!(dot.starts_with("digraph"));
assert!(to_mermaid(&graph).contains("Heater"));