Can I use the syntax of Typst only, and define custom primitive commands?

I like the syntax of Typst, and want to use it for something else.

Just like TeX having several extensions like Plain TeX, LaTeX, etc., can I define a different set of “primitive commands” (i don’t know whether Typst has something like that), and apply it to other scenarios than building a pdf document (e.g., building a html document instead)?

I’m not sure I can answer your general question, can you give more details what you mean by “primitive commands”?

For creating HTML documents specifically, that is on the roadmap and is tracked by this GitHub issue: Tracking issue: HTML export · Issue #5512 · typst/typst · GitHub

I don’t remember where I read this but I think it’s also planned to allow the creation of types by the user, and this will let us use set rules on them. Maybe this is closer to what you’re looking for?
This is mentioned as “Custom types (that work with set and show rules” on the roadmap.

3 Likes

I’m not sure how Typst works. In my mind, every code are parsed into functions and values, and after executing them recursively, some “primitive” functions are finally called, which means these functions cannot be implemented in the language of Typst. Then Typst executes these functions using a more basic implementation, like writing some data into the pdf document.

If this is the case, what I want to do is to define a different set of primitive functions, using my own implementation, and then build a whole system on it.

I still don’t understand what the final product is that you are trying to achieve. Do you want to export to a different format?

You can read here how Typst roughly works. In particular, it parses and evaluates your source code to something called Content, which maybe is the “intermediate representation” that you are looking for. This content is then either exported to HTML or layouted in to Frames (1 page = 1 frame) which can be exported to PNG/SVG/PDF.

1 Like

Thank you for your answers. They do help me a lot.

My main goal is to use Typst to write notes on my own website.
However, Typst is designed to build a pdf document, instead of HTML.
So I want to redefine the commands to product HTML instead.

For example, I want to write something like this:

= This is a header // <h1>This is a header</h1>
This is a paragraph // <p>This is a paragraph</p>
$x^2 + y^2 = z^2$ // Mathjax or something else

I think the basic structure of pdf document and HTML is different. PDF documents are controlled by positions and sizes (actually IDK) while HTML are rendered by document flows. This difference possibly makes producing HTML directly using Typst impossible.

Therefore, I want to rewrite the basic commands in Typst to produce HTML instead of PDF, while preserving the syntax.

From your answer, I think what I should do is to do only the first two steps, and parse the Content to HTML directly? I will look for more references later.

Yeah but as mentioned before, what you are trying to achieve is actually currently implemented by the maintainers of Typst.

Someone already posted the tracking issue: Tracking issue: HTML export · Issue #5512 · typst/typst · GitHub

With the git version of Typst you can already export very basic documents to HTML. It will become better over the next weeks and months. Applications like blogging are explicitly in the mind of the creators, just have a look at the tracking issue above, where “blog” is literally mentioned.

And someone just posted here on the Forum that he is already using Typst to generate his website: My website is now built with Typst's new experimental HTML exporter

1 Like