外部数据
除了扫描 content/ 下的 .typ 源文件,站点入口可以直接读取本地结构化数据或 before-build 钩子准备好的中间文件,批量生成页面以及 JSON、TXT 等静态文件。
数据生成页面
Typst 内置 json、toml、yaml、csv 与 xml 读取函数。读取后先用 @tola/schema:0.0.0 做结构校验,再循环调用 document(...) 生成页面:
[
{ "slug": "tola", "name": "Tola", "summary": "Typst 静态站点生成器", "repo": "https://github.com/tola-rs/tola-ssg" },
{ "slug": "merman", "name": "Merman", "summary": "Mermaid 图表渲染包", "repo": "https://github.com/tola-rs/merman" }
]#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))
// 列表页 /projects/
#document(output-of("/projects/"), format: "html", title: "项目列表")[
#html.h1[项目列表]
#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
]
}
]
]
// 详情页 /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)[源码仓库]]
]
}模块内部读取站点目录中的文件(例如站点根下的 data/projects.json 或 generated/stats.json)时,用 @tola/site:0.0.0 的 project-path("generated/stats.json"),不要写 /(在模块内 / 指向模块目录)。
钩子生成数据
Typst 编译期运行在纯确定性沙箱里,不能访问网络、环境变量或执行外部命令。需要把外部 CLI 输出、Git 提交时间、远端接口快照或数据库导出纳入构建时,在 [[hooks.before-build]] 中把数据写成 JSON 放进 generated/,再由 Typst 用 json("/generated/...") 读取:
[[hooks.before-build]]
name = "git-dates"
nu = "tools/git-dates.nu"
dev = "reuse"
rerun-on = { include = ["content/**/*.typ"] }
generates = ["generated/git-dates.json"]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
}如果需要在模块级复用,可以把生成路径放到 generated/{name}/...(本仓库的 tools/export-reference.nu 正是通过 before-build 调用 tola help --all --format json 写入 generated/reference.json,再由参考页模板一次性加载)。
输出静态文件
除了 document(..., format: "html") 与 format: "pdf",可以在 Bundle 顶层调用 asset(output, content) 直接写出任意文本或二进制文件(如 api/posts.json、llms.txt、robots.txt、CNAME、graph.json):
#import "@tola/address:0.0.0": output-url
#import "@tola/document:0.0.0": plain-text
#import "@tola/site:0.0.0": site
// 1. 输出轻量 JSON 接口供前端或外部消费
#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. 动态生成 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
如果外部工作流(CI、发布脚本、generate-outputs 钩子)需要读取站点的页面列表,但不想把整份数据公开到 public/,可以单独建一个查询文件,通过 tola query 导出 JSON:
// 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>tola query ./tools/manifest.typ --label manifest --json --prettytola query 不执行构建钩子,也不写入 public/;在 Nu 脚本里可通过 use tola query 直接调用命名模块查询。