A minimal protocol to write Typst plugins.
Note that plugins require Typst version 0.8.0 or later.
A plugin can be written in Rust, C, Zig, Go, Haskell, MoonBit, or any language that compiles to WebAssembly.
Rust plugins can use this crate to automatically implement the protocol with a macro:
// Rust file
use wasm_minimal_protocol::*;
initiate_protocol!();
#[wasm_func]
pub fn hello() -> Vec<u8> {
b"Hello from wasm!!!".to_vec()
}// Typst file
#let p = plugin("/path/to/plugin.wasm")
#assert.eq(str(p.hello()), "Hello from wasm!!!")For other languages, the protocol is described at Plugin Function – Typst Documentation. You should also take a look at this repository's examples.
See the example for your language:
If you want to pass structured data to Typst, the Typst functions cbor and cbor.encode are available. Examples marked with 🌟 cover how it's done.
Some examples require wasi-stub. You can install wasi-stub or replace the commands with cargo run --manifest-path ../../crates/wasi-stub/Cargo.toml -- [<arguments>...].
If you have all the required dependencies, you may build all examples by running cargo test.
Refer to CONTRIBUTING for details.
See also WASM plugin development — Best of Typst (TCDM), which lists Zig wrapper, C protocol generator, and other projects that may facilitate development of plugins.
The runtime used by Typst does not allow the plugin to import any function (beside the ones used by the protocol). In particular, if your plugin is compiled for WASI, it will not be able to be loaded by Typst.
To get around that, you can use wasi-stub. It will detect all WASI-related imports, and replace them by stubs that do nothing.
If you are compiling C code with emcc, or compiling Haskell code, stubbing is almost certainly required.