External data

Beyond scanning .typ files under content/, the site entry can read structured data files or intermediate JSON generated by before-build hooks to emit pages, JSON endpoints, and plain-text assets.

Pages from data

Typst provides built-in json, toml, yaml, csv, and xml loaders. Validate loaded data with @tola/schema:0.0.0 before passing it to document(...):

json
[
  { "slug": "tola", "name": "Tola", "summary": "Typst static site generator", "repo": "https://github.com/tola-rs/tola-ssg" },
  { "slug": "merman", "name": "Merman", "summary": "Mermaid diagram renderer", "repo": "https://github.com/tola-rs/merman" }
]
typst
#import "@tola/address:0.0.0": output-of, output-url
#import "@tola/schema:0.0.0" as s

#let project-schema = s.schema((
  slug: s.non-empty(s.trim(str)),
  name: s.non-empty(s.trim(str)),
  summary: s.non-empty(s.trim(str)),
  repo: s.https-url,
))

#let projects = s.parse(json("/data/projects.json"), s.array-of(project-schema))

// Index /projects/
#document(output-of("/projects/"), format: "html", title: "Projects")[
  #html.h1[Projects]
  #html.ul[
    #for item in projects {
      let out = output-of("/projects/" + item.slug + "/")
      html.li[
        #html.a(href: output-url(out), item.name): #item.summary
      ]
    }
  ]
]

// Detail pages /projects/<slug>/
#for item in projects {
  document(output-of("/projects/" + item.slug + "/"), format: "html", title: item.name)[
    #html.h1(item.name)
    #html.p(item.summary)
    #html.p[#html.a(href: item.repo)[Source repository]]
  ]
}

Inside a module, / resolves to the module root rather than the consuming site; read site-level data files (such as data/projects.json or generated/stats.json) with project-path("generated/stats.json") from @tola/site:0.0.0.

Hook-generated data

Typst evaluates inside a deterministic sandbox with no network, environment variable, or subprocess access. When you need CLI outputs, Git commit timestamps, API snapshots, or database exports, write JSON into generated/ from [[hooks.before-build]] and read it via json("/generated/..."):

toml
[[hooks.before-build]]
name = "git-dates"
nu = "tools/git-dates.nu"
dev = "reuse"
rerun-on = { include = ["content/**/*.typ"] }
generates = ["generated/git-dates.json"]
nu
def main [] {
  let rows = (glob "content/**/*.typ" | each {|file|
    let rel = ($file | path relative-to $env.PWD)
    let ts = (^git log -1 --format=%cI -- $rel | str trim)
    { key: $rel, updated: $ts }
  })
  mkdir generated
  $rows | reduce -f {} {|it, acc| $acc | insert $it.key $it.updated }
    | to json
    | save --raw --force generated/git-dates.json
}

For reusable modules, namespace generated outputs under generated/{name}/... (this repository’s tools/export-reference.nu runs tola help --all --format json in before-build to produce generated/reference.json).

Static outputs

Alongside document(..., format: "html") and format: "pdf", call asset(output, content) at the Bundle top level to emit text or binary files (api/posts.json, llms.txt, robots.txt, CNAME, graph.json):

typst
#import "@tola/address:0.0.0": output-url
#import "@tola/document:0.0.0": plain-text
#import "@tola/site:0.0.0": site

// 1. Lightweight JSON endpoint
#let api-payload = posts.map(p => (
  title: plain-text(p.meta.title),
  url: output-url(p.output, origin: site.origin),
  published: p.meta.published.rfc3339,
  tags: p.meta.tags,
))
#asset("api/posts.json", json.encode(api-payload))

// 2. Dynamic robots.txt
#let robots = (
  "User-agent: *",
  "Allow: /",
  if site.origin != none { "Sitemap: " + output-url("sitemap.xml", origin: site.origin) },
).filter(line => line != none).join("\n") + "\n"

#asset("robots.txt", robots)

tola query

When external tools (CI, publish scripts, or generate-outputs hooks) need your site’s page metadata without publishing it into public/, add a standalone query file and inspect it with tola query:

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

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

tola query runs no build hooks and never writes to public/. Nu scripts can invoke named module queries directly via use tola query.

Search this site

Use the arrow keys to move through results.

Type to search.