Zum Inhalt springen

Mermaid Cheat Sheet

Overview

Mermaid is a JavaScript-based diagramming and charting tool that renders text definitions into SVG diagrams and charts. It uses a Markdown-inspired syntax that makes it easy to create and modify diagrams as part of your documentation workflow. Mermaid is natively supported in GitHub, GitLab, Notion, Docusaurus, and many other platforms.

Mermaid supports flowcharts, sequence diagrams, class diagrams, state diagrams, entity relationship diagrams, Gantt charts, pie charts, git graphs, and more. Diagrams are defined in plain text, making them versionable with git and easy to include in Markdown documentation.

Installation

CLI

# Install globally
npm install -g @mermaid-js/mermaid-cli

# Generate SVG from a .mmd file
mmdc -i diagram.mmd -o diagram.svg

# Generate PNG
mmdc -i diagram.mmd -o diagram.png

# Generate PDF
mmdc -i diagram.mmd -o diagram.pdf

# With custom config
mmdc -i diagram.mmd -o diagram.svg -c mermaid-config.json

Browser / CDN

<script type="module">
  import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
  mermaid.initialize({ startOnLoad: true, theme: 'default' });
</script>

<pre class="mermaid">
  graph TD
    A-->B
</pre>

npm (for Bundlers)

npm install mermaid
import mermaid from 'mermaid';
mermaid.initialize({ startOnLoad: true });

Flowcharts

Node Shapes

SyntaxShape
A[Text]Rectangle
A(Text)Rounded rectangle
A([Text])Stadium / pill
A[[Text]]Subroutine
A[(Text)]Cylinder (database)
A((Text))Circle
A>Text]Asymmetric / flag
A{Text}Diamond (decision)
A{{Text}}Hexagon
A[/Text/]Parallelogram
A[\Text\]Parallelogram (alt)
SyntaxStyle
A --> BArrow
A --- BLine (no arrow)
A -.- BDotted line
A -.-> BDotted arrow
A ==> BThick arrow
A -- text --> BArrow with label
A -. text .-> BDotted with label
A == text ==> BThick with label
A <--> BBidirectional

Directions

DirectionDescription
graph TDTop to bottom
graph TBTop to bottom
graph BTBottom to top
graph LRLeft to right
graph RLRight to left

Subgraphs

graph TB
    subgraph Frontend
        A[React App] --> B[API Client]
    end
    subgraph Backend
        C[API Server] --> D[Database]
    end
    B --> C

Sequence Diagrams

sequenceDiagram
    participant U as User
    participant C as Client
    participant S as Server

    U->>C: Click Login
    C->>S: POST /auth/login
    activate S
    S-->>C: JWT Token
    deactivate S
    C->>U: Show dashboard

    alt Valid credentials
        S-->>C: 200 OK
    else Invalid credentials
        S-->>C: 401 Unauthorized
    end

    loop Health Check
        S->>S: Ping
    end

Arrow Types

SyntaxDescription
->>Solid line with arrowhead
-->>Dotted line with arrowhead
-xSolid line with cross
--xDotted line with cross
-)Solid line with open arrow
--)Dotted line with open arrow

Class Diagrams

classDiagram
    class Animal {
        +String name
        +int age
        +makeSound() void
    }
    class Dog {
        +String breed
        +bark() void
    }
    Animal <|-- Dog : inherits

Relationship Types

SyntaxDescription
<|--Inheritance
*--Composition
o--Aggregation
-->Association
--Link (solid)
..>Dependency
..|>Realization

State Diagrams

stateDiagram-v2
    [*] --> Idle
    Idle --> Processing : Submit
    Processing --> Success : Valid
    Processing --> Error : Invalid
    Error --> Idle : Retry
    Success --> [*]

    state Processing {
        [*] --> Validating
        Validating --> Saving
        Saving --> [*]
    }

Entity Relationship Diagrams

erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ LINE_ITEM : contains
    PRODUCT ||--o{ LINE_ITEM : "ordered in"

    CUSTOMER {
        int id PK
        string name
        string email UK
    }
    ORDER {
        int id PK
        date created
        string status
    }

Gantt Charts

gantt
    title Project Timeline
    dateFormat  YYYY-MM-DD

    section Planning
    Requirements    :done, req, 2024-01-01, 14d
    Design          :active, des, after req, 10d

    section Development
    Backend         :dev1, after des, 30d
    Frontend        :dev2, after des, 25d

    section Testing
    Integration     :test, after dev1, 10d

Pie Charts

pie title Browser Market Share
    "Chrome" : 65
    "Safari" : 19
    "Firefox" : 4
    "Edge" : 4
    "Other" : 8

Git Graph

gitGraph
    commit
    commit
    branch feature
    checkout feature
    commit
    commit
    checkout main
    merge feature
    commit

Configuration

{
  "theme": "default",
  "themeVariables": {
    "primaryColor": "#1a73e8",
    "primaryTextColor": "#ffffff",
    "lineColor": "#333333",
    "fontSize": "14px",
    "fontFamily": "Inter, sans-serif"
  },
  "flowchart": {
    "curve": "basis",
    "padding": 15
  },
  "sequence": {
    "diagramMarginX": 50,
    "actorMargin": 50,
    "mirrorActors": true
  }
}

Available Themes

ThemeDescription
defaultStandard light theme
darkDark background theme
forestGreen-toned theme
neutralGrayscale theme
baseCustomizable base theme

Troubleshooting

IssueSolution
Diagram not renderingCheck syntax at mermaid.live; ensure mermaid.js is loaded
Special characters in labelsWrap labels in quotes: A["Label with (parens)"]
Diagram too wide/tallReduce content or adjust config padding
GitHub not renderingUse mermaid code fence in Markdown files
Font issuesSet fontFamily in theme variables
SVG export issuesUse CLI tool mmdc for reliable export
Complex diagram too slowSimplify; break into smaller sub-diagrams
Theme colors not applyingUse init directive at top of diagram