Skip to content

Mermaid diagrams in Markdown

example of the Mermaid diagramexample of the Mermaid diagram

Example taken from mermaid.js.org.

There are many diagramming languages (see text-to-diagram). Mermaid seems to be popular, as it is supported by GitHub.

There are two options:

  • rehype-mermaid - the original plugin. Battle-tested and well supported.
  • @beoe/rehype-mermaid - my experiment. It is based on mermaid-isomorphic (the same one used by rehype-mermaid). It also adds:

@beoe/rehype-mermaid

Installation

  1. Install dependencies

    Terminal window
    pnpm add @beoe/rehype-mermaid playwright
  2. Update package.json

    package.json
    "scripts": {
    "postinstall": "playwright install chromium"
    }
  3. Configure Astro. See note about Rehype Plugins for Code.

    astro.config.mjs
    import { rehypeMermaid } from "@beoe/rehype-mermaid";
    export default defineConfig({
    markdown: {
    rehypePlugins: [
    [
    rehypeMermaid,
    {
    strategy: "file", // alternatively use "data-url"
    fsPath: "public/beoe", // add this to gitignore
    webPath: "/beoe",
    darkScheme: "class",
    },
    ],
    ],
    },
    });
  4. Add line to .gitignore:

    public/beoe
  5. Add CSS for dark mode:

    src/styles/custom.css
    html[data-theme="light"] .beoe-dark {
    display: none;
    }
    html[data-theme="dark"] .beoe-light {
    display: none;
    }
  6. Optional install dependency for cache

    Terminal window
    pnpm add @beoe/cache
  7. Optional configure cache

    astro.config.mjs
    import { getCache } from "@beoe/cache";
    const cache = await getCache();
    export default defineConfig({
    markdown: {
    rehypePlugins: [
    [
    rehypeMermaid,
    {
    strategy: "file",
    fsPath: "public/beoe",
    webPath: "/beoe",
    darkScheme: "class",
    cache,
    },
    ],
    ],
    },
    });
  8. Optional add pan and zoom for diagrams

Example

example.md
```mermaid alt="example of the Mermaid diagram"
flowchart LR
Start --> Stop
```
example of the Mermaid diagramexample of the Mermaid diagram