Repository navigation
Description
Description
Hello everyone,
While developing a binding of Typst for the Lua language(it is pretty hacky right now), I encountered some challenges that might be shared across other similar projects that can bring maintainability issues in the long run.
The main problem is the need to hack the typst-cli package, which is not designed to expose compiler functions explicitly.
Just as an example, you need to hack Cargo.toml and rewrite the workspace references in it, create your own lib.rs file, import the compile function and other related ones that you desire while fixing missing imports that you don't completely understand.
The result is a hard to understand codebase for a simple binding that depends on lots of functions that are constantly changing on each iteration of typst.
Proposal
To improve maintainability and facilitate integration with various programming languages, I suggest creating a new crate, maybe named typst-compiler, specifically designed to expose only the compiler functions. This crate would provide a consistent interface for projects such as typst-py, typst-rb, eliminating the need to hack typst-cli. Typst-cli could also use the typst-compiler instead of having .
The new crate would exclude unrelated functionalities like updating packages, terminal handling, file watching, and argument parsing, focusing solely on document compilation.
Taking a look at the needs of the three projects, this interface could provide functionalities like:
- file input name
- workdir and root
- output format e.g pdf, png, svg etc.
- ppi
- compilation directly from string
- typst SystemWorld library exposure (to pass in host language values directly to typst)
Does this proposal make sense, or am I missing something?
Use Case
Facilitate the creation of Typst bindings for multiple programming languages.
Obs:
I'm taking the liberty to link typst-py and typst-rb authors that might be interested: @messense @actsasflinn
Activity
frozolotl commentedon Aug 1, 2024
There is actually such a project currently being developed by @tingerrr: #4540
PgBiel commentedon Aug 2, 2024
Closing as a duplicate of #4452
Note that the compiler itself is already available as a crate (
typst); whattypst-clidoes differently is provide the external stuff (downloading packages, providing system fonts, accessing the filesystem, and so on). In that issue, it is requested to have a separate crate to provide those features separately fromtypst-cliso that downstream consumers don't necessarily have to re-implement them (although, depending on your environment's restrictions, that could be necessary either way).tingerrr commentedon Aug 2, 2024
I'd like to add that the
typst-kitcrate at this stage contains most of the necessary things to replicate theclibehavior when it comes to the environment (as noted above), but no building blocks forWorldimplementations yet. This will be added at a later date, as we're still unsure about the API. If you want to test this, checkout the branch and see if the API seems right to you.rousbound commentedon Aug 6, 2024
Hi @tingerrr!
I just saw that typst-kit was merged into the main branch, nice work!
I will try to hack it and add the World/SystemWorld into typst-kit to provide some implementation proposal for a typst-kit::compile module.
tingerrr commentedon Aug 6, 2024
Sure, feel free to cc me on it or request a review directly when it's done.
rousbound commentedon Aug 9, 2024
Hey @tingerrr!
I took a first look at the usage of typst-kit by the typst-cli.
If I understood correctly, the idea is that other tools like IDEs, etc could use the typst-kit to expose the right functions.
Having that in mind I was wondering if you have other examples of the typst-kit usage that I could see, maybe outside the Typst repo.
Thanks in advance.
tingerrr commentedon Aug 10, 2024
It's less so the exposure of "the right functions" and more so providing the correct implementation of common functionality.
I have an example here:
https://github.com/tingerrr/typst-test/blob/e67c93d2f525a574fbe77cf6a5f7a26c3fb79d2c/crates/typst-test-cli/src/kit.rs
The things which should be in kit but aren't yet is the
SystemWorldimplementation there. That needs to be module enough to work for other projects too.This branch is frequently force pushed, so I don't expect this link to hold long, but I'll try to remember to upload a copy of the repo state and update the link here if I do.