# Embedding

Five ways to put a diagram somewhere, from the most isolated to the most integrated. The first three need a
hosted Tritone site: [run your own](hosting.md). The Markdown plugin can also do without one, and
[write an element](#2-markdown-plugin).

| Where | Use |
| --- | --- |
| A site or docs tool you control | [iframe](#1-iframe), or the [Markdown plugin](#2-markdown-plugin) that writes it for you |
| GitHub, GitLab, npm | [Markdown image](#3-markdown-image): the only form they do not strip |
| The Markdown preview of VS Code and of Cursor | The [extension](#in-vs-code-and-cursor): a code block that plays in the preview. Without it, the [Markdown image](#3-markdown-image), or the SVG as a file of the repository |
| A conversation with Claude or ChatGPT | The [MCP server](hosting.md#the-mcp-server): the model calls a tool with a story, and the player is drawn in the conversation |
| A chat that has no MCP server | A [link](#in-a-chat) that the model writes |
| Notion, Confluence and other embed blocks | Paste the address of the [iframe](#1-iframe) page |
| Your own page or app | [Custom element](#4-custom-element) or the [API](#5-api) |

## Where a diagram moves, and where it answers

Markdown is text: what draws it decides what a diagram can be there. A diagram that answers its reader (the steps,
**Back** and **Next**, the zoom, an actor dragged) is a script at work in the page. A place that takes scripts out
is left with a picture, and the picture still moves: it is a file that plays by itself.

| Where the Markdown is read | Moves | Answers the reader | With |
| --- | --- | --- | --- |
| A site you build: markdown-it, MDX, Docusaurus, Astro | Yes | Yes | The [plugin](#2-markdown-plugin), the [iframe](#1-iframe) or the [element](#4-custom-element) |
| GitHub: a README, an issue, a pull request | Yes | No. A press on the picture opens the diagram that does | The [Markdown image](#3-markdown-image), as a link |
| The Markdown preview of VS Code and of Cursor | Yes | Yes | The [extension](#in-vs-code-and-cursor). Without it, the [Markdown image](#3-markdown-image), from its address or as a file, which moves and does not answer |
| A chat: Claude, ChatGPT | Yes | Yes | The [MCP server](hosting.md#the-mcp-server), added to the chat. With none, a [link](#in-a-chat) to the diagram, and nothing moves there |

This is what each of them did in November 2026, and why:

- **GitHub** shows a `tritone` code block as code and writes an `<iframe>` out as text. An image it fetches
  itself and hands on with rules that let an SVG keep its styles and its animation, and run no script. The blocks it
  does draw (Mermaid, maps, 3D models) are its own: nobody outside GitHub can add one.
- **VS Code** (1.144) and **Cursor** (3.32) carry the same Markdown preview. It loads an image from an `https`
  address or from the folder, and allows no frame and no script that a document brings. An extension can add both a
  kind of code block and a script to that preview: it is how VS Code itself draws Mermaid there, and how
  [Tritone's extension](#in-vs-code-and-cursor) makes a `tritone` block the player. The preview does not tell an
  image which theme the editor is in, so the picture follows the system's.
- **A chat** shows no picture from an address its model wrote: ChatGPT checks every address and refuses the ones a
  model made up, and Claude loads no image from another site. Both show a link. What puts a diagram that answers
  inside a conversation is an MCP App, a tool that brings its own interface: the
  [MCP server](hosting.md#the-mcp-server) is one. It was checked with the client and the host of the Apps SDK, not in
  Claude or ChatGPT themselves.

### In a chat

A chat that has the [MCP server](hosting.md#the-mcp-server) draws the player itself. In any other, a model hands
its reader a link. A model that can run code writes the address as the editor does, with `encode`. One that
cannot, cannot work out base64 either, so the `<source>` of an address is also read when it is the text itself,
percent-encoded:

```text
https://your-host/live.html?view#story%20Hello%0Aa(laptop)%5BYou%5D%0Ab(server)%5BAPI%5D%0Astep%20Ask%0A%20%20a%20-%3E%20b:%20GET%20%2F
```

A space is `%20`, a line break `%0A`, a percent sign `%25`; `encodeURIComponent` writes all of it. The reader
presses the link and gets the diagram on the whole window, with its controls. It holds for every address: the
editor's, the [iframe](#1-iframe) page's and the [image](#3-markdown-image)'s.

## 1. iframe

The page `embed.html` draws the diagram written in its own address:

```text
https://your-host/embed.html?model=<model>&step=<n>#<source>
```

- `<source>`: the diagram text as UTF-8, in URL-safe base64 (`-` and `_`, no `=`): `encode(text)` writes it and
  `decode(source)` reads it, both in `embed.ts`. It sits after the `#`, so it
  is never sent to the server. `decode` also reads the text itself, percent-encoded: the form
  [a model writes in a chat](#in-a-chat).
- `model`: for Mermaid text only. `planes` (default), `nodes`, `city`, `tree`, `ring`, `stages`, `lanes`,
  `dominators`, `sankey` or `matrix`: see [models](models.md).
- `step`: for stories only. The step to open on, counted from 1.

The page shows the diagram as a [canvas](#the-canvas): it fills the frame, whatever its size, and the reader
zooms and moves around in it. It follows the reader's light or dark setting. Inside a page of the Tritone site,
whichever domain each is served from, it takes the theme chosen there: the frame asks the page around it when it
opens, and is told when the reader changes it. A page of your own can do the same by answering the message
`tritone:theme?` with `{ tritoneTheme: 'dark' }` or `'light'`, as `packages/ui/theme.js` does.

```html
<iframe src="https://your-host/embed.html#c3Rvcnkg…" title="How an order is placed"
  width="100%" height="560" style="border:0"></iframe>
```

The live editor writes this for the diagram on screen: **Embed**, in its **Share** window. The editor reads the
same address with `live.html` in place of `embed.html`, so `https://your-host/live.html?model=<model>#<source>`
opens that diagram for editing. With `view` in the query (`live.html?view#<source>`) the editor opens it for
reading: the diagram has the window, with the editor's header over it and a button to edit it. That is the **Link**
of the Share window.

## 2. Markdown plugin

Can a Markdown file embed an iframe? **It depends on who renders it.** Markdown allows raw HTML, so an iframe is
legal; whether it survives is the renderer's choice.

| Renderer | iframes | Use |
| --- | --- | --- |
| markdown-it (VitePress, many static sites) | Yes | This plugin |
| MDX, Docusaurus, Astro | Yes | The `<iframe>` itself, or the custom element |
| The Markdown preview of VS Code and of Cursor | No, and no script from another host | The [extension](#in-vs-code-and-cursor), which is this plugin writing an element |
| GitHub, GitLab, npm | **No**, removed for safety | A [Markdown image](#3-markdown-image) |

The plugin turns a fenced block tagged `tritone` into the embed:

````markdown
```tritone
story Hello
a(laptop)[You]
b(server)[API]
step Ask
  a -> b: GET /
```
````

A word after `tritone` is the [model](models.md) a block of Mermaid text is drawn in:

````markdown
```tritone city
flowchart LR
  web[Web] --> api[API]
```
````

```js
import markdownIt from 'markdown-it'
import tritone from 'tritone/markdown-it'

const md = markdownIt().use(tritone, { host: 'https://your-host' })
```

| Option | Default | Meaning |
| --- | --- | --- |
| `host` | required, but with `'element'` | Address of the hosted site, without a trailing slash |
| `as` | `'iframe'` | `'image'` writes a Markdown-safe `<img>` instead. `'element'` writes the text itself |
| `height` | `560` | Height of the iframe, in pixels |

With `as: 'element'` there is no hosted site and no frame. The block becomes a `<pre class="tritone">` that holds
its text, escaped, with the model in `data-model`, and the page that shows it loads Tritone and calls
[`run()`](#4-custom-element). Until it does, and where no script runs, a reader sees the text.

```js
const md = markdownIt().use(tritone, { as: 'element' })
```

The plugin has no dependencies, not even markdown-it. Plugins for remark and other renderers do not exist yet.

### In VS Code and Cursor

VS Code and Cursor carry the same Markdown preview. It allows no frame and no script from another host, and it
lets an extension add both a kind of code block and the script that draws it. Tritone's extension, in
`apps/vscode`, is this plugin with `as: 'element'` and a script that mounts each block: in the preview of a
Markdown file a `tritone` block is the player, as a [canvas](#the-canvas), in the theme of the editor.

- Every edit draws the new text on the step and the view its reader was at.
- A block that is not a story is read as Mermaid text and drawn in 3D, in the model named after the tag, as in
  ```` ```tritone city ````. The preview may not fetch Mermaid, so Mermaid is a file of the extension, the version
  the live editor loads: the script asks for it the first time a block needs it, and a file of stories never does.
- An actor that was dragged stays there until the next edit. The preview cannot write `at col,row` into the file:
  write it by hand to keep it.
- Cursor also shows a `.md` file as rendered text in the editor itself. That view is Cursor's own and runs no
  extension: the diagram is in the preview, which **Markdown: Open Preview** opens.

The extension is on no marketplace yet. CONTRIBUTING.md says how to
run it in a window of the editor, and how to package it.

## 3. Markdown image

A story can be a single SVG file that plays by itself, with no script. An image is the one thing every Markdown
renderer keeps:

```markdown
[![How an order is placed](https://your-host/svg/c3Rvcnkg….svg)](https://your-host/live.html?view#c3Rvcnkg…)
```

The image is the first half, and it is enough by itself. The link around it is what the editor's **Share** window
adds: nothing in an image can be pressed, so a press on the picture opens the diagram for reading, with its
controls.

- It plays every step in turn and starts again, with the step number and title as a caption.
- A viewer that plays no animation at all (a rasteriser, a preview of a file) shows the last step, settled, with
  every actor on the floor. One that plays it starts on the first step, as written: an actor arrives with the step that
  first mentions it, so name in the first step what a reader should see at once.
- It brings its own paper, so it reads on a page of any colour. Paper and ink follow the reader's light or dark
  setting, or the `color-scheme` of the page around it where the browser hands that to an image (Chromium does,
  Safari does not).
- It is drawn with CSS that a browser reads (custom properties, `light-dark()`, `color-mix()`). What shows an SVG
  with a renderer of its own, such as the one macOS draws file icons with, loses its colours: give those a PNG,
  which the editor saves. So does an e-mail: most mail programs show no SVG at all.
- The same address is the same picture until the renderer changes, so whoever fetched it keeps it for a day:
  a browser, and the proxy GitHub shows images through. A better renderer reaches a page that already showed the
  picture the day after.
- It has no narration, metrics or controls: those belong to the player.
- It works for **stories only**. Mermaid text is drawn in the browser, so it has no image form.
- The source is in the path, so unlike the iframe the server does see it, and so does a proxy that fetches the
  image for a page, as GitHub's does. Addresses are limited to 12,000 characters.

You can also commit the file: `tritone.render(text)` returns the same SVG, and the picture at the top of the
README is one. With a step, `tritone.render(text, { step: 2 })`, the SVG is that step alone, settled and with no
animation: a still picture, which is what the editor turns into a PNG. With `paper: false` it has no paper of its
own: for a drawing put in a page over something the page draws behind it.

## 4. Custom element

```html
<script type="module">
  import tritone from './tritone.js' // bun run build:lib writes packages/tritone/dist/tritone.js
  tritone.defineElement() // <tritone-diagram>
</script>

<tritone-diagram>
story Hello
a(laptop)[You]
</tritone-diagram>
```

The element reads its own text and an optional `model` attribute. It renders into itself, with no shadow root, so
the page's [theme properties](#theming) reach it. This is the recommended bridge to React, Vue, Svelte and the
rest: they all render custom elements.

`tritone.run()` does the same for existing markup, the way Mermaid does: every `<pre class="tritone">` becomes a
diagram, and `data-model` picks the model. `apps/live/inline.html` shows both, at `/inline.html` of a hosted site.

## 5. API

Everything is exported from `index.ts`.

| Function | Does |
| --- | --- |
| `detect(text)` | Returns the diagram type that will draw the text: `story`, `graph`, or a registered one |
| `render(text, options)` | Returns `{ svg, type }`: a complete SVG that needs no script, with its own paper. A story plays every step in a loop, or with `step` (counted from 0) is that step alone, still; `paper: false` leaves its paper out. Works without a DOM for stories. For Mermaid text it first fetches [what draws it](#what-a-page-loads) |
| `mount(element, text, options)` | Puts the interactive diagram in the element. Mounting again replaces it, whatever type of diagram was there. It resolves to the diagram's handle: `view`, and for a story `show(step)` and `step`, the one on screen. For Mermaid text it first fetches [what draws it](#what-a-page-loads) |
| `arrange(text, moved)` | Returns the text with what a reader moved written into it: `moved` is the `detail` of a `tritone:move` event. The rest of the text is untouched |
| `run({ querySelector })` | Mounts every matching element. Default selector: `.tritone` |
| `defineElement(name)` | Defines the custom element. Default name: `tritone-diagram` |
| `initialize({ mermaid, model })` | Passes the Mermaid object, if it is not a global, and the default model |
| `encode(text)`, `decode(source)` | Turn a diagram text into the `<source>` of an address, and back. `decode` reads base64 and percent-encoded text alike |
| `registerKind`, `registerModel`, `registerDiagram` | See [plugins](plugins.md) |

Options:

| Option | For | Meaning |
| --- | --- | --- |
| `model` | Mermaid text | The model that places the graph |
| `step` | Stories | The step to open on, counted from 0 |
| `canvas` | Both | `true` shows the scene as a [canvas](#the-canvas) |
| `view` | Both | The `view` of the handle of the diagram that was in the element, to open where the reader was |

One more function has an entry of its own, so a page that only shows code does not load the rest:

```js
import { highlight } from 'tritone/highlight'

pre.innerHTML = highlight(text) // one <span class="ln"> per line, the text escaped
```

It paints story text for a `<pre>`: a class per kind of token (`k` statement, `t` title, `a` actor, `d` kind, `s`
label, `o` arrow, `n` value, `c` comment), and no colours of its own. Mermaid text gets its comments painted and
nothing else. The live editor and these pages use it.

Errors are thrown as exceptions with a readable message; `run()` and the element write that message in place of the
diagram.

## What a page loads

The package has four entries, and each carries what it needs and nothing else. The weights are minified and
compressed with gzip, measured in November 2026; a test holds every one of them to a budget.

| Entry | Holds | Weight |
| --- | --- | --- |
| `tritone` | The [API](#5-api), and all a story needs | 12.6 kB |
| `tritone/highlight` | `highlight` | 0.5 kB |
| `tritone/markdown-it` | The [Markdown plugin](#2-markdown-plugin) | 0.5 kB |
| `tritone/server` | `handleSvg`, which answers for a [Markdown image](#3-markdown-image): stories only | 7.7 kB |

`tritone` does not carry the code that draws Mermaid text. `render` and `mount` fetch it with `import()` the first
time a text is not a story: the graph pipeline once, 9.8 kB, and the model the text is drawn in, 0.8 to 1.5 kB
each. A page of stories never asks for either, and a graph asks for its own model and no other. Nothing changes in
the way they are called: both already returned a promise. `detect`, `arrange`, `encode` and `decode` answer at
once and fetch nothing, and a model added with [`registerModel`](plugins.md#models) is there from the start.

A bundler that splits code, as Vite, Rollup, webpack, esbuild and Bun do, turns each of those fetches into a file
of its own: the weights above are what a page of such a project loads.

`bun run build:lib`, for a page with no bundler, does not split. It writes one file,
`packages/tritone/dist/tritone.js`, and Bun puts every part inside it: 24.3 kB, one request, and nothing fetched
later. It is the simplest to copy and the quickest to start.

To have the parts as files that are fetched when a text needs them, build with `--splitting`, into a folder of
its own:

```bash
bun build packages/tritone/src/index.ts --outdir packages/tritone/dist/split --entry-naming tritone.js --splitting --target browser --minify
```

Copy the whole folder next to the page, not its `tritone.js` alone: the files ask for each other by name. A page
of stories then fetches three of them, the 12.6 kB above, and a first graph adds the pipeline and one model. Two
of the three are asked for only once `tritone.js` has arrived, so the first story is drawn one round trip later
than from the one file: half the bytes, for a slower start.

## How a story fits its element

The player of a story follows the width of its own element, not of the window: in a narrow column of a wide page
it is a narrow player.

| Width of the element | Layout |
| --- | --- |
| Under 720px | One column, in the order a reader needs: title, metrics, scene, its controls, narration, **Back** and **Next**, the list of steps |
| From 720px | The narration stands beside the scene, in a column 240 to 380px wide, with the metrics above it. **Back** and **Next** are under it, in the row of the scene's controls, and the list of steps has the whole width |

What a story holds changes the layout too:

- A story with nothing to say (a still picture) gives the whole width to its scene.
- A long narration makes the player taller, never wider. **Back** and **Next** have a row of their own, so in a
  [canvas](#the-canvas) they stay where they are from step to step, as long as the narration fits its element.
- A long list of steps scrolls sideways and keeps the step on screen in sight. **Next** ends a long title with
  dots, and a word no line can hold is broken.
- The hint about the keys is left out where there is no pointer that hovers: on a phone.

## The canvas

By default a diagram is a figure in the flow of a page: it is as wide as its element and as tall as it needs.
With `canvas: true` the scene takes the room its element has, and the reader moves around in it:

| To | Do |
| --- | --- |
| Zoom | Pinch, or `Ctrl` and the wheel, on the point to look at; the **−** and **+** buttons; `+` and `-` |
| Move the view | Drag the floor of a story; in a graph view, where a drag rotates, drag with `Shift` or the middle button. Scroll, once the view is zoomed or moved. `Shift` and an arrow key |
| See everything again | **Fit**, or `0` |
| Move an actor of a story | Drag it to another floor cell |
| Move a node of a graph view | Drag it: a drag that starts on a node moves it, any other turns the scene |

Give the element a height: a canvas fills it. The embed page and the live editor are canvases. The scene of a
story takes the height the rest of the player leaves, and never less than 180px; under 720px of width it keeps 62%
of that width, up to 320px. What does not fit makes the player taller than its element, and the page around it
scrolls.

While the whole drawing is in sight the wheel is left to the page, so a diagram in the middle of an article never
stops a reader who is scrolling past it.

What was dragged stays there for as long as the diagram is on screen. To keep it, write it into the text: a place
is a line of it, [`at col,row`](language.md#where-actors-stand) for an actor and a
[comment](graph.md#moved-nodes) for a node. The element fires `tritone:move`, and `arrange` writes what its
`detail` says:

```js
element.addEventListener('tritone:move', (event) => {
  text = arrange(text, event.detail)
})
```

`event.detail.id` is what was dragged, and `event.detail.at` has the place of everything that has one, not only
of that: one written place would shift the automatic places of the actors declared after it.

To draw a changed text without losing the reader's place, hand the old diagram's view to the new one:

```js
let diagram = await mount(element, text, { canvas: true })
diagram = await mount(element, newText, { canvas: true, step: diagram.step, view: diagram.view })
```

## Theming

Diagrams follow the reader's light or dark setting. A page with a theme of its own sets `--tritone-scheme` to
`dark` or `light` on any ancestor of the diagram, and the diagram takes that one:

```css
:root[data-theme="dark"] { color-scheme: dark; --tritone-scheme: dark; }
```

To change colours, set these CSS custom properties on any ancestor of the diagram; the ones you leave out keep
their default.

| Stories | |
| --- | --- |
| `--tritone-paper`, `--tritone-card` | Background of the player and of the labels in the scene, and the tint of a button under the pointer |
| `--tritone-ink`, `--tritone-soft` | Outlines and text, and secondary text |
| `--tritone-grid` | Floor grid |
| `--tritone-sky`, `--tritone-amber`, `--tritone-lilac`, `--tritone-mint`, `--tritone-coral`, `--tritone-sand` | The six colour families of the shapes. Mint and coral also colour responses and requests |

| Graph views | |
| --- | --- |
| `--tritone-bg`, `--tritone-surface`, `--tritone-line` | Widget background, the top of a node and the edge around a label, plane borders |
| `--tritone-fg`, `--tritone-muted` | Text, and secondary text and idle edges |
| `--tritone-accent` | Selection, active edges, packets, focus |
| `--tritone-group-1` … `--tritone-group-5`, `--tritone-group-none` | Group colours, in turn, and nodes with no group |

An SVG used as an image cannot see the page's properties, so it always uses the defaults. A story brings its own
paper, white or near black, with its ink on it, so it reads on any surface. Which of the two it picks is the
reader's setting or, in the browsers that pass it on, the `color-scheme` of the element that shows it: Chromium
passes it on, Safari does not. To hold it to one, write `style="--tritone-scheme:dark"` (or `light`) on the `<svg>`
of the file. Where the page has a theme of its own, put the SVG itself in the page, as this site does: it then
reads `--tritone-scheme` and `--tritone-paper` like any diagram.

## Keyboard and accessibility

| Key | Stories | Graph views |
| --- | --- | --- |
| `←` `→` | Previous and next step | Rotate |
| `↑` `↓` | | Tilt, where the model allows |
| `R` | Replay the step | |
| `Space` | Pause and resume | Select the focused node or bar |
| `Enter` | Activate the focused button | Select the focused node or bar |
| `Esc` | | Clear the selection |
| `+` `-` `0` | Zoom in, zoom out, show everything, in a canvas | The same |
| `Shift` `←` `→` `↑` `↓` | Move the view, in a canvas | The same |

- Every control is a real button; the narration and the node panel are announced when they change.
- With `prefers-reduced-motion`, nothing moves: stories show each step as it ends.
- On touch screens, a horizontal drag rotates a graph view and a vertical one scrolls the page. In a canvas a drag
  belongs to the scene, and the buttons zoom: there is no two-finger pinch yet.
- An actor or a node is moved with a pointer. From the keyboard its place is written in the text.