Bundle postprocessing stage

(Originally posted on Discord, link to original message: Discord)

I am working on several packages and a template related with typst html export. In my case, I wanted to add search support for my bundle, which unfortunately can’t be completed in standard Typst due to Typst not having a postprocess stage. I think I might be able to add bundle postprocessing by compiling the document twice, and the first compilation would contain a hook that queries every single exported asset and document, and export it to a filelist.txt. The second stage would contain that filelist.txt info and process every file listed in filelist.txt It would be easier if Typst has a postprocess stage or if it supports directory walking though.

Relevant: Post-processing bundle output · Issue #8704 · typst/typst · GitHub

I did some simple experiments

#!/usr/bin/env bash
#let _ = \``` // i added backslash before the backticks to make syntax highlighting work
TYPST_FLAGS="--features bundle,html --format bundle $0 --pretty"
TYPST_FLAGS_2="--input=filename=$0 $TYPST_FLAGS"
case "$1" in
  compile) typst compile $TYPST_FLAGS && \
           typst compile $TYPST_FLAGS_2 && \
           exit 0 ;;
  watch)   typst watch   $TYPST_FLAGS && \
           exit 0 ;;
  *)       echo "Unknown option: $1. Enter 'compile' or 'watch'"; exit 1 ;;
esac
exit 1
\``` // i added backslash before the backticks to make syntax highlighting work

#if "filename" in sys.inputs {
  let filename = sys.inputs.filename
  let dir = filename.replace(regex("\.typ$"), "")
  let files = read(dir + "/filelist.txt").split("\n")
  document("post-processed.html")[#files]
} else {
  context asset("filelist.txt", {
    let docs = query(document).map(it => it.path)
    let assets = query(asset).map(it => it.path)
    (docs + assets).sorted().join("\n")
  })

  import html as h

  document("index.html")[
    I am the tailwind master
    #h.div(class: "foo bar baz qux quux")[Hello]
  ]
}