Skip to content

Typst Compiler as a Library #4653

Closed as not planned
Closed as not planned

Description

@rousbound

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

  1. added
    feature request
    on Aug 1, 2024
  2. frozolotl commented on Aug 1, 2024

    @frozolotl
    Contributor

    There is actually such a project currently being developed by @tingerrr: #4540

  3. PgBiel commented on Aug 2, 2024

    @PgBiel
    Contributor

    Closing as a duplicate of #4452

    Note that the compiler itself is already available as a crate (typst); what typst-cli does 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 from typst-cli so that downstream consumers don't necessarily have to re-implement them (although, depending on your environment's restrictions, that could be necessary either way).

  4. added
    duplicate
    on Aug 2, 2024
  5. tingerrr commented on Aug 2, 2024

    @tingerrr
    Contributor

    I'd like to add that the typst-kit crate at this stage contains most of the necessary things to replicate the cli behavior when it comes to the environment (as noted above), but no building blocks for World implementations 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.

  6. rousbound commented on Aug 6, 2024

    @rousbound
    Author

    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.

  7. tingerrr commented on Aug 6, 2024

    @tingerrr
    Contributor

    Sure, feel free to cc me on it or request a review directly when it's done.

  8. rousbound commented on Aug 9, 2024

    @rousbound
    Author

    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.

  9. tingerrr commented on Aug 10, 2024

    @tingerrr
    Contributor

    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 SystemWorld implementation 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.

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

    duplicatefeature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions