Color mode

Mermaid

Mermaid diagram rendering for ```mermaid code blocks.

日本語

Overview

mermaid() replaces mermaid code blocks with a <figure class="rr-mermaid"> that renders to SVG. Diagrams are rendered at build time with a headless browser by default, with an automatic client-side fallback. It runs with order: -10.

Usage

ts
import { defineConfig } from "@riebeckite/core";
import { mermaid } from "@riebeckite/plugin-mermaid";
 
export default defineConfig({
  // ...
  plugins: [
    mermaid({
      render: "build",
      theme: { light: "default", dark: "dark" },
    }),
  ],
});

Behavior

Build

  • Replaces each ```mermaid <pre> with a figure.rr-mermaid containing:
    • figcaption.rr-mermaid__caption — from the code block title or a %% caption: ... line in the source
    • div.rr-mermaid__canvas — the diagram (role="img", labelled by the caption when present)
    • details.rr-mermaid__fallback — collapsible diagram source
  • Static SVG is rendered at build time when render is "build" or "both" by running the Mermaid browser API in Puppeteer's headless Chromium. Rendering uses Chromium's layout engine, not JSDOM polyfills or custom getBBox / text-width estimation
  • Mermaid runs with securityLevel: "strict", the selected theme, transparent background, and a unique SVG id per diagram
  • Invalid diagrams report ruleId: "invalid-diagram"; Chromium renderer failures report ruleId: "renderer-error". When build SVG is unavailable, the figure remains data-mermaid="pending" for client fallback

Client (initMermaidDiagrams)

  • Loads Mermaid from the CDN (jsDelivr, Mermaid 11) unless globalThis.mermaid or an injected instance is provided
  • Renders every [data-mermaid="pending"] figure; failures set data-mermaid="error" (placeholder message via CSS)
  • When theme is { light, dark }, the theme is chosen from html[data-theme] or prefers-color-scheme

Options

Option Type Default Description
render "build" | "client" | "both" "build" When diagrams are rendered
theme string | { light: string; dark: string } { light: "default", dark: "dark" } Mermaid theme
caption boolean true Show title / %% caption: as figcaption
fallback boolean true Show the diagram source in <details>

render modes:

  • "build" — render SVG at build time; diagrams that fail fall back to client rendering
  • "client" — skip build-time rendering, render in the browser only
  • "both" — compatibility alias. It currently behaves like "build": build first, then client fallback only when build rendering fails

Exports

  • mermaid(options?) — plugin factory
  • Types: MermaidOptions, MermaidClientOptions, MermaidRenderMode, MermaidTheme

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/mermaid/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Mermaid
4 +
5 + Mermaid diagram rendering for ` ```mermaid ` code blocks.
6 +
7 + [日本語](./mermaid.md)
8 +
9 + ## Overview
10 +
11 + `mermaid()` replaces mermaid code blocks with a `<figure class="rr-mermaid">`
12 + that renders to SVG. Diagrams are rendered at build time with a headless browser by
13 + default, with an automatic client-side fallback. It runs with `order: -10`.
14 +
15 + ## Usage
16 +
17 + ```ts
18 + import { defineConfig } from "@riebeckite/core";
19 + import { mermaid } from "@riebeckite/plugin-mermaid";
20 +
21 + export default defineConfig({
22 + // ...
23 + plugins: [
24 + mermaid({
25 + render: "build",
26 + theme: { light: "default", dark: "dark" },
27 + }),
28 + ],
29 + });
30 + ```
31 +
32 + ## Behavior
33 +
34 + ### Build
35 +
36 + - Replaces each ` ```mermaid ` `<pre>` with a `figure.rr-mermaid` containing:
37 + - `figcaption.rr-mermaid__caption` — from the code block title or a
38 + `%% caption: ...` line in the source
39 + - `div.rr-mermaid__canvas` — the diagram (`role="img"`, labelled by the
40 + caption when present)
41 + - `details.rr-mermaid__fallback` — collapsible diagram source
42 + - Static SVG is rendered at build time when `render` is `"build"` or
43 + `"both"` by running the Mermaid browser API in Puppeteer's headless Chromium.
44 + Rendering uses Chromium's layout engine, not JSDOM polyfills or custom
45 + `getBBox` / text-width estimation
46 + - Mermaid runs with `securityLevel: "strict"`, the selected theme, transparent
47 + background, and a unique SVG id per diagram
48 + - Invalid diagrams report `ruleId: "invalid-diagram"`; Chromium renderer
49 + failures report `ruleId: "renderer-error"`. When build SVG is unavailable,
50 + the figure remains `data-mermaid="pending"` for client fallback
51 +
52 + ### Client (`initMermaidDiagrams`)
53 +
54 + - Loads Mermaid from the CDN (jsDelivr, Mermaid 11) unless `globalThis.mermaid`
55 + or an injected instance is provided
56 + - Renders every `[data-mermaid="pending"]` figure; failures set
57 + `data-mermaid="error"` (placeholder message via CSS)
58 + - When `theme` is `{ light, dark }`, the theme is chosen from
59 + `html[data-theme]` or `prefers-color-scheme`
60 +
61 + ## Options
62 +
63 + | Option | Type | Default | Description |
64 + | ------ | ---- | ------- | ----------- |
65 + | `render` | `"build" \| "client" \| "both"` | `"build"` | When diagrams are rendered |
66 + | `theme` | `string \| { light: string; dark: string }` | `{ light: "default", dark: "dark" }` | Mermaid theme |
67 + | `caption` | `boolean` | `true` | Show title / `%% caption:` as `figcaption` |
68 + | `fallback` | `boolean` | `true` | Show the diagram source in `<details>` |
69 +
70 + `render` modes:
71 +
72 + - `"build"` — render SVG at build time; diagrams that fail fall back to client
73 + rendering
74 + - `"client"` — skip build-time rendering, render in the browser only
75 + - `"both"` — compatibility alias. It currently behaves like `"build"`: build
76 + first, then client fallback only when build rendering fails
77 +
78 + ## Exports
79 +
80 + - `mermaid(options?)` — plugin factory
81 + - Types: `MermaidOptions`, `MermaidClientOptions`, `MermaidRenderMode`,
82 + `MermaidTheme`
83 +
84 + ## See also
85 +
86 + - [Plugin guide](../reference/plugin-api.en.md)
87 +