This is very doable, but the method is very hacky.
Currently Typst exposes no public APIs for manipulating generated HTML or PDF files. The only available APIs are for generating the documents. We can, however, bypass that by compiling the document two times.
The first run would be generating the raw html, and the second run would be reading the html and css files, then indexing and minifying them. The minification and indexing utilities would run in Typst's wasm environment.
There are some drawbacks:
- This requires a filesystem, which means that web app users are unlikely to be able to use them
- Web app users can upload the compiled result, but that can be tedious.
- This requires running the
typst command twice, and the second time would have some extra flags. This behaviour might confuse some users.
- WASM is slower than native, but faster than JS for our scenario.
- Slower than calling native binaries from Node.js
As well as some benefits:
- Everything is running in wasmi, so there isn't a lot of stuff to maintain.
- Cross-platform
- As long as Typst doesn't do something weird the user is safe.
This is very doable, but the method is very hacky.
Currently Typst exposes no public APIs for manipulating generated HTML or PDF files. The only available APIs are for generating the documents. We can, however, bypass that by compiling the document two times.
The first run would be generating the raw html, and the second run would be reading the html and css files, then indexing and minifying them. The minification and indexing utilities would run in Typst's wasm environment.
There are some drawbacks:
typstcommand twice, and the second time would have some extra flags. This behaviour might confuse some users.As well as some benefits: