文档查询

在 Tola 中,整座站点编译进同一个 Typst Bundle,所有 #document(...) 共享同一套内省空间。写单篇 PDF 时习惯直接调用 query(heading) 或全局 counter(figure),搬到多页 Bundle 里若不限定作用域,就会把其他页面的元素一并查出来。

文档作用域

在 context 内调用 @tola/document:0.0.0 的 current-document(),可以拿到当前所在 #document(...) 的 output、route 与 location。结合 Typst 的 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))
  [本页共包含 #figs.len() 个图表。]
}

同样地,可以把范围收窄到文档里的某个带 <label> 容器(例如只查询正文区域 <article-body> 内的自定义标注,忽略侧边栏和页脚):

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

对于最常见的两类内省需求,直接使用 @tola/document 的封装比手写 query 更稳妥:

  • 查当前页标题大纲:用 headings(depth: ...),直接附带 level、nesting、text、label、location 和 outlined。
  • 查全站页面引用关系:用 references(from:, from-within:, to:, to-within:),自动区分站内、外链与未解析链接。

state 与 counter

在同一个 Bundle 中连续渲染多篇 #document(...) 时,Typst 的 counter(例如 counter(footnote)、counter(figure)、counter(heading))与自定义 state(...) 不会在文档边界自动清零。如果不重置,第 2 篇文章的脚注编号会接在第 1 篇后面。

在基础布局函数渲染每篇文档前统一重置:

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()
  ...
]

如果站点用自定义 state("code-lines", none) 在正文里向下一个代码块传参(例如指定高亮行号),同样要在每篇文档开头调用一次 state("code-lines", none).update(none),防止某篇文章末尾多写了一次标记而波及下一篇页面。

顶层汇总资产

Bundle 级 query(...) 的跨文档可见性并不是负担——它允许每个页面在渲染时仅留下轻量 #metadata(...) 标记,再在入口文件顶层一次性去重、编译并输出共享资产。

例如,多个页面中都嵌有 Mermaid 图表或按需生成的 SVG:在组件渲染时记录 #metadata(...) <site-diagram>,然后在 site.typ 末尾的 context 块里集中查询并调用 asset(...) 发布:

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: "示意图")
  ]
}

// 在 site.typ 顶层所有 document(...) 之后调用一次
#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))
  }
}

相同内容在多个页面出现时只编译和发布一份文件,避免 HTML 重复内联大段 SVG,顺带在顶层汇总术语表或图表索引。

tola query

名字相近但运行位置和用途不同:

维度Typst 内置 query(...)tola query 命令行
运行阶段Bundle 编译期内部,在 context 块中执行独立 CLI 命令,不生成 public/,不跑钩子
输入已排版元素(heading、figure、metadata 等)独立 .typ 文件或模块 [queries] 入口
输出Typst 值数组,直接参与页面或 asset(...) 生成JSON 数据流(供脚本、CI 或 tola lsp 消费)
典型场景目录、反链、去重资产汇总、图表计数导出全站文章清单给 Nu 钩子、编辑器补全与跳转

如果在外部脚本(如 generate-outputs 钩子)里需要站点页面清单,把筛选逻辑抽到 lib.typ 中,再写一个仅输出 #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

站内搜索

可以用上下方向键选择结果。

输入关键词开始搜索。