I’m trying to make a minimal viable static site generator with typst. So far the problem I’ve ran into is that assets cannot be part of sub-documents, like so:
My workaround for this is adding a show rule that no longer renders the asset, which stops the compiler error. However, I would still like to include the file somehow. My idea there is something along the lines of:
Assuming the concept works, you would introduce state like this into your code:
#let exports = state("exports", ())
#document("index.html")[
#show asset: it => exports.update(exports => {
exports.push(it.path, it.data)
exports
})
...
]
...
#context for (path, data) in exports.get() {
asset(path, data)
}
exports was replaced by a state, initialized to an empty array
where exports was modified, we have instead a call to update. I named the parameter exports – it is the array inside the state – but the name can be anything you choose. After modifying it, it is returned and that becomes the new state value.
where exports was read, a context is added and exports is replaced with exports.get().
exports itself is not the value, you first need to retrieve it from the state; thus get().
Functions in Typst are “pure”, in particular: export.get() can’t just return different values at different times. That requirement is weakened by wrapping code in context; it allows the contained code to depend on where in the document it appears.