Plugins
Tritone has three extension points. Each is one function call, made once, before the diagrams are drawn.
| To add | Call | Size of the job |
|---|---|---|
| A shape for story actors | registerKind |
A few lines |
| A way to lay out a Mermaid graph in space | registerModel |
One function |
| A whole new diagram, with its own language | registerDiagram |
A parser and a renderer |
A plugin is an ordinary module that imports tritone and calls one of them. There is no manifest and no loader.
Actor kinds
A kind is a colour family, a height, and a function that draws the shape standing on a point of the floor.
import tritone, { box, tube } from 'tritone'
tritone.registerKind('loadbalancer', {
tone: 'amber', // sky, amber, lilac, mint, coral or sand
top: 0.7, // height in floor units: notes are pinned above it
draw: (u, v) => box(u, v, 1.4, 0.7, 0.5) + tube(u, v, 0.25, 0.2, 0.5),
})
After that, lb(loadbalancer)[Load balancer] works in any story.
Four helpers do the isometric drawing, so shapes match the built-in ones:
| Helper | Draws |
|---|---|
box(u, v, w, d, h, z?) |
A box centred on floor point (u, v): w wide, d deep, h tall, from height z |
tube(u, v, r, h, z?) |
An upright cylinder |
ball(u, v, r, z) |
A sphere with its centre at height z |
iso(u, v, z?) |
The screen point [x, y] of a point in space, for anything else |
draw returns SVG markup. The faces carry the classes t (top), l and r (sides), which the theme fills from
the kind’s tone: do not set colours yourself, or the shape will ignore the theme and the dark mode.
A footprint of about 1.2 by 1.2 floor units sits well between its neighbours. draw must be pure: the same
arguments, the same markup, and no access to the page, because stories are also rendered on a server.
Models
A model places the nodes, groups and edges of a Mermaid graph in space. It is a pure function from a Graph to a
Scene; the contract is described in graph views.
import tritone, { type Model } from 'tritone'
const row: Model = (graph) => ({
items: graph.nodes.map((node, i) => ({
kind: 'ball',
layer: 0,
at: [(i - (graph.nodes.length - 1) / 2) * 2, 0, 0],
r: 0.4,
node: node.id,
})),
camera: { yaw: 20, pitch: 20 },
yawRange: [-180, 180],
pitchRange: [0, 45],
fog: true,
})
tritone.registerModel('row', row)
Then model: 'row', data-model="row" or ?model=row selects it. A reader can
move its nodes like those of any other model, with nothing more to write.
A registered model is the function you passed: it is there at once, and nothing is fetched for it. The built-in ones are each fetched the first time a text is drawn in them. Registering under the name of a built-in model replaces it, and that one is then never fetched. A name that is not registered is an error that lists the ones that are.
Diagram types
A diagram type owns a kind of text from start to finish, exactly as in Mermaid: it recognises the text, and it draws it.
import tritone, { type Diagram } from 'tritone'
const timeline: Diagram = {
id: 'timeline',
detect: (text) => /^\s*timeline\b/.test(text),
render: async (text, options) => '<svg>…</svg>', // a complete SVG that needs no script
mount: async (element, text, options) => {
element.innerHTML = await timeline.render(text, options)
},
}
tritone.registerDiagram(timeline)
detectis asked before the built-in types, most recently registered first. Returntrueonly for your own text: a first-line keyword, asstorydoes, is the convention.rendershould work without a DOM if you want your diagram drawn on a server or in a build:tritone.render(text)then returns its picture there. The image endpoint,handleSvg, carries the story code alone and draws stories only.mountowns the element it is given. If it adds listeners, it must cope with being called again on the same element.- Throw an
Errorwith a message a writer can act on. It is shown in place of the diagram. arrange(text, moved)is optional: write it if a reader can move things in your diagram. Firetritone:movefrom the element, and return here the text withmoved, the event’sdetail, written into it. That is how a place chosen with the pointer reaches the text, see the canvas.
The two built-in types are written this way and are the best examples:
diagrams/story/ and
diagrams/graph/.
Sharing a plugin
Nothing is published to npm yet, so there is no registry of plugins. When there is one, the suggested package name
is tritone-plugin-<what-it-adds>, with tritone as a peer dependency.