Skip to content

typst-community/wasm-minimal-protocol

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

176 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

wasm-minimal-protocol

Check wasm-minimal-protocol (macro) version on crates.io wasi-stub version on crates.io GitHub Downloads (all assets, all releases) Ask DeepWiki GitHub contributors

A minimal protocol to write Typst plugins.

Note that plugins require Typst version 0.8.0 or later.

You want to write a plugin

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.

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.

wasi-stub

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.

About

A minimal protocol to write Typst plugins.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors