Search

Static search usually takes one of two forms: small sites can emit a JSON index directly from Typst and query it in the browser; larger sites that need full-text tokenization and chunked index loading can run an external indexer in generate-outputs.

Lightweight JSON index

When searching titles, descriptions, and tags is enough, emit search-index.json at the top level of site.typ using asset(...) with no extra tooling:

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

#let search-entries = pages.map(p => (
  title: plain-text(p.meta.title),
  description: p.meta.description,
  url: output-url(p.output),
  kind: p.kind,
))

#asset("search-index.json", json.encode(search-entries))

Expose output-url("search-index.json") in a DOM attribute and fetch it lazily when the user opens search so initial page loads stay fast.

External indexers

When you need full-text body indexing, CJK segmentation, or sharded index chunks, run an indexer (such as Pagefind, TinySearch, or MiniSearch) against the compiled HTML snapshot in [[hooks.generate-outputs]]. The example below uses Pagefind.

Index boundaries

By default an indexer scans all of <body>, pulling headers, sidebars, TOCs, and copy buttons into snippets. Mark the article container and ignore chrome explicitly:

typst
// Attach page kind for client filtering
#html.elem("meta", attrs: (
  name: "page-kind",
  content: page.kind,
  "data-pagefind-filter": "kind[content]",
  "data-pagefind-meta": "kind[content]",
))

// Restrict indexing to main article content
#html.elem("main", attrs: (id: "article", "data-pagefind-body": ""))[
  #include page.file
]

// Exclude sidebars and TOCs
#html.elem("aside", attrs: (class: "sidebar", "data-pagefind-ignore": ""))[
  ...
]

Leave data-pagefind-body off 404.html and redirect pages, or mark <html> with data-pagefind-ignore="all".

Build hook

In generate-outputs, (get-hook).input-dir holds the compiled HTML snapshot and (get-hook).output-dir collects files added to the publish directory. Because any content edit changes the snapshot, set dev = "skip" so live reloads stay fast while tola build, tola check, and tola preview build the index normally:

toml
[[hooks.generate-outputs]]
name = "search"
nu = "tools/search.nu"
dev = "skip"
outputs = [{ tree = "pagefind" }]
nu
use tola get-hook

def main [] {
  let hook = get-hook
  let out = ($hook.output-dir | path join "pagefind")
  mkdir $out
  ^npx --no-install pagefind --site $hook.input-dir --output-path $out
}

In a theme module, bind dev to a module option (dev = { option = "search.dev" }) and write into _tola-modules/{name}/pagefind.

Client loading

Pass the resolved script URL through output-url(...) into an attribute instead of hardcoding /pagefind/pagefind.js in JS, so non-root base-path deployments resolve properly:

typst
#import "@tola/address:0.0.0": output-url

#html.elem("div", attrs: (
  id: "search-root",
  "data-search-url": output-url("pagefind/pagefind.js"),
))[]
js
const root = document.getElementById("search-root");
let index = null;

async function runQuery(term) {
  try {
    if (!index) {
      index = await import(root.dataset.searchUrl);
      await index.init();
    }
    const res = await index.search(term);
    return await Promise.all(res.results.slice(0, 10).map((r) => r.data()));
  } catch {
    // Graceful fallback when skipped during tola dev
    return [];
  }
}

Search this site

Use the arrow keys to move through results.

Type to search.