The way I labeled the subject, it may appear quite far from OCaml .
Typst is an interesting markup-based typesetting system. It’s fast. The language has a lot of functional traits.
Typst plugins are based on Webassembly modules. I think that OCaml can now target Wasm.
But both Typst and OCaml have some constraint regarding Wasm: I wonder whether both worlds meet somewhere, allowing us to write Typst plugins in OCaml?
It seems possible in principle… provided that Typst’s Wasm engine supports the Wasm GC extension, which I’m not certain about. In my understanding however, that extension should make its way into the standard soon.
It also requires to compile OCaml to Wasm modules that don’t depend on a Javascript environment, which is not the case of the files produced by the current wasm_of_ocaml, but is a work in progress. I don’t know about Wasocaml, maybe it would allow it more easily.
It would also likely require some code generation to make the glue between the very low-level interface required by Typst (some integers and a buffer) and OCaml parameters and return value.
Wasmi currently doesn’t support GC, but Wasmtime does, and has a very similar interface to Wasmi, so the conversion isn’t too difficult (side-note, I’m neither a typst, wasmi or wasmtime developer, nor do I use Rust regularly, so there might be some issues in that conversion – any are my fault and any that didn’t make it there are due to Rust’s type system).
But I haven’t got any further with it than this.
An alternative could be to use typst query, e.g.
Here is some OCaml code
```ocaml
let x = 1 + 2;;
``` <ocaml>
And here is some more
```ocaml
print_endline "Hello, world!";;
``` <ocaml>
Then if you do typst query test.typ '<ocaml>' it gives you a JSON list of dicts containing text e.g.