Writing a Selfscribe plugin
A plugin draws something richer than text: a countdown, a map, a score, a calendar. A document asks for it in its rules block, by naming the plugin as what a block becomes, exactly as it asks for a built-in one:
~~~ selfscribe
use markdown
block ``` countdown ... ``` -> countdown verbatim
~~~
```countdown
2026-12-25 Christmas
```
The engine never runs plugin code. It turns the block into
<pre data-self-plugin="countdown">2026-12-25 Christmas</pre> (every renderer can show that),
and the app hands that element to your plugin to draw. So a document stays plain text, safe
to open anywhere, and still reads well where your plugin is not installed.
The file
#A plugin is one JavaScript module whose default export describes it:
export default {
name: "countdown", // the name documents use: lower-case letters, digits and "-"
title: "Countdown",
summary: "Days until a date.", // one line for the Manager card
description: "Longer help…", // optional
rules: ["block ``` countdown ... ``` -> countdown verbatim"], // added by "Add"
examples: [{ title: "Christmas", body: "```countdown\n2026-12-25 Christmas\n```" }],
css: ".self-countdown { display: flex; gap: 8px }", // optional, added once
hydrate(element, ctx) {
// `element` is what the engine drew; its text is the block's body.
const days = /* … */ 0;
element.setAttribute("contenteditable", "false"); // if it is interactive (Write mode)
element.replaceChildren(/* your DOM */);
},
};
The whole of examples/plugins/countdown.js is a working plugin to start from, and
tools/plugin-starter is a small project (a clock) that bundles a
plugin into one file and tests it: copy the folder, rename, npm install, npm run build.
In TypeScript, definePlugin({ … }) from selfscribe-plugins checks the fields as you type.
namemust be new: a built-in's name is refused.rulesmust include one that ends in the plugin's name (-> countdown).verbatimkeeps thebody as text for you; without it the body is parsed as Selfscribe and you get its HTML.
hydrate(element, ctx)runs for every element withdata-self-plugin="NAME", in thepreview, Write mode, hybrid widgets, pages, slides and gallery thumbnails. Draw inside the element; do not reach outside it.
ctx.edits(when present) proposes changes to the document's text, which the app checks and applies as one undo step.create({ settings, host })instead ofhydrategives you settings (declare them insettings: [{ key, label, kind: "bool" | "text" | "list" | "choice", default }]; users set them asset plugin-NAME-KEY VALUEin preferences.self) and returns{ name, hydrate }.- Colours: use the page's tokens (
var(--doc-fg),var(--doc-link),var(--doc-border)),so every theme, light and dark, draws it.
Step by step: wrapping a JavaScript library
#Say you want maps from lines like London, 51.5072, -0.1276, drawn with a library such as
Leaflet. The finished plugin is in
tools/plugin-examples/map: copy it, or follow along.
- Pick the rule. A fenced block with your name is the usual choice:
block`map ...`-> map verbatim. The body reaches you as plain text. - Bundle the library into one file. The app loads a plugin as a single module, so
importstatements of other files do not work. Build with your bundler in library mode (Vite:build.libwithformats: ["es"]) so Leaflet ends up insidemap.js. Put the library's CSS in the plugin'scssstring. - Draw in
hydrate. Readelement.textContent, make adivinside the element, and giveit to the library. Size it from the element (
element.clientWidth), never the window. Never put document text into HTML: settextContent, or give a library a text node (bindTooltip(node), notbindTooltip(name)). A document may come from anyone, and a plugin runs with the app's rights. - Keep it quiet.
contenteditable="false"on anything interactive; no network requestsunless the plugin lists the hosts in its
networkfield, since readers expect documents to stay offline. The app shows that list before installing (it is the plugin's own claim, so install only plugins you trust). The map liststile.openstreetmap.org: its tiles are pictures from the web, which the desktop app allows and a website must allow in its Content-Security-Policy. - Try it. In the desktop app, drop
map.jsin the plugins folder and press Reload aftereach change; on the web, install it from the Manager, and install the file again after each change: the same name updates it (raise
versionso the question says which one it replaces). Open a document with amapblock in Preview.
What to know:
hydrateruns again whenever the block is drawn again (an edit nearby, switching modes).Keep it idempotent: clear the element first.
- To see the whole document, for a number that depends on what comes before it, read the
text with
ctx.edits.read(element, null)where it is offered. The editor's preview has only the blocks on screen in the page, so the page is not the whole document there. Elsewhere (an export, a static page) read the page around the element,element.closest(".self-doc"). The built-insourcesplugin does both, and watches the page for blocks that come and go. - Clean up. Return a function from
hydrate(stop timers, remove listeners, destroy thelibrary's widget). Hosts call it through
dispose(root)fromselfscribe/clientwhen they throw rendered output away; the Selfscribe app does so for the preview, hybrid mode, the pages and slides views, and whenever a plugin is drawn again. (A page of your own that removes output should calldisposetoo, or the CodeMirror package'ssetBlockDisposer(dispose).) - See what went wrong. With Settings › App and updates › Developer tools on, the
Developer panel lists every plugin block in the preview and whether it drew (done, error, missing library), with a button to draw them again after you change the file.
- Errors are caught: a plugin that throws keeps its block as text, with a red line above it
("The clock plugin could not draw this: …"), and the rest of the document still draws. The browser's developer tools show the full error.
- Plugins installed from the Manager are kept in the app's storage (IndexedDB in a browser,
the app's folder on the desktop), so a plugin with a large library inside is fine.
Small built-in plugins to learn from
#These built-ins are short, need no library, and read well as plain text where they are not
drawn. Each is one file in packages/selfscribe-plugins/src/client/, a good start for your own.
| Plugin | Rule | You write | It draws |
mindmap | `block ` mindmap ... ` -> mindmap verbatim` | an indented list | a map that branches left to right; click a node to fold its branch |
spark | inline ~[_]~ -> spark verbatim | ~[3 5 2 8 6]~, ~[bar: 4 7 3]~ | a sparkline as tall as the text; hover or arrow keys read the values |
meter | inline [=_=] -> meter verbatim | [= 72 / 100 =], [= 3 of 8 =], [= 45% =] | a small filled bar with the numbers |
until | inline ((_)) -> until verbatim | ((2026-12-24 Christmas)) | "Christmas in 3 days", then "today", counting while the page is open |
poll | `block ` poll ... ` -> poll verbatim` | a question, then options like - Tea (3), or with tally marks | bars of votes; a click adds a vote to that line of the text |
heatmap | `block ` heatmap ... ` -> heatmap verbatim` | 2026-09-14 3 per line, or dates alone | a calendar of weeks, darker for busier days |
The poll shows how a plugin writes back: it reads the document with ctx.edits.read, finds its
own block by its text, and proposes a change to one line with ctx.edits.propose, which the
app applies as one undo step. Without ctx.edits (a gallery, a static page) it draws the bars
without buttons.
Reference
#| Field | Required | What it is |
name | yes | the name documents use: lower-case letters, digits and -; not a built-in's |
title, summary | no, but show them | shown on the Manager card (the name is used when missing) |
description | no | longer help, shown on the plugin's page |
rules | yes | rule lines the Manager's "Add to this document" writes; one must make the name |
examples | no | { title, body } snippets shown on the plugin's page |
css | no | CSS added to the page once |
version | no | the plugin's own version, shown in the Manager |
network | only if it goes online | the hosts it contacts (["tile.openstreetmap.org"]), said before it is installed |
credits | no | the libraries inside and their licences |
settings | no | { key, label, kind, default }; kind is bool, text, list or choice |
hydrate(element, ctx) | one of the two | draws one element; may return a clean-up function |
create({ settings, host }) | one of the two | returns { name, hydrate }, with the user's settings |
ctx in hydrate has:
name: the plugin's name;root: the part of the page being drawn;warnOnce(key, ...data): a console warning, shown once perkeyfor the life of the page;edits(only where the document can be edited): propose a change to the text the elementcame from.
host in create may offer (check before use; each is missing where the host cannot do it):
resolveMedia(element, path): ablob:URL for a file next to the documentelementcamefrom, or
null;loadDocument(element, path): another document of the folder, drawn;readDocument(key)andwriteDocument(key, text): its text, to read and to replace (as one undo step);openDocument(key, fragment),onDocumentChange(listener),hydrateIn(root): open adocument, hear about changes, draw plugins inside a part you made.
Outside the app
#The same file works on any page that uses the engine:
import { pluginFromModule, registerPlugins } from "selfscribe-plugins";
import clock from "./clock.js";
const { names } = registerPlugins({ extra: [pluginFromModule(clock, "clock.js")] });
// parse with { plugins: names }, put the HTML in the page, then hydrate(root)
pluginFromModule checks the file the way the app does and says what is wrong in words.
Sharing it
#Send the .js file, or a .zip with an index.js (bundle any libraries into it). People
install it from Tools › Manage plugins… › Install a plugin…. The app shows the name, the
rules it adds, its version, the hosts it contacts and a warning, and installs nothing until
they say yes.
To show those details, the app reads the file first in a frame of its own: it cannot reach the
app, its storage or your documents, cannot fetch anything, and is removed after a few seconds.
The file's top level does run there, so keep it to one thing: export default { … }, with the
work inside hydrate. A library that needs a page when it loads (Leaflet does) is fine there;
one that fetches at load time gets nothing. What the file declares when it is installed must
match what it declared in the frame (name, rules, the hosts it contacts, version), or it is
refused. A file whose top level never finishes can hold up the window for those few seconds in
browsers that run the frame on the page's thread. Installed plugins live
in the app's storage, load at every start, sit under Yours in the Manager, and go with
Remove.
In the desktop app there is also a plugins folder: Tools › Manage plugins… › Plugins folder
opens it (plugins/ in the app data folder; on Windows %APPDATA%\app.selfscriber\plugins).
Drop NAME.js there, or a folder NAME/ with an index.js, and press Reload (or restart).
Putting a file there is your own act, so there is no question; delete the file to remove the
plugin. A plugin installed from the Manager wins if both have one name. While writing a plugin,
keep it in the folder and press Reload after each change.
A plugin runs with the same rights as the app. Install only plugins from people you trust, and publish yours with its source.