Skip to content

How It Works

Architecture

The plugin processes diagrams in 4 steps:

Mermaid SyntaxParserLayout EngineSVG RendererStatic SVG

1. Detection

The plugin scans markdown ```mermaid code blocks and identifies the diagram type from the first line:

  • graph TD / flowchart LR — Flowchart
  • sequenceDiagram — Sequence
  • classDiagram — Class

2. Parsing

Each diagram type has its own hand-written recursive descent parser. No parser generators or heavy dependencies.

graph/flowchartsequenceDiagramclassDiagramRaw textDiagram type?Flowchart ParserSequence ParserClass ParserAST

The parser produces an AST (Abstract Syntax Tree) with nodes, edges, relationships, etc.

3. Layout

The layout engine calculates positions for all elements:

  • Flowchart + Class: Custom Sugiyama-style layered graph layout (zero dependencies, replaces dagre)
  • Sequence: Custom column-based algorithm (participants in columns, messages as rows)

Sugiyama pipeline

PhaseAlgorithmPurpose
1DFS back-edge reversalRemove cycles to ensure a DAG
2Kahn's topological sort + longest pathAssign vertical ranks
3Dummy node insertionHandle edges spanning multiple ranks
4Barycenter heuristicMinimize edge crossings
5Median alignment + spacingAssign x/y coordinates
6Dummy removal + bend pointsClean up edge routing

4. SVG Rendering

SVG is generated as pure strings — no DOM, no browser. Text width is estimated with a character-width lookup table.

No client-side JavaScript

Build timeParser + Layout + RenderStatic SVG in HTMLUser's browserPure SVG — no JS

All parsing, layout calculation, and SVG rendering happens when VitePress builds your site. The end user receives only pure SVG embedded in HTML.

Compared to mermaid.js

vitepress-plugin-mermaid-diagrammermaid.js
RenderingBuild-time (server)Client-side (browser)
Bundle size~60 KB~2 MB
Client JS0 KB~800 KB min+gzip
Dependencies020+
Diagram types315+
Browser DOMNot requiredRequired