Skip to content

Crate for downstream tooling #4452

Closed

Description

@tingerrr

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
  • 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

  1. added
    feature request
    on Jun 25, 2024
  2. zinifier commented on Jun 26, 2024

    @zinifier

    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:

    • parsing a "theme" file and ensure it has specific functions that allow certain parameters
    • list functions/parameters declarations from rust to produce theme documentation
    • get typed result/content from calling a typst function from rust code directly
    • generate PDF from content from rust code

    What i lacked or found hard:

    • typed function parameters: is it impossible or i could not find it in docs? typed args/signatures would certainly be a bonus to the typst language if they're not here yet
    • parse/downcast raw text into a typed Typst value from rust code, for example 12pt into a size, to then call another typst function with it
    • relative content paths (like images/imports) are relative to different things and that needs to be properly documented for use as a library... if i remember correctly import is relative from content typst file, then images are relative from the theme typst file, and all this needs to be in the same project root

    Typst is incredible, lots of love :)

  3. tingerrr commented on Jun 26, 2024

    @tingerrr
    ContributorAuthor
    • parsing a "theme" file and ensure it has specific functions that allow certain parameters

    This is unlikely to be exposed from such a crate, you can simply apply a theme from within your World implementation by giving it a Library which uses different default Styles. 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).

    • list functions/parameters declarations from rust to produce theme documentation

    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?

    • get typed result/content from calling a typst function from rust code directly

    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 Engine and Introspector and such, although I'm unsure how much of this can be done outside the typst crate itself.

    • generate PDF from content from rust code

    This is exposed in the typst-pdf crate.

    • typed function parameters: is it impossible or i could not find it in docs? typed args/signatures would certainly be a bonus to the typst language if they're not here yet

    That's a planned feature! Specifically type annotations which validate function arguments, as well as custom types.

    • parse/downcast raw text into a typed Typst value from rust code, for example 12pt into a size, to then call another typst function with it

    I think you can either parse these using the typst-syntax crate and evaluate them, but I think you could likely also use some of the conversion traits found in the typst crate itself.

    • relative content paths (like images/imports) are relative to different things and that needs to be properly documented for use as a library... if i remember correctly import is relative from content typst file, then images are relative from the theme typst file, and all this needs to be in the same project root

    This has been extensively discussed on the discord, I think we even arrived at some consensus of what we need for this to work.

  4. tingerrr commented on Jun 26, 2024

    @tingerrr
    ContributorAuthor

    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-test and I could retire typst-project in favor of this.

  5. added
    crate
    on Jul 1, 2024
  6. tingerrr commented on Jul 2, 2024

    @tingerrr
    ContributorAuthor

    @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-kit then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cratefeature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions