Queries

Tola compiles the entire site into a single Typst Bundle, so every #document(...) shares one introspection space. Calling bare query(heading) or global counter(figure) from single-PDF habits pulls in elements from every other page unless scoped explicitly.

Document scope

Inside context, call current-document() from @tola/document:0.0.0 to get the enclosing #document(...)‘s output, route, and location, then narrow queries with selector.within(...):

typst
#import "@tola/document:0.0.0": current-document

#let current-figures() = context {
  let doc = current-document().location
  let figs = query(selector(figure).within(doc))
  [This page contains #figs.len() figures.]
}

Similarly, scope to a labeled container (such as <article-body>) to ignore sidebars and footers:

typst
#context {
  let notes = query(selector(<callout-note>).within(<article-body>))
  ...
}

For the two most common introspection tasks, prefer @tola/document’s helpers over raw query:

  • Page outline: headings(depth: ...) returns level, nesting, text, label, location, and outlined for the current document.
  • Cross-page links: references(from:, from-within:, to:, to-within:) classifies internal, external, and unresolved links across the Bundle.

state and counter

Rendering multiple #document(...) calls in one Bundle shares counter (footnote, figure, heading) and custom state(...) values across document boundaries. Without resetting, document 2′s footnote numbers continue where document 1 left off.

Reset them at the start of each document layout:

typst
#let reset-document-counters() = {
  counter(heading).update(0)
  counter(footnote).update(0)
  counter(figure.where(kind: image)).update(0)
  counter(figure.where(kind: table)).update(0)
  counter(math.equation).update(0)
}

#document(page.output, format: "html", title: page.meta.title)[
  #reset-document-counters()
  ...
]

If templates pass per-block options via a custom state("code-lines", none) before code blocks, reset that state at the start of each document too so a dangling marker at the end of one page never affects the next page.

Bundle-level asset aggregation

Bundle-wide visibility in query(...) lets individual pages drop lightweight #metadata(...) markers while the top-level entry compiles, deduplicates, and emits shared assets once.

For example, when diagrams appear on multiple pages, emit an <img> and record #metadata(...) <site-diagram> inside the component, then collect and publish once at the end of site.typ:

typst
// components/diagram.typ
#import "@tola/address:0.0.0": output-url
#import "@tola/hash:0.0.0": blake3, bytes-to-hex

#let diagram-id(source) = bytes-to-hex(blake3(bytes(source))).slice(0, 8)

#let diagram-embed(source) = {
  let id = diagram-id(source)
  let out = "diagrams/" + id + ".svg"
  html.div(class: "diagram")[
    #metadata((id: id, source: source, output: out)) <site-diagram>
    #html.img(src: output-url(out), alt: "Diagram")
  ]
}

// Call once at the top level of site.typ after all document(...) calls
#let publish-diagrams() = context {
  let seen = (:)
  for entry in query(<site-diagram>) {
    let item = entry.value
    if item.id in seen { continue }
    seen.insert(item.id, true)
    asset(item.output, render-svg(item.source))
  }
}

Identical content reused across pages is compiled and published once, HTML pages stay free of repeated inline SVGs, and site-wide glossaries or figure indexes can be assembled the same way.

tola query

Despite similar names, Typst query(...) and the tola query command run in different places:

AspectTypst query(...)tola query CLI
Where it runsInside context during Bundle compilationStandalone CLI command; runs no hooks and writes no public/
InputLaid-out elements (heading, figure, metadata)Standalone .typ file or module [queries] entry
OutputTypst array used to render pages or asset(...)JSON stream for scripts, CI, or tola lsp
Use casesTOCs, backlinks, deduplicated assets, figure countsExporting page manifests to Nu hooks, completion, go-to-definition

When an external hook (such as generate-outputs) needs structured site data, keep selection in a shared library and add a small query file emitting #metadata(...) <pages>:

typst
// queries/pages.typ
#import "../site/pages.typ": select-pages
#import "@tola/address:0.0.0": output-url

#metadata(select-pages().map(p => (
  id: p.id,
  title: p.meta.title,
  url: output-url(p.output),
))) <pages>
sh
tola query ./queries/pages.typ --label pages --json --pretty

Search this site

Use the arrow keys to move through results.

Type to search.