wasm-minimal-protocol

Crates.iowasm-minimal-protocol
lib.rswasm-minimal-protocol
version0.1.0
sourcesrc
created_at2024-10-08 15:50:36.530325
updated_at2024-10-08 15:50:36.530325
descriptionTypst plugin helper macro library
homepage
repository
max_upload_size
id1401358
size26,065
(astrale-sharp)

documentation

README

wasm-minimal-protocol

A minimal protocol to write typst plugins.

Note that plugins require typst version 0.8 or more.

You want to write a plugin

A plugin can be written in Rust, C, Zig, or any language than 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 https://typst.app/docs/reference/foundations/plugin/. You should also take a look at this repository's examples.

Examples

See the example for your language:

If you have all the required dependencies, you may build all examples by running cargo test.

wasi-stub

The runtime used by typst do 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, stubbing is almost certainly required.

Commit count: 0

cargo fmt