A pure Rust crate for converting Mermaid diagram syntax to SVG.
Mermaid text → Parser → AST → Layout (dagre_rust) → SVG Renderer → SVG string
We use dagre_rust (a Rust port of dagre.js, the same layout engine mermaid.js uses) for graph layout, then render SVG directly. This gives us:
- Pure Rust — No external runtime dependencies (no Node.js, no headless browser)
- Same layout algorithms as mermaid.js — dagre_rust provides node positioning and edge routing
- Reasonable scope — We focus on parsing and rendering, delegating layout to dagre
- Graph direction:
graph TD,graph LR,graph TB,graph RL,graph BT - Node shapes: rectangle, rounded, stadium/pill, diamond, hexagon, asymmetric/flag, subroutine, cylinder, circle
- Edge types: arrow, line, dotted, thick — all with optional labels
- Subgraphs:
subgraph title ... end - Basic inline styling:
style A fill:#f9f,stroke:#333
These render something but are not yet at mermaid.js parity:
erDiagram,classDiagram,stateDiagram/stateDiagram-v2sequenceDiagram,mindmap,timeline,journeyquadrantChart,pie,gantt
use mermaid_to_svg::{render_mermaid_to_svg, MermaidTheme};
let mermaid = r#"
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[Action 1]
B -->|No| D[Action 2]
"#;
// With default theme
let svg = render_mermaid_to_svg(mermaid, None)?;
// With custom theme
let theme = MermaidTheme {
background: "#1e1e1e".into(),
node_fill: "#2d2d2d".into(),
node_stroke: "#888888".into(),
text_color: "#ffffff".into(),
edge_color: "#888888".into(),
};
let svg = render_mermaid_to_svg(mermaid, Some(&theme))?;# From a file
cargo run --bin render_mermaid -- diagram.mmd > output.svg
# From stdin
echo 'graph TD; A-->B' | cargo run --bin render_mermaid > output.svgSee docs/visual-comparison.md for a side-by-side comparison of our rendering output against the canonical mermaid-cli across 80 sample diagrams.
See docs/agent-visual-verification.md for the agent prompt and visual verification loop used to iterate against Mermaid reference output.
cargo test --libcargo testTo visually compare rendering output against the canonical mermaid CLI:
./scripts/visual_compare.shThis generates a static HTML page at output/comparison.html showing our SVG output side-by-side with the mermaid CLI reference for each sample diagram.
Snapshot tests use insta. To update snapshots:
cargo insta review./scripts/generate_reference_svgs.shlib.rs— Public APIparser.rs— Mermaid flowchart syntax parserast.rs— Abstract syntax tree typeslayout.rs— Uses dagre_rust for graph layoutsvg_renderer.rs— Converts layout result to SVGtheme.rs— Theme colors for light/dark mode supporterror.rs— Error types
MIT — see LICENSE.
This project includes code derived from mermaid.js, dagre.js, and dagre_rust. See THIRD_PARTY_NOTICES for details.
Warp requires contributors to sign a contributor license agreement (CLA) before their contributions can be merged. You can read and sign our CLA at https://cla.warp.dev.