Color mode

Canvas

Render Obsidian .canvas files (JSON Canvas 1.0) as diagrams.

日本語

Usage

ts
import { defineConfig } from "@riebeckite/core";
import { canvas } from "@riebeckite/plugin-canvas";
 
export default defineConfig({
  // ...
  plugins: [canvas({ render: "both" })],
});

The plugin runs with order: -15, after obsidian-markdown.

Accepted inputs

  1. A ```canvas fence whose body is raw JSON Canvas
  2. A ```canvas fence whose body is ![[diagram.canvas]] or [[diagram.canvas]], read through contentSource
  3. A direct ![[diagram.canvas]] embed, resolved as an attachment

When a canvas is missing or cannot be parsed, the code block is kept and a message with source: "@riebeckite/plugin-canvas" is reported.

Output

The plugin emits div.rb-canvas with data-canvas (the input), data-canvas-nodes, data-canvas-edges, and data-canvas-render (static / client / both, or ready after hydration). It contains:

  • script[type="application/json"][data-canvas-payload] — escaped JSON Canvas; inert, never executed
  • div.rb-canvas__static — when render is "static" or "both": a no-JS layered fallback with absolutely positioned node cards and an SVG edge list
  • div.rb-canvas__stage — when render is "client" or "both": an empty stage filled by initCanvas()
  • details.rb-canvas__fallback — a node/edge list for accessibility and no-JS

file nodes resolve through contentIndex: notes link to their permalink and other files link to their attachment URL. Note links are finalised in onManifestCreated from the manifest, rewriting the manifest entry's HTML. Core synchronizes that HTML with the content the route renders. text nodes get minimal Markdown handling (wikilinks and escaping).

Client

initCanvas() finds elements whose data-canvas-render is client / both, builds positioned nodes and SVG edges from the payload, then sets data-canvas-render="ready"; CSS hides the static fallback once ready. It also supports wheel zoom and drag pan (pan/zoom-lite).

Options

Option Default Description
className "rb-canvas" Wrapper class name
language "canvas" Code fence language
render "both" "static", "client", or "both"
height unset Stage height (px number or CSS length)
maxNodes unset Maximum number of nodes drawn

Exports

  • canvas(options?) / canvasPlugin — plugin factory
  • initCanvas() — client initializer
  • parseCanvas(json) — pure JSON Canvas parser
  • buildCanvasLayout(doc) — pure coordinate/edge normaliser
  • resolveCanvasOptions(options) — apply defaults
  • Types: CanvasOptions, CanvasRenderMode, CanvasDocument, CanvasNode, CanvasEdge, and more

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/canvas/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Canvas
4 +
5 + Render Obsidian `.canvas` files (JSON Canvas 1.0) as diagrams.
6 +
7 + [日本語](./canvas.md)
8 +
9 + ## Usage
10 +
11 + ```ts
12 + import { defineConfig } from "@riebeckite/core";
13 + import { canvas } from "@riebeckite/plugin-canvas";
14 +
15 + export default defineConfig({
16 + // ...
17 + plugins: [canvas({ render: "both" })],
18 + });
19 + ```
20 +
21 + The plugin runs with `order: -15`, after `obsidian-markdown`.
22 +
23 + ## Accepted inputs
24 +
25 + 1. A ` ```canvas ` fence whose body is raw JSON Canvas
26 + 2. A ` ```canvas ` fence whose body is `![[diagram.canvas]]` or
27 + `[[diagram.canvas]]`, read through `contentSource`
28 + 3. A direct `![[diagram.canvas]]` embed, resolved as an attachment
29 +
30 + When a canvas is missing or cannot be parsed, the code block is kept and a
31 + message with `source: "@riebeckite/plugin-canvas"` is reported.
32 +
33 + ## Output
34 +
35 + The plugin emits `div.rb-canvas` with `data-canvas` (the input), `data-canvas-nodes`,
36 + `data-canvas-edges`, and `data-canvas-render` (`static` / `client` / `both`, or
37 + `ready` after hydration). It contains:
38 +
39 + - `script[type="application/json"][data-canvas-payload]` — escaped JSON Canvas;
40 + inert, never executed
41 + - `div.rb-canvas__static` — when `render` is `"static"` or `"both"`: a no-JS
42 + layered fallback with absolutely positioned node cards and an SVG edge list
43 + - `div.rb-canvas__stage` — when `render` is `"client"` or `"both"`: an empty
44 + stage filled by `initCanvas()`
45 + - `details.rb-canvas__fallback` — a node/edge list for accessibility and no-JS
46 +
47 + `file` nodes resolve through `contentIndex`: notes link to their permalink and
48 + other files link to their attachment URL. Note links are finalised in
49 + `onManifestCreated` from the manifest, rewriting the manifest entry's HTML. Core
50 + synchronizes that HTML with the content the route renders. `text` nodes get
51 + minimal Markdown handling (wikilinks and escaping).
52 +
53 + ## Client
54 +
55 + `initCanvas()` finds elements whose `data-canvas-render` is `client` / `both`,
56 + builds positioned nodes and SVG edges from the payload, then sets
57 + `data-canvas-render="ready"`; CSS hides the static fallback once ready. It also
58 + supports wheel zoom and drag pan (pan/zoom-lite).
59 +
60 + ## Options
61 +
62 + | Option | Default | Description |
63 + | ------ | ------- | ----------- |
64 + | `className` | `"rb-canvas"` | Wrapper class name |
65 + | `language` | `"canvas"` | Code fence language |
66 + | `render` | `"both"` | `"static"`, `"client"`, or `"both"` |
67 + | `height` | unset | Stage height (px number or CSS length) |
68 + | `maxNodes` | unset | Maximum number of nodes drawn |
69 +
70 + ## Exports
71 +
72 + - `canvas(options?)` / `canvasPlugin` — plugin factory
73 + - `initCanvas()` — client initializer
74 + - `parseCanvas(json)` — pure JSON Canvas parser
75 + - `buildCanvasLayout(doc)` — pure coordinate/edge normaliser
76 + - `resolveCanvasOptions(options)` — apply defaults
77 + - Types: `CanvasOptions`, `CanvasRenderMode`, `CanvasDocument`, `CanvasNode`,
78 + `CanvasEdge`, and more
79 +
80 + ## See also
81 +
82 + - [Plugin guide](../reference/plugin-api.en.md)
83 +