Mermaid

Master this essential documentation concept

Quick Definition

A text-based diagramming tool that uses simple markdown-like syntax to generate diagrams and flowcharts, allowing diagrams to be written as code and rendered automatically.

How Mermaid Works

graph TD A[Write Mermaid Syntax] --> B{Valid Syntax?} B -->|Yes| C[Renderer Parses Code] B -->|No| D[Syntax Error Shown] D --> A C --> E[Diagram Generated] E --> F[Embed in Docs] F --> G[GitHub / GitLab] F --> H[Confluence / Notion] F --> I[Static Site Generators]

Understanding Mermaid

A text-based diagramming tool that uses simple markdown-like syntax to generate diagrams and flowcharts, allowing diagrams to be written as code and rendered automatically.

Key Features

  • Centralized information management
  • Improved documentation workflows
  • Better team collaboration
  • Enhanced user experience

Benefits for Documentation Teams

  • Reduces repetitive documentation tasks
  • Improves content consistency
  • Enables better content reuse
  • Streamlines review processes

Turn Videos into Data, AI & Analytics Documents

Use Docsie to convert training videos, screen recordings, and Zoom calls into ready-to-publish data, ai & analytics templates. Download free templates below, or generate documentation from video.

See how Docsie helps with mermaid diagrams in knowledge base

Looking for a better way to handle mermaid in your organization? Docsie's Mermaid Diagrams in Knowledge Base solution helps teams streamline their workflows and improve documentation quality.

Real-World Documentation Use Cases

Replacing Stale Architecture PNGs in a Microservices README

Problem

Engineering teams maintain PNG or Visio exports of service architecture diagrams in their READMEs. Every time a new service is added or a dependency changes, the image file must be manually updated, re-exported, and re-committed, causing diagrams to fall out of sync with reality within weeks.

Solution

Mermaid diagrams are stored as plain text directly inside Markdown files. GitHub and GitLab render them natively, so updating the architecture means editing a few lines of text in the same PR that changes the code, keeping diagrams perpetually accurate.

Implementation

['Replace the existing PNG embed in README.md with a fenced Mermaid code block using graph TD to define service nodes and arrows for API call directions.', 'Add a CONTRIBUTING.md rule requiring that any PR introducing a new service or dependency must update the corresponding Mermaid block in the same commit.', "Enable GitHub's native Mermaid rendering (no plugin needed) or add mermaid-js to the MkDocs or Docusaurus site config for rendered output in hosted docs.", 'Set up a CI lint step using mermaid-js CLI (mmdc --input diagram.md) to catch syntax errors before merge.']

Expected Outcome

Architecture diagrams stay accurate across releases because they live in version control alongside the code, and reviewers can see diagram diffs in pull requests just like code diffs.

Documenting OAuth 2.0 Authorization Code Flow for API Consumers

Problem

API documentation teams need to explain multi-step authentication flows to developers integrating with their platform. Written step-by-step lists are hard to follow, and sequence diagrams created in Lucidchart require a paid license to edit and produce binary files that cannot be reviewed in code review.

Solution

Mermaid's sequenceDiagram syntax lets technical writers describe the exact actors (Browser, Auth Server, Resource Server) and message exchanges in plain text, producing a clear swimlane-style sequence diagram that renders inline in any Markdown-based doc platform.

Implementation

['Create a sequenceDiagram block with participants labeled Browser, Client App, Auth Server, and Resource Server to match the actual OAuth roles in your system.', 'Use Mermaid arrow types (->>, -->>, -x) to distinguish synchronous requests, async callbacks, and error responses within the authorization code exchange.', 'Embed the diagram at the top of the Authentication section in your API reference docs so developers see the full flow before reading endpoint details.', 'Version the diagram file in the same repository as your OpenAPI spec so changes to auth endpoints trigger a documentation review of the sequence diagram.']

Expected Outcome

Developer onboarding time for OAuth integration drops because the sequence diagram provides an at-a-glance mental model before developers read the detailed endpoint documentation.

Visualizing CI/CD Pipeline Stages in a DevOps Runbook

Problem

DevOps teams document their CI/CD pipelines in runbooks, but the pipeline logic—including conditional deployment gates, parallel test jobs, and rollback paths—is too complex to describe in prose. Existing flowcharts in Google Slides become outdated as pipeline YAML files evolve independently.

Solution

Mermaid flowcharts can model conditional branches (diamond nodes), parallel paths (subgraphs), and failure states using graph TD syntax. The diagram lives in the same Git repository as the pipeline YAML, making it easy to update both simultaneously.

Implementation

["Map each pipeline stage (Build, Unit Test, Integration Test, Security Scan, Deploy Staging, Smoke Test, Deploy Production) to Mermaid nodes, using diamond shapes for conditional gates like 'All Tests Pass?'.", 'Use Mermaid subgraph blocks to group parallel jobs such as unit tests and linting that run simultaneously in the CI system.', "Add failure path arrows from each critical stage to a Rollback node with red styling using Mermaid's classDef to visually distinguish failure paths.", "Commit the Mermaid diagram to the docs/ folder of the infrastructure repository and link it from the pipeline YAML file's header comment for discoverability."]

Expected Outcome

New engineers onboarding to the team can understand the full pipeline logic in minutes by reading the diagram, reducing the number of Slack questions about deployment gates and rollback procedures.

Mapping User Onboarding State Transitions in a Product Knowledge Base

Problem

Product and engineering teams struggle to communicate the exact states a new user passes through during onboarding (email unverified, profile incomplete, trial active, subscription required) because the logic is spread across backend code and product specs with no single visual reference.

Solution

Mermaid's stateDiagram-v2 syntax directly models finite state machines with transitions and guard conditions, making it ideal for representing onboarding flows. The diagram can live in Confluence or Notion via Mermaid plugins, or in a GitHub wiki accessible to both product and engineering.

Implementation

['Define each onboarding state as a Mermaid stateDiagram-v2 state node: Registered, EmailVerified, ProfileComplete, TrialActive, Subscribed, and Churned.', 'Add labeled transitions between states (e.g., EmailVerified --> ProfileComplete: Completes profile form) to capture the exact trigger for each state change.', "Include fork and join nodes using Mermaid's state fork syntax to show parallel verification steps like email confirmation and phone verification that must both complete before trial activation.", 'Publish the diagram in the product wiki and reference it in engineering tickets whenever a state transition bug is reported, using it as the source of truth for expected behavior.']

Expected Outcome

Product managers and engineers share a single source of truth for onboarding logic, reducing misalignment bugs caused by differing interpretations of the onboarding flow specification.

Best Practices

âś“ Use Quoted Labels for Node Text Containing Special Characters

Mermaid node labels that include parentheses, colons, slashes, or other special characters will break the parser if left unquoted. Wrapping labels in double quotes inside node brackets ensures the renderer treats the entire string as display text rather than syntax. This is especially important when documenting API endpoints, file paths, or error messages.

âś“ Do: Write node labels as A["POST /api/v2/users"] or B["Error: 401 Unauthorized"] to safely include special characters in diagram nodes.
âś— Don't: Do not write A[POST /api/v2/users] or B[Error: 401 Unauthorized] without quotes, as colons and slashes will cause silent rendering failures or malformed diagrams.

âś“ Limit Diagrams to a Single Concept Per File

Embedding multiple unrelated diagrams in a single Mermaid block or trying to represent an entire system in one flowchart produces cluttered, unreadable output. Each Mermaid diagram should answer one specific question, such as 'how does authentication work?' or 'what states can an order be in?'. Splitting diagrams by concept also makes them easier to update independently.

âś“ Do: Create separate Mermaid blocks for the authentication flow, the data model relationships, and the deployment pipeline, linking between them in your documentation prose.
âś— Don't: Do not attempt to combine a sequence diagram, a state machine, and an architecture overview into a single graph TD block to 'save space' in the documentation file.

âś“ Apply classDef Styling to Communicate Semantic Meaning

Mermaid supports CSS-like class definitions via classDef that let you color-code nodes by their role or status. Using consistent color conventions—such as red for error states, green for success paths, and gray for external systems—helps readers instantly parse diagram structure without reading every label. Define your color conventions once and reuse them across all diagrams in a project.

âś“ Do: Define classDef errorState fill:#ff4444,color:#fff and apply it with class RollbackNode,FailedBuild errorState to visually distinguish failure paths across all pipeline diagrams.
âś— Don't: Do not apply random colors to nodes for aesthetic reasons without a consistent semantic meaning, as inconsistent color use forces readers to re-learn the visual language for every diagram.

âś“ Store Mermaid Diagrams as Text in Version-Controlled Markdown Files

The primary advantage of Mermaid over image-based diagramming tools is that diagrams are plain text and can be diffed, reviewed, and merged like code. Exporting diagrams to PNG or SVG and committing the binary output defeats this purpose. Keeping diagrams as .md files or inline Mermaid blocks in documentation Markdown ensures that every change is traceable and reviewable in pull requests.

âś“ Do: Commit diagram changes as edits to the Mermaid code block in your README.md or docs/*.md files so reviewers can see exactly which nodes or edges changed in the PR diff.
âś— Don't: Do not export Mermaid diagrams to PNG using the CLI and commit only the image file, as this loses the source text and makes future edits require recreating the diagram from scratch.

âś“ Validate Mermaid Syntax in CI Using the Official CLI

Mermaid syntax errors often render silently as blank diagrams or show a cryptic error message only in the browser, making broken diagrams easy to miss in code review. Running the Mermaid CLI (mmdc) as a CI step catches syntax errors before merge and ensures all diagrams in the repository are valid and renderable. This is particularly important in documentation-heavy repositories where many contributors may edit diagrams.

âś“ Do: Add a CI job that runs npx @mermaid-js/mermaid-cli mmdc -i docs/architecture.md -o /tmp/output.svg and fails the build if the exit code is non-zero, blocking merges with broken diagrams.
âś— Don't: Do not rely solely on manual browser preview in GitHub or Confluence to validate Mermaid syntax, as rendering behavior can differ between platforms and broken diagrams may go unnoticed until a user reports them.

How Docsie Helps with Mermaid

Build Better Documentation with Docsie

Join thousands of teams creating outstanding documentation

Start Free Trial