Mermaid diagrams in Markdown

Mermaid is a way to draw diagrams with text. You write a few short lines. A reader that supports Mermaid turns them into a picture: a flowchart, a timeline, a chart.

You can keep the diagram inside a normal Markdown file. There is no image to export and no drawing tool to open. To change the diagram, you change the text.

The mermaid code block

A Mermaid diagram lives in a fenced code block. A fence is three backticks. After the opening fence, write mermaid as the language:

```mermaid
flowchart LR
    A --> B
```

The first line inside the block says which kind of diagram it is. Here it is flowchart LR. Everything after that line describes the diagram.

Lines that start with %% are comments. The reader ignores them.

Example 1: a flowchart with a decision

```mermaid
flowchart TD
    Start[Write the note] --> Check{Is it clear?}
    Check -->|Yes| Publish[Share it]
    Check -->|No| Edit[Rewrite it]
    Edit --> Check
```

How to read it:

Example 2: a sequence diagram

A sequence diagram shows who talks to whom, in order.

```mermaid
sequenceDiagram
    participant U as User
    participant S as Server
    U->>S: Open the page
    S-->>U: Send the page
    U->>S: Log in
    S-->>U: Welcome
```

Example 3: a pie chart

```mermaid
pie title Time spent on a project
    "Writing" : 40
    "Reading" : 25
    "Meetings" : 15
    "Other" : 20
```

Each slice is a label in quotes, a colon and a number. The number must be above zero.

Other diagram types

Mermaid can draw many more kinds. The common ones:

Each type has its own first word and its own rules. The Mermaid documentation has a page for each one.

Where Mermaid diagrams are drawn

GitHub draws Mermaid blocks in Markdown files, issues, discussions, pull requests and wikis. You only need the mermaid code block. Many Markdown editors and notes apps support Mermaid too, but not all of them.

When a reader does not support Mermaid, it usually treats the block like any other code. You see the text you wrote, not a picture. Nothing is lost. It is just not drawn.

Common mistakes

Indentation is not needed. Most people indent the lines inside a diagram only because it is easier to read.

AI chats and agents write Mermaid

AI chats and coding agents often put Mermaid in plans, specs and design notes. A flowchart is a quick way to show steps. A sequence diagram shows how parts of a system talk.

If a chat gives you a diagram, copy the whole code block, including the fences, into your Markdown file. If the diagram does not draw, check it against the mistakes above. Short labels and one idea per diagram make the text easier to fix.

Reading Mermaid diagrams

PilcrowMD draws Mermaid diagrams on Android and on Mac, with no internet needed. On Android, tap a diagram to see it full screen. On Mac, Quick Look, Compare and PDF show the code block instead of the picture.

Common questions

Do I need to install anything to write Mermaid? No. You write plain text in a code block. You only need a reader that draws it.

Why do I see code instead of a diagram? Either the reader does not support Mermaid, or the diagram has a syntax error. Check that the block starts with mermaid and that the first line names a real diagram type.

Can I change how a diagram looks? Yes. Mermaid has options for colours, shapes and layout. Start with the defaults. They are enough for most notes.

Also on Mac: PilcrowMD for Mac (beta)