Color mode

Graphviz

Graphviz (DOT) diagram rendering for ```dot and ```graphviz code blocks.

日本語

Overview

graphviz() replaces DOT code blocks with a <figure class="rb-graphviz"> that renders to SVG. Diagrams are rendered at build time by default using the @viz-js/viz WebAssembly build of Graphviz, which runs fully offline inside Node and lets the build process exit cleanly (no leaked workers or timers). It runs with order: -10.

Usage

ts
import { defineConfig } from "@riebeckite/core";
import { graphviz } from "@riebeckite/plugin-graphviz";
 
export default defineConfig({
  // ...
  plugins: [
    graphviz({
      render: "build",
      engine: "dot",
    }),
  ],
});
markdown
```dot
// caption: Request flow
digraph {
  rankdir="LR"
  client -> server [label="request"]
  server -> client [label="response"]
}
```

The fenced info string graphviz is accepted as an alias of dot.

Behavior

Build

  • Replaces each DOT <pre> with a figure.rb-graphviz containing:
    • figcaption.rb-graphviz__caption — from the code block title or a // caption: ... line in the source
    • div.rb-graphviz__canvas — the diagram (role="img", labelled by the caption when present)
    • details.rb-graphviz__fallback — collapsible DOT source
  • Static SVG is rendered at build time when render is "build" or "both", using the WASM Graphviz layout engine. The XML prolog and DOCTYPE emitted by Graphviz are stripped so the SVG can be inlined in HTML.
  • Invalid DOT reports ruleId: "invalid-diagram"; WASM/renderer failures report ruleId: "renderer-error", both under source: "@riebeckite/plugin-graphviz".
  • Each figure carries stable hooks:
    • class="rb-graphviz"
    • data-graphviz="rendered" | "pending" | "error"
    • data-graphviz-engine="dot"
    • data-graphviz-source="..." (the escaped DOT source)

Client (initGraphvizDiagrams)

  • Used by "client" mode, and as a fallback for failed "both" builds.
  • Fetches @viz-js/viz from jsDelivr as an ES module unless a renderer is injected through options.renderer or globalThis.viz.
  • Renders every [data-graphviz="pending"] figure and flips the state to data-graphviz="rendered"; failures set data-graphviz="error" (placeholder message via CSS).

Rendering offline

Build-time rendering needs no network access. @viz-js/viz ships the Graphviz engine as WebAssembly and runs in-process under Node; the module is imported dynamically so it never enters the client bundle. It was chosen over @hpcc-js/wasm because its Node lifecycle is clean — the process exits without a lingering worker.

Options

Option Type Default Description
render "build" | "client" | "both" "build" When diagrams are rendered
engine "dot" | "neato" | "fdp" | "sfdp" | "circo" | "twopi" "dot" Graphviz layout engine
caption boolean true Show the title / // caption: as figcaption
fallback boolean true Show the DOT source in <details>
className string "rb-graphviz" Base class for the <figure> (sub-elements use __canvas, __caption, __fallback)

render modes:

  • "build" — render SVG at build time; invalid diagrams are reported and the figure becomes data-graphviz="error"
  • "client" — skip build-time rendering, render in the browser only
  • "both" — build first, then fall back to client rendering when build rendering fails

CSS hooks

  • .rb-graphviz, .rb-graphviz__canvas, .rb-graphviz__caption, .rb-graphviz__fallback
  • [data-graphviz="pending"] / [data-graphviz="error"] show a placeholder
  • Dark mode follows html[data-theme="dark"], or the prefers-color-scheme: dark system query while data-theme is absent

Exports

  • graphviz(options?) — plugin factory (graphvizPlugin is an alias)
  • initGraphvizDiagrams — client initializer (@riebeckite/plugin-graphviz/client)
  • Types: GraphvizOptions, GraphvizClientOptions, GraphvizRenderMode, GraphvizEngine

Limitations

  • The client renderer loads @viz-js/viz from the jsDelivr CDN; offline client rendering requires an injected renderer.
  • HTML-like labels (label=<...>) are supported by Graphviz, but only standard, safe output should be trusted; the plugin does not sanitize generated SVG.
  • Captions use the code block title or a leading // caption: ... comment.

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/graphviz/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Graphviz
4 +
5 + Graphviz (DOT) diagram rendering for ` ```dot ` and ` ```graphviz ` code blocks.
6 +
7 + [日本語](./graphviz.md)
8 +
9 + ## Overview
10 +
11 + `graphviz()` replaces DOT code blocks with a `<figure class="rb-graphviz">` that
12 + renders to SVG. Diagrams are rendered at build time by default using the
13 + [`@viz-js/viz`](https://github.com/mdaines/viz-js) WebAssembly build of Graphviz,
14 + which runs fully offline inside Node and lets the build process exit cleanly (no
15 + leaked workers or timers). It runs with `order: -10`.
16 +
17 + ## Usage
18 +
19 + ```ts
20 + import { defineConfig } from "@riebeckite/core";
21 + import { graphviz } from "@riebeckite/plugin-graphviz";
22 +
23 + export default defineConfig({
24 + // ...
25 + plugins: [
26 + graphviz({
27 + render: "build",
28 + engine: "dot",
29 + }),
30 + ],
31 + });
32 + ```
33 +
34 + ````markdown
35 + ```dot
36 + // caption: Request flow
37 + digraph {
38 + rankdir="LR"
39 + client -> server [label="request"]
40 + server -> client [label="response"]
41 + }
42 + ```
43 + ````
44 +
45 + The fenced info string `graphviz` is accepted as an alias of `dot`.
46 +
47 + ## Behavior
48 +
49 + ### Build
50 +
51 + - Replaces each DOT `<pre>` with a `figure.rb-graphviz` containing:
52 + - `figcaption.rb-graphviz__caption` — from the code block title or a
53 + `// caption: ...` line in the source
54 + - `div.rb-graphviz__canvas` — the diagram (`role="img"`, labelled by the
55 + caption when present)
56 + - `details.rb-graphviz__fallback` — collapsible DOT source
57 + - Static SVG is rendered at build time when `render` is `"build"` or `"both"`,
58 + using the WASM Graphviz layout engine. The XML prolog and DOCTYPE emitted by
59 + Graphviz are stripped so the SVG can be inlined in HTML.
60 + - Invalid DOT reports `ruleId: "invalid-diagram"`; WASM/renderer failures report
61 + `ruleId: "renderer-error"`, both under
62 + `source: "@riebeckite/plugin-graphviz"`.
63 + - Each figure carries stable hooks:
64 + - `class="rb-graphviz"`
65 + - `data-graphviz="rendered" | "pending" | "error"`
66 + - `data-graphviz-engine="dot"`
67 + - `data-graphviz-source="..."` (the escaped DOT source)
68 +
69 + ### Client (`initGraphvizDiagrams`)
70 +
71 + - Used by `"client"` mode, and as a fallback for failed `"both"` builds.
72 + - Fetches `@viz-js/viz` from jsDelivr as an ES module unless a renderer is
73 + injected through `options.renderer` or `globalThis.viz`.
74 + - Renders every `[data-graphviz="pending"]` figure and flips the state to
75 + `data-graphviz="rendered"`; failures set `data-graphviz="error"` (placeholder
76 + message via CSS).
77 +
78 + ## Rendering offline
79 +
80 + Build-time rendering needs no network access. `@viz-js/viz` ships the Graphviz
81 + engine as WebAssembly and runs in-process under Node; the module is imported
82 + dynamically so it never enters the client bundle. It was chosen over
83 + `@hpcc-js/wasm` because its Node lifecycle is clean — the process exits without
84 + a lingering worker.
85 +
86 + ## Options
87 +
88 + | Option | Type | Default | Description |
89 + | ------ | ---- | ------- | ----------- |
90 + | `render` | `"build" \| "client" \| "both"` | `"build"` | When diagrams are rendered |
91 + | `engine` | `"dot" \| "neato" \| "fdp" \| "sfdp" \| "circo" \| "twopi"` | `"dot"` | Graphviz layout engine |
92 + | `caption` | `boolean` | `true` | Show the title / `// caption:` as `figcaption` |
93 + | `fallback` | `boolean` | `true` | Show the DOT source in `<details>` |
94 + | `className` | `string` | `"rb-graphviz"` | Base class for the `<figure>` (sub-elements use `__canvas`, `__caption`, `__fallback`) |
95 +
96 + `render` modes:
97 +
98 + - `"build"` — render SVG at build time; invalid diagrams are reported and the
99 + figure becomes `data-graphviz="error"`
100 + - `"client"` — skip build-time rendering, render in the browser only
101 + - `"both"` — build first, then fall back to client rendering when build
102 + rendering fails
103 +
104 + ## CSS hooks
105 +
106 + - `.rb-graphviz`, `.rb-graphviz__canvas`, `.rb-graphviz__caption`,
107 + `.rb-graphviz__fallback`
108 + - `[data-graphviz="pending"]` / `[data-graphviz="error"]` show a placeholder
109 + - Dark mode follows `html[data-theme="dark"]`, or the
110 + `prefers-color-scheme: dark` system query while `data-theme` is absent
111 +
112 + ## Exports
113 +
114 + - `graphviz(options?)` — plugin factory (`graphvizPlugin` is an alias)
115 + - `initGraphvizDiagrams` — client initializer (`@riebeckite/plugin-graphviz/client`)
116 + - Types: `GraphvizOptions`, `GraphvizClientOptions`, `GraphvizRenderMode`,
117 + `GraphvizEngine`
118 +
119 + ## Limitations
120 +
121 + - The client renderer loads `@viz-js/viz` from the jsDelivr CDN; offline client
122 + rendering requires an injected `renderer`.
123 + - HTML-like labels (`label=<...>`) are supported by Graphviz, but only standard,
124 + safe output should be trusted; the plugin does not sanitize generated SVG.
125 + - Captions use the code block title or a leading `// caption: ...` comment.
126 +
127 + ## See also
128 +
129 + - [Plugin guide](../reference/plugin-api.en.md)
130 +