Writing plugins for Grimoire and Cantrip

Jennifer

One JSON request in, one JSON reply out: how a diagram or a formula gets into a book and a slide deck, with grimoire-mermaid and grimoire-katex as the worked examples.

November 2026, by @mplx.
Based on Jennifer 0.27.0, Grimoire 0.6.1 and Cantrip 0.7.3

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 20

The plan

PartWhat
The contractrequest, reply, versions, features
Finding and trustingnames, PATH, grimoire plugins
A skeletongrimoire-shout, in this talk's code folder
grimoire-mermaidfences, generated files, names
grimoire-katexMathML, display and inline, safety
Limitscaps, timeouts, errors
Shippingtests, layout, jvc app install
2 / 20

Why a program, not a module

Plugins sinceKinds
Grimoire0.4.0 (files, mathml: 0.5.0)preprocessor, renderer
Cantrip0.6.0preprocessor only
3 / 20

Naming one

# grimoire.toml                    # cantrip.toml, beside talk.md
[preprocessor.mermaid]             [preprocessor.mermaid]

[preprocessor.katex]               [preprocessor.katex]
program = "/opt/katex/bin/katex"   inline = false
HostTries, in order
Grimoireits own plugins/ dir, then PATH: grimoire-NAME, then cantrip-NAME
CantripPATH: cantrip-NAME, then grimoire-NAME
eithercommand = "tools/x": a path, used as written
4 / 20

The request, as Cantrip sent it

{"api": 1, "kind": "preprocessor",
 "features": ["files", "mathml"],
 "book": {"title": "Plugin deck", "authors": [], "language": "",
   "src": "/home/me/deck", "out": "/home/me/deck",
   "generated": "/tmp/cantrip-plugins-29/_generated"},
 "plugin": {"name": "dump", "settings": {"style": "dark"},
   "files": "/tmp/cantrip-plugins-29/_generated/dump"},
 "entries": [{"kind": "page", "title": "A diagram",
   "src": "talk.md", "out": "talk.md", "level": 0, "number": "1"}],
 "chapters": [{"src": "talk.md", "content": "---\ntheme: ..."}]}
5 / 20

The reply, and the rules

{"api": 1, "chapters": [{"src": "talk.md", "content": "..."}]}
Rule
apimust be 1; refuse a request with another number
sparseonly the chapters you changed; a chapter the book lacks is an error
no new chaptersthe outline decides what a book holds
exit statusnot 0 stops the build; your stderr is the message
stdoutthe reply and nothing else; diagnostics go to stderr
deterministicsame request, same reply, same file names
6 / 20

One run, step by step

grimoire-mermaid in one build

  1. The host writes the request to the plugin's stdin and waits
  2. The plugin hands each diagram to its renderer
  3. The renderer writes an SVG and a PNG into plugin.files
  4. The reply links them as _generated/mermaid/<hash>.svg
7 / 20

Who runs what

$ cantrip plugins talk.md
preprocessor  mermaid  ->  /usr/bin/grimoire-mermaid  (found on PATH)
preprocessor  katex    ->  /usr/bin/grimoire-katex  (found on PATH)

$ cantrip build talk.md
cantrip: warning: preprocessor mermaid: runs /usr/bin/grimoire-mermaid,
         found on PATH rather than in this deck
8 / 20

A skeleton: grimoire-shout

code/bin/grimoire-shout     the launcher: stdin in, exit status out
code/src/shout.j            the module: every step of the contract
code/src/shout_test.j       ten tests, spliced in by jennifer test
export func run(text as string) {
    try {
        io.printf("%s", replyFor(request($text)));
        return 0;
    } catch (e) {
        io.eprintf("%s: %s\n", NAME, $e.message);
        return 1;
    }
}
9 / 20

Check the request, then the settings

if (json.typeOf($req, "/api") != "int" or
    json.asInt($req, "/api") != API) {
    fail("this plugin speaks api 1 only");
}
if (json.asString($req, "/kind") != "preprocessor") {
    fail("this is a preprocessor");
}
for (def key in json.keys($req, "/plugin/settings")) {
    if (not (inList(SETTINGS, $key) or inList(HOST_KEYS, $key))) {
        fail("unknown setting `{$key}`");
    }
}
10 / 20

Fences, the CommonMark way

```shout
plugins are programs
```

````markdown
```shout
this one is an example, inside a longer fence
```
````
11 / 20

Caps and a timeout

def started as time.Time init time.now();
while (not os.poll($p)) {
    if (time.milliseconds(time.sub(time.now(), $started)) >= $ms) {
        os.kill($p);
        os.wait($p);
        os.release($p);
        throw Error{kind: "timeout", message: "over {$ms} ms"};
    }
    time.sleep(time.fromMilliseconds(10));
}

The hosts wait for a plugin without a timeout, and so does os.run: bound your own child processes, and cap input and output sizes.

12 / 20

grimoire-mermaid: a fence becomes a picture

```mermaid
flowchart LR
  accTitle: From Markdown to a book
  A[Markdown] --> B[Site]
```
![From Markdown to a book](_generated/mermaid/6b79d22118f154bc.svg)
SettingDefault
renderer"merman"or "mmdr"
theme, font, png, scale, jobspicture and speed
13 / 20

grimoire-mermaid: names and determinism

return kit.contentName(
    [
        "grimoire-mermaid 1",
        $r.settings.renderer,
        $r.version,
        $r.settings.theme,
        $r.fontDigest,
        $png,
        scaleText($r.settings.scale),
        $source
    ],
    NAME_DIGITS);
14 / 20

grimoire-katex: TeX in, MathML out

Inline maths sits in the line: $`a^2 + b^2 = c^2`$.

```math
\sum_{i=1}^{n} i = \frac{n(n+1)}{2}
```
15 / 20

grimoire-katex: safe by construction

def out as list of string init [
    "-F", "mathml",
    "-e", MAX_EXPAND,     # at most 1000 macro expansions
    "-s", MAX_SIZE,       # no size above 100 em
    "-c", ERROR_COLOUR    # spot what KaTeX drew instead of failing
];
16 / 20

What a reader sees when it fails

$ cantrip build talk.md
cantrip: preprocessor katex: exited 1: grimoire-katex: talk.md:16:
  KaTeX parse error: Unexpected end of input in a macro argument,
  expected '}' at end of input: ...2\,dx = \frac{1

cantrip: preprocessor mermaid: exited 1: grimoire-mermaid: talk.md:8:
  merman-cli: Diagram parse error (flowchart-v2): unexpected end
  of input

cantrip: preprocessor mermaid: exited 1: grimoire-mermaid:
  unknown setting `bogus`; the settings are renderer, program, ...
17 / 20

Testing a plugin

TestHow
fences, settings, rewritepure functions, white-box overlay NAME_test.j
refusalstesting.assertThrows("unclosed", "plugin")
the renderer's sidetests/fake-renderer, a shell stand-in set as program
real outputsame bytes twice, when the real tool is installed
end to endscripts/check-example.sh: build examples/book
jennifer test src/shout_test.j        # from code/: 10 passed
18 / 20

Shipping one

bin/grimoire-NAME     the launcher
src/NAME.j            the module
src/NAME_test.j       its tests
src/pluginkit.j       a copy of Grimoire's kit, if you use it
deck.toml             unscoped name, bin = "bin/grimoire-NAME"
NEEDS                 one required command per line
jvc app install https://github.com/mplx/grimoire-katex
19 / 20

Read more

Based on Jennifer 0.27.0, Grimoire 0.6.1, Cantrip 0.7.3, November 2026.

20 / 20