Back to guides
BeginnerFlowchartGuideDocumentation

The Definitive Guide to Mermaid.js: Diagramming as Code in 2025

Alex Rivera

Alex Rivera

Senior Dev

Jan 15, 202525 min read
The Definitive Guide to Mermaid.js: Diagramming as Code in 2025

In the landscape of modern software engineering, documentation is often the first casualty of rapid iteration. Traditional diagramming tools—Visio, Lucidchart, or even whiteboard photos—suffer from a fatal flaw: they are binary blobs or external assets detached from the code they describe. When the code changes, the diagram becomes obsolete immediately. 'Diagrams as Code' solves this fundamental problem by treating infrastructure and logic visualizations as text files that live in your repository.

Mermaid.js has emerged as the de facto standard for this movement in 2025. By using a simple Markdown-like syntax, it allows developers to generate complex flowcharts, sequence diagrams, and Gantt charts dynamically. Because it is text, it can be version-controlled, diffed in Pull Requests, and rendered automatically by platforms like GitHub, GitLab, Notion, and Obsidian. This guide will take you from a complete beginner to a Mermaid power user.

Section 1: The Philosophy of Text-Based Diagramming

Before diving into syntax, it is crucial to understand the workflow benefits. Imagine you are refactoring a monolithic application into microservices. In a drag-and-drop tool, you would need to manually realign boxes, reroute arrows, and ensure fonts match. In Mermaid, you simply change a line of text.

  • **Version History**: You can run `git blame` on a diagram to see who added a specific dependency.
  • **Reviewability**: Pull Requests can include diagram updates that reviewers can read as diffs.
  • **Consistency**: The layout engine handles positioning. You stop wasting time aligning pixels and focus on logic.
  • **Portability**: The source is plain text (KB) versus heavy image files (MB).

Section 2: Flowchart Syntax Mastery

Flowcharts are the bread and butter of Mermaid. They are composed of nodes (geometric shapes) and edges (arrows/lines). The graph direction can be Top-Bottom (TB), Bottom-Top (BT), Left-Right (LR), or Right-Left (RL).

graph TD
    Start --> Stop

While the example above is trivial, real-world diagrams require more expressive nodes. Mermaid uses brackets to define shapes: `[]` for squares, `()` for rounded edges, `(())` for circles, and `{}` for rhombuses (decisions).

Complex Microservices Architecture Example

Let's visualize a realistic scenario: A Kubernetes-based architecture handling user authentication and data processing. We will use `subgraph` to represent network boundaries (VPCs or Clusters) and different line types to represent synchronous vs asynchronous communication.

Note: Note the use of `:::` to attach classes to nodes. This keeps your graph definition clean while allowing for powerful CSS customization at the top of the file.

Section 3: Advanced Styling and Classes

The default Mermaid styling is functional but often too plain for client presentations. You can define CSS classes directly within the diagram. This is done using the `classDef` keyword. You can control fill color, stroke color, stroke width, and even stroke dash arrays.

classDef error fill:#ffcdd2,stroke:#b71c1c,stroke-width:2px;
classDef success fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;

NodeA:::error --> NodeB:::success

Styling isn't just about aesthetics; it's about semantics. Use red for error states, green for success states, and dashed lines for optional or asynchronous processes. This visual language helps stakeholders understand the diagram without reading every label.

Section 4: Decision Trees and Logic Flows

One of the most common use cases for Mermaid is mapping out business logic or algorithms. The rhombus shape `{}` is standard for decisions. Let's look at a complex approval workflow for a loan application system.

In the diagram above, we use logic gates to clearly show the path an application takes. The specific syntax `Node -- Label --> Node` allows you to place text specifically on the arrow connecting two nodes, which is critical for decision trees.

Conclusion

Mermaid.js effectively bridges the gap between developers and documentation. By adopting it, you ensure that your diagrams are as agile as your code. Whether you are documenting a high-level cloud architecture or a low-level function logic, Mermaid provides the tools to do it efficiently and maintainably.

Section 5: CI/CD & Docs Automation

Treat your Mermaid files like any other code artifact. Add a lint step to CI to catch syntax errors early, generate PNG/SVG assets automatically on every merge, and publish them to an internal docs portal. This keeps diagrams fresh without manual exports and avoids broken embeds across your wiki.

  • Run `npx @mermaid-js/mermaid-cli -i src/diagrams/*.mmd -o dist/diagrams` in CI to keep assets synchronized.
  • Fail the pipeline if `mermaid.parse` throws, preventing broken diagrams from shipping.
  • Publish diagrams as part of Storybook or a static docs site so PMs and QA can browse without pulling the repo.

Note: For private codebases, store rendered diagrams in an internal CDN with caching to speed up onboarding and architecture reviews.

Section 6: Migration Checklist

Replacing legacy Visio/Lucidchart assets is easier if you phase it: convert one system map, validate with stakeholders, then scale to more services. Keep a shared style guide (classDef tokens, font stacks) so every team ships consistent visuals.

  • Inventory existing diagrams and prioritize high-traffic docs (README, onboarding, runbooks).
  • Create a base theme (colors, spacing, classDefs) and import it as snippets to avoid one-off styling.
  • Automate exports and link them from your wiki to avoid stale uploads and screenshot drift.
Found this guide helpful?
Mermaid AI Studio - Free Online Mermaid Diagram Editor & Generator with AI