Skip to content

Mermaid diagrams in markdown

Example taken from mermaid.js.org

There are a lot of diagraming languages (see text-to-diagram). Mermaid seems to be popular (it is supported by GitHub).

There are two options:

  • rehype-mermaid - 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). But it also adds:

@beoe/rehype-mermaid

Installation

  1. Install dependencies

    Terminal window
    pnpm add @beoe/rehype-mermaid
  2. Configure Astro. See note about Rehype plugins for code.

    astro.config.mjs
    import { rehypeMermaid } from "@beoe/rehype-mermaid";
    export default defineConfig({
    integrations: [
    starlight({
    customCss: ["./src/styles/custom.css"],
    }),
    ],
    markdown: {
    rehypePlugins: [
    [
    rehypeMermaid,
    { class: "not-content", strategy: "img-class-dark-mode" },
    ],
    ],
    },
    });
  3. 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;
    }
  4. Optional install dependency for cache

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

    astro.config.mjs
    import { getCache } from "@beoe/cache";
    const cache = await getCache();
    export default defineConfig({
    markdown: {
    rehypePlugins: [
    [
    rehypeMermaid,
    { class: "not-content", strategy: "img-class-dark-mode", cache },
    ],
    ],
    },
    });
  6. Optional add pan and zoom for diagrams

rehype-mermaid

Installation

  1. Install dependencies

    Terminal window
    pnpm add rehype-mermaid
  2. Configure Astro. See note about Rehype plugins for code.

    astro.config.mjs
    import { rehypeMermaid } from "rehype-mermaid";
    export default defineConfig({
    markdown: {
    rehypePlugins: [rehypeMermaid],
    },
    });

Strategies

inline-svgimg-svg
supports css option✔️ yesno
text is searchable (Cmd + F)✔️ yesno
supports dark mode (1)no✔️ yes
issues with rehype-raw (2)yes✔️ no
other CSS on the page may conflict (3)yes✔️ no

inline-svg is deafault strategy.

(1) dark mode

In order to enable dark mode use:

astro.config.mjs
export default defineConfig({
markdown: {
rehypePlugins: [[rehypeMermaid, { strategy: "img-svg", dark: true }]],
},
});

But it doesn’t work with Starlight’s slector based dark mode. See starlight#1829.

(2) issues with rehype-raw

I noticed issues only with sankey diagram. So this is minor issue

(3) other CSS

You may style inline SVG with CSS:

src/styles/custom.css
svg[id^="mermaid"] {
/* undo global styles */
.node .label {
line-height: 1.2;
}
/* primitive dark mode */
.flowchart-link {
stroke: var(--sl-color-white) !important;
}
.marker {
stroke: var(--sl-color-white);
fill: var(--sl-color-white) !important;
}
.node-labels {
fill: var(--sl-color-white);
}
}

Example

example.md
```mermaid
flowchart LR
Start --> Stop
```