Skip to main content

Crate mermaid_text

Crate mermaid_text 

Source
Expand description

§mermaid-text

Render Mermaid graph/flowchart diagrams as Unicode box-drawing text — no browser, no image protocol, pure Rust. Intended for use in terminals, SSH sessions, CI logs, and any context where a visual diagram is useful but image rendering is unavailable. The output is deterministic and structured, making it suitable for LLM agents that need to read and reason about diagrams.

§ASCII mode

For terminals that do not support Unicode box-drawing characters (old SSH boxes, CI log viewers, fonts without the Box Drawing block), an ASCII-only rendering mode is available. The Unicode renderer runs first and its output is then post-processed by a character-by-character substitution table that maps every non-ASCII glyph to a plain + - | > < v ^ * o x equivalent.

let out = mermaid_text::render_ascii("graph LR; A[Build] --> B[Deploy]").unwrap();
assert!(out.contains("Build"));
assert!(out.contains("Deploy"));
// Every character in the output is plain ASCII.
assert!(out.is_ascii());

§Quick start

use mermaid_text::render;

let src = "graph LR; A[Build] --> B[Test] --> C[Deploy]";
let output = render(src).unwrap();
assert!(output.contains("Build"));
assert!(output.contains("Test"));
assert!(output.contains("Deploy"));
// The output is a multi-line Unicode string ready for printing.
println!("{output}");

§Width-constrained rendering

Pass an optional column budget so the renderer tries progressively smaller gap sizes until the output fits:

use mermaid_text::render_with_width;

let output = render_with_width(
    "graph LR; A[Start] --> B[End]",
    Some(80),
).unwrap();
assert!(output.contains("Start"));

§Feature matrix

FeatureSupported
graph LR/TD/RL/BT and flowchart keywordyes
Rectangle, rounded, diamond, circle nodesyes
Stadium, subroutine, cylinder, hexagon nodesyes
Asymmetric, parallelogram, trapezoid, double-circle nodesyes
Solid -->, plain ---, dotted -.->, thick ==> edgesyes
Bidirectional <-->, circle --o, cross --x edgesyes
Edge labels (|label| and -- label --> forms)yes
Subgraphs with nested subgraphsyes
Per-subgraph direction overridepartial (see Limitations)
Width-constrained compactionyes
A* obstacle-aware edge routing (incl. back-edge perimeter routing)yes
Junction merging (┼ ├ ┤ ┬ ┴)yes
style, classDef, click, linkStyle directivessilently ignored
sequenceDiagram (participants, ->>, -->>, ->, -->)yes
pie (with optional showData and title)yes (rendered as horizontal bar chart)
erDiagram (entities + relationships with cardinality)yes (Phase 1 — name-only boxes)
journey (user-journey, section/task tree with score bars)yes
gantt (project schedule bar chart)yes (Phase 1 — bar chart, no excludes/status tags/milestones)
timeline (vertical time-period bullet list)yes (Phase 1 — title, sections, multi-event periods; no custom themes)
gitGraph (branch/commit lane diagram)yes (Phase 1 — normal/merge/cherry-pick commits; no custom themes or orientation)
mindmap (hierarchical outline tree)yes (Phase 1 — vertical tree with root box; all shapes normalised to text; icons silently ignored)
quadrantChart (2x2 priority matrix)yes (Phase 1 — cross-axis chart with quadrant labels and proportionally-placed data points; no custom point styling or background colours)
requirementDiagram (formal requirements + elements + relationships)yes (Phase 1 — vertical box list with relationship summary; no graphical connection lines)
sankey-beta / sankey (directed flow between named nodes)yes (Phase 1 — grouped-arrow list layout; proportional band routing planned for Phase 2)
xychart-beta / xychart (bar/line chart with categorical or numeric axes)yes (Phase 1 — last bar/line series; horizontal orientation rendered vertically; no custom colours)
block-beta / block (fixed-width block grid with directed edges)yes (Phase 1 — rectangle blocks only; nested blocks and vertical spans ignored; edge summary as text below grid)
packet-beta / packet (network packet header bit-range diagram)yes (Phase 1 — fixed 32-bit row width; no custom colours)
architecture-beta / architecture (system architecture with groups, services, and edges)yes (Path A — groups as subgraph containers, services as nodes, edges spatially routed via Sugiyama; port specifiers stored but deferred to Path B)

§Limitations

  • Dotted junctions render as solid — Unicode lacks dotted T-junction and cross glyphs, so / segments that meet other edges fall back to solid //// at the intersection point.
  • RL/BT subgraphs do not reverse internal order — when a subgraph overrides the direction to RL or BT, the nodes inside the subgraph are not reordered; they are simply laid out as if the direction were LR/TD.
  • Deeply-nested alternating direction overrides — each subgraph is evaluated against the top-level graph direction only. A layout such as LR-inside-TB-inside-LR collapses the inner LR nodes but does not propagate the correction upward through multiple nesting levels.
  • Long labels in narrow columns — the compaction pass reduces gap widths but cannot reflow node labels; very long labels may cause nodes to overlap when rendering into a very narrow max_width.

§See also

termaid — the Python prior art from which several rendering techniques (direction-bit canvas, barycenter heuristic constants, subgraph border padding) were adapted.

Re-exports§

pub use architecture::ArchEdge;
pub use architecture::ArchGroup;
pub use architecture::ArchService;
pub use architecture::Architecture;
pub use architecture::Port;
pub use block_diagram::Block;
pub use block_diagram::BlockDiagram;
pub use block_diagram::BlockEdge;
pub use class::Attribute as ClassAttribute;
pub use class::Class;
pub use class::ClassDiagram;
pub use class::Member;
pub use class::Method;
pub use class::RelKind;
pub use class::Relation;
pub use class::Stereotype;
pub use class::Visibility;
pub use er::Attribute;
pub use er::AttributeKey;
pub use er::Cardinality;
pub use er::Entity;
pub use er::ErDiagram;
pub use er::LineStyle;
pub use er::Relationship;
pub use gantt::GanttDiagram;
pub use gantt::GanttSection;
pub use gantt::GanttTask;
pub use git_graph::Branch;
pub use git_graph::Commit;
pub use git_graph::CommitKind;
pub use git_graph::Event as GitEvent;
pub use git_graph::GitGraph;
pub use journey::JourneyDiagram;
pub use journey::Section;
pub use journey::Task;
pub use mindmap::Mindmap;
pub use mindmap::MindmapNode;
pub use packet::Packet;
pub use packet::PacketField;
pub use pie::PieChart;
pub use pie::PieSlice;
pub use quadrant_chart::AxisLabels;
pub use quadrant_chart::QuadrantChart;
pub use quadrant_chart::QuadrantLabels;
pub use quadrant_chart::QuadrantPoint;
pub use requirement_diagram::Element as RequirementElement;
pub use requirement_diagram::RelationshipKind;
pub use requirement_diagram::Requirement;
pub use requirement_diagram::RequirementDiagram;
pub use requirement_diagram::RequirementKind;
pub use requirement_diagram::RequirementRelationship;
pub use requirement_diagram::Risk;
pub use requirement_diagram::VerifyMethod;
pub use sankey::Sankey;
pub use sankey::SankeyFlow;
pub use sequence::Message;
pub use sequence::MessageStyle;
pub use sequence::Participant;
pub use sequence::SequenceDiagram;
pub use timeline::Timeline;
pub use timeline::TimelineEntry;
pub use timeline::TimelineSection;
pub use types::Direction;
pub use types::Edge;
pub use types::EdgeEndpoint;
pub use types::EdgeStyle;
pub use types::Graph;
pub use types::Node;
pub use types::NodeShape;
pub use xy_chart::XAxis;
pub use xy_chart::XyChart;
pub use xy_chart::XyOrientation;
pub use xy_chart::YAxis;

Modules§

architecture
Data model for Mermaid architecture-beta diagrams.
block_diagram
Data model for Mermaid block-beta diagrams.
class
Data model for Mermaid classDiagram charts.
detect
Diagram-type detection from the first non-blank line of Mermaid source.
er
Data model for Mermaid erDiagram (entity-relationship) charts.
gantt
Data model for Mermaid gantt diagrams.
git_graph
Data model for Mermaid gitGraph diagrams.
journey
Data model for Mermaid journey (user-journey) diagrams.
layout
Layout algorithms and the character grid canvas.
mindmap
Data model for Mermaid mindmap diagrams.
packet
Data model for Mermaid packet-beta diagrams.
parser
Mermaid diagram parsers.
pie
Data model for Mermaid pie charts.
quadrant_chart
Data model for Mermaid quadrantChart diagrams.
render
Rendering pipeline: graph + positions → Unicode string.
requirement_diagram
Data model for Mermaid requirementDiagram diagrams.
sankey
Data model for Mermaid sankey-beta diagrams.
sequence
Types for Mermaid sequence diagrams.
timeline
Data model for Mermaid timeline diagrams.
types
Core types shared across parsing, layout, and rendering.
xy_chart
Data model for Mermaid xychart-beta diagrams.

Structs§

RenderOptions
Bundle of optional rendering knobs accepted by render_with_options.

Enums§

Error
All errors that can be returned by this crate.

Functions§

render
Render a Mermaid diagram source string to Unicode box-drawing text.
render_ascii
Render a Mermaid diagram source string to ASCII-only text.
render_ascii_with_width
Render a Mermaid diagram source string to ASCII-only text, optionally compacting the output to fit within a column budget.
render_with_options
Render a Mermaid diagram with the full set of opt-in knobs.
render_with_width
Render a Mermaid diagram source string to Unicode box-drawing text, optionally compacting the output to fit within a column budget.
to_ascii
Convert a Unicode-rendered diagram string to its ASCII equivalent.