TypstDocumentation

plugin

A WebAssembly plugin.

This is advanced functionality and not to be confused with Typst packages.

Typst is capable of interfacing with plugins compiled to WebAssembly. Plugin functions may accept multiple byte buffers as arguments and return a single byte buffer. They should typically be wrapped in idiomatic Typst functions that perform the necessary conversions between native Typst types and bytes.

Plugins run in isolation from your system, which means that printing, reading files, or anything like that will not be supported for security reasons. To run as a plugin, a program needs to be compiled to a 32-bit shared WebAssembly library. Many compilers will use the WASI ABI by default or as their only option (e.g. emscripten), which allows printing, reading files, etc. This ABI will not directly work with Typst. You will either need to compile to a different target or stub all functions.

Example

#let myplugin = plugin("hello.wasm")
#let concat(a, b) = str(
  myplugin.concatenate(
    bytes(a),
    bytes(b),
  )
)

#concat("hello", "world")
Preview

Protocol

To be used as a plugin, a WebAssembly module must conform to the following protocol:

Exports

A plugin module can export functions to make them callable from Typst. To conform to the protocol, an exported function should:

Imports

Plugin modules need to import two functions that are provided by the runtime. (Types and functions are described using WAT syntax.)

Resources

For more resources, check out the wasm-minimal-protocol repository. It contains:

Constructor
Question mark

Creates a new plugin from a WebAssembly file.

plugin() -> plugin

path
str
RequiredPositional
Question mark

Path to a WebAssembly file.