SelfscribeDownloadOpen the app

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
~~~ 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:

js
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.

  • name must be new: a built-in's name is refused.
  • rules must include one that ends in the plugin's name (-> countdown). verbatim keeps the

    body as text for you; without it the body is parsed as Selfscribe and you get its HTML.

  • hydrate(element, ctx) runs for every element with data-self-plugin="NAME", in the

    preview, 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 of hydrate gives you settings (declare them in

    settings: [{ key, label, kind: "bool" | "text" | "list" | "choice", default }]; users set them as set plugin-NAME-KEY VALUE in 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.

  1. Pick the rule. A fenced block with your name is the usual choice:

    block ` map ... ` -> map verbatim. The body reaches you as plain text.

  2. Bundle the library into one file. The app loads a plugin as a single module, so

    import statements of other files do not work. Build with your bundler in library mode (Vite: build.lib with formats: ["es"]) so Leaflet ends up inside map.js. Put the library's CSS in the plugin's css string.

  3. Draw in hydrate. Read element.textContent, make a div inside the element, and give

    it to the library. Size it from the element (element.clientWidth), never the window. Never put document text into HTML: set textContent, or give a library a text node (bindTooltip(node), not bindTooltip(name)). A document may come from anyone, and a plugin runs with the app's rights.

  4. Keep it quiet. contenteditable="false" on anything interactive; no network requests

    unless the plugin lists the hosts in its network field, 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 lists tile.openstreetmap.org: its tiles are pictures from the web, which the desktop app allows and a website must allow in its Content-Security-Policy.

  5. Try it. In the desktop app, drop map.js in the plugins folder and press Reload after

    each change; on the web, install it from the Manager, and install the file again after each change: the same name updates it (raise version so the question says which one it replaces). Open a document with a map block in Preview.

What to know:

  • hydrate runs 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-in sources plugin does both, and watches the page for blocks that come and go.

  • Clean up. Return a function from hydrate (stop timers, remove listeners, destroy the

    library's widget). Hosts call it through dispose(root) from selfscribe/client when 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 call dispose too, or the CodeMirror package's setBlockDisposer(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.

PluginRuleYou writeIt draws
mindmap`block ` mindmap ... ` -> mindmap verbatim`an indented lista map that branches left to right; click a node to fold its branch
sparkinline ~[_]~ -> 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
meterinline [=_=] -> meter verbatim[= 72 / 100 =], [= 3 of 8 =], [= 45% =]a small filled bar with the numbers
untilinline ((_)) -> 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 marksbars 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 alonea 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

#
FieldRequiredWhat it is
nameyesthe name documents use: lower-case letters, digits and -; not a built-in's
title, summaryno, but show themshown on the Manager card (the name is used when missing)
descriptionnolonger help, shown on the plugin's page
rulesyesrule lines the Manager's "Add to this document" writes; one must make the name
examplesno{ title, body } snippets shown on the plugin's page
cssnoCSS added to the page once
versionnothe plugin's own version, shown in the Manager
networkonly if it goes onlinethe hosts it contacts (["tile.openstreetmap.org"]), said before it is installed
creditsnothe libraries inside and their licences
settingsno{ key, label, kind, default }; kind is bool, text, list or choice
hydrate(element, ctx)one of the twodraws one element; may return a clean-up function
create({ settings, host })one of the tworeturns { 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 per key for the life of the page;
  • edits (only where the document can be edited): propose a change to the text the element

    came from.

host in create may offer (check before use; each is missing where the host cannot do it):

  • resolveMedia(element, path): a blob: URL for a file next to the document element came

    from, or null;

  • loadDocument(element, path): another document of the folder, drawn; readDocument(key) and

    writeDocument(key, text): its text, to read and to replace (as one undo step);

  • openDocument(key, fragment), onDocumentChange(listener), hydrateIn(root): open a

    document, hear about changes, draw plugins inside a part you made.

Outside the app

#

The same file works on any page that uses the engine:

js
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.