Repository navigation
Description
Description
Lift parts of typst-cli and the typst/package bundler into a dedicated crate for implementing tooling related to Typst.
A non-exhaustive list of features I believe should be added to such a crate:
- manifest parsing
- parsing manifest files into a well typed struct
- manifest validation
- ensuring the validity of a manifest for the universe
- manifest writing/editing
- editing a manifest in place like cargo does (see
toml-edit) - I think this is not required now, but essential later when git packages and custom registries arrive
- editing a manifest in place like cargo does (see
- tool section reading and writing
- in a similar vein as above
- package downloading
- making the implementation of a custom registry easier
- exposing the default locations and fallbacks
- font loading
- loading fonts with the correct priorities
- package download locking
- being able to lock downloading of a package consistently to avoid interference with other tools running in parallel, provided they also use the same locking mechanism
- project locking
- as above but for doing automation inside the project
If these features are sufficiently abstracted, most of them could be optional feature flags, reducing possible code bloat.
My reasoning for keeping manifest parsing and validation separate is that some things are fixed, like what the Typst Universe allows as valid manifests, but there are valid use cases where these need not be valid for the Universe: company internal registries may have different requirements for licenses, keywords, etc. Furthermore, tools should be able to ignore some errors where they don't matter while still being able to read other parts of the manifest, like their own tool section, or to fix the manifest like a linter would.
Prior Art
Some prior art would be typst-project, a crate I made for typst-test and by some extent utpm. It provides manifest parsing, reading the tool section for custom configuration, and a quite frankly overengineered API for locating the project root.
Another example of where this was done is jj, which actually exposes the whole CLI as a library. They provide a jj_lib library crate for the core building blocks, a jj_cli lib crate for users to implement custom backends and commands for company internal usage while maintiang the rest of the default CLI and the jj_cli binary crate as the default jj implementation. While this is certainly overengineered from the perspective of Typst, it can serve as a good example of how to find the right API abstractions.
Use Case
Various downstream tooling projects for Typst written in Rust exist, many of which needed to reimplement some part of the functionality contained in typst-cli.
Some projects which had to reimplement features include:
| tool | reimplemented feature |
|---|---|
| tinymist | manifest parsing, font loading, package downloading |
| typst-lsp | manifest parsing, font loading, package downloading |
| typst-test | manifest parsing, font loading, package downloading |
| utpm | manifest parsing, package downloading |
| the typst/package bundler | manifest parsing |
A single crate providing a correct default implementation of various Typst related functionality could reduce code duplication and improve correctness across these projects. Fixes and improvements to such a crate would allow each tool to benefit from those without having to manually keep up with those.
Activity
zinifier commentedon Jun 26, 2024
My usecase: use typst to produce booklets, currently by converting Markdown with frontmatter settings to a .typ file that imports a typ theme function. Ideally, i would not even need the intermediate typ file and would call the theme functions directly from Rust.
What i would like to have exposed:
What i lacked or found hard:
12ptinto a size, to then call another typst function with itTypst is incredible, lots of love :)
tingerrr commentedon Jun 26, 2024
This is unlikely to be exposed from such a crate, you can simply apply a theme from within your
Worldimplementation by giving it aLibrarywhich uses different defaultStyles. Maybe take a look at this, which also applies some default styles to documents it receives from discord commands (although there it is simple prepended as source code).I'm unsure what you mean, get the signatures for things which are written in rust within rust code, or getting Typst signatures from within rust code?
I'm also unsure about this, which functions do you mean, if you intend to evaluate Typst script code and get its result you should be able to if you construct the necessary
EngineandIntrospectorand such, although I'm unsure how much of this can be done outside thetypstcrate itself.This is exposed in the
typst-pdfcrate.That's a planned feature! Specifically type annotations which validate function arguments, as well as custom types.
I think you can either parse these using the
typst-syntaxcrate and evaluate them, but I think you could likely also use some of the conversion traits found in thetypstcrate itself.This has been extensively discussed on the discord, I think we even arrived at some consensus of what we need for this to work.
tingerrr commentedon Jun 26, 2024
If we arrive at a consensus of what a first version of this crate should contain and how the API should look like, I'd be thrilled to start working on it. This is a direct blocker for
typst-testand I could retiretypst-projectin favor of this.tingerrr commentedon Jul 2, 2024
@laurmaedje has noted on the discord that manifest parsing is already partially in
typst-syntax, so the rest could be added there too. Which is what I would start with some time this week.The rest would go into
typst-kitthen.