Crates.io | wasm-minimal-protocol |
lib.rs | wasm-minimal-protocol |
version | 0.1.0 |
source | src |
created_at | 2024-10-08 15:50:36.530325 |
updated_at | 2024-10-08 15:50:36.530325 |
description | Typst plugin helper macro library |
homepage | |
repository | |
max_upload_size | |
id | 1401358 |
size | 26,065 |
A minimal protocol to write typst plugins.
Note that plugins require typst version 0.8
or more.
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.
See the example for your language:
If you have all the required dependencies, you may build all examples by running cargo test
.
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.