使用模块
模块是包含 tola-module.toml 清单的目录,用来封装 Typst 组件、Nu 函数、静态资源或 CLI 子命令;声明 [build] entry 后即成为接管整站路由与渲染的主题。
模块来源
在 tola.toml 中通过 [modules.<别名>] 注册模块。站点自行指定别名,同一份模块可以挂载到多个别名下并分别配置。用 path 引用本地目录,git 引用 Git 仓库,或 tarball 引用发行归档。
[build]
entry = { module = "notes" }
[modules.notes]
path = "theme"
[modules.notes.options]
show-title = true[modules.theme]
git = { url = "https://github.com/example/tola-theme.git", tag = "v1.0.0", subdir = "dist/theme" }[modules.theme]
tarball = { url = "https://example.com/theme-v1.0.0.tar.gz", sha256 = "0123..." }path:相对tola.toml解析(--pure模式下必须位于站点内)。git:tag、branch、完整rev至多指定一项,省略则跟踪默认分支;可选subdir指向子目录。HTTPS 走 Git 凭据助手,SSH 走本机 SSH 配置。tarball:支持url或本地path,格式为.tar.gz、.tar.zst或.zip,可选 64 位十六进制sha256。归档内须包含单个外层目录且清单位于其中;下载归档不会执行作者构建脚本,应使用打包后的发行归档。
注册后,正文与模板通过 #import "@tola-modules/<别名>:0.0.0": * 导入模块的 Typst 库,在终端用 tola <别名> --help 查看模块提供的子命令与指南。远程模块的解析摘要写入 tola.lock,刷新版本运行 tola update <别名>。
官方模块
官方模块独立于 Tola 发布。下面的示例使用仓库的默认分支,tola.lock 会记录实际提交。用 tola update 刷新锁文件。
tailwind
扫描 Tola 渲染出的 HTML 页面并编译 Tailwind CSS v4:
[modules.tailwind]
git = { url = "https://github.com/tola-rs/modules.git", subdir = "tailwind" }
[modules.tailwind.options]
runner = "auto" # 或显式指定,例如 ["bunx", "--no-install", "@tailwindcss/cli"]
minify = false
dev = "run" # "run" | "reuse" | "skip"先安装 Tailwind CSS v4 CLI,例如运行 npm install --save-dev tailwindcss @tailwindcss/cli。自动查找的顺序是声明模块的目录、站点根目录、PATH。
在 <head> 中用 output-url("_tola-modules/tailwind/tailwind.css") 引入样式表,路径中的别名应与配置一致。需要自定义 CSS 或 @source 指令时,添加 styles/tailwind.css 并以 @import "tailwindcss" source(none); 开头。导入的 CSS 和插件从该样式表所在目录解析。可以用 toolchain 把额外依赖的版本或摘要加入磁盘缓存键。
resize-images
批量缩放声明在 [assets] 中的 JPEG、PNG 和 WebP 图片,并按尺寸分别缓存(适合图库或相册批处理;单张页面插图则更适合按需调用 @tola/image):
[assets]
trees = [{ source = "photos", url-prefix = "/gallery" }]
[modules.resize-images]
git = { url = "https://github.com/tola-rs/modules.git", subdir = "resize-images" }
[modules.resize-images.options]
include = ["photos/**"]
exclude = ["photos/drafts/**"]
widths = [480, 960] # 另有 format = "webp"|"png"|"jpg"、quality、upscale、jobs、dev
[modules.resize-images.options.overrides."photos/cover.jpg"]
widths = [1200]使用 output-url("images/<别名>/<源路径>/w<宽度>.<格式>") 引用产物,例如 output-url("images/resize-images/photos/cover.jpg/w1200.webp")。处理其他模块的静态图片时,指定 source = { module = "notes" }。
选项合并
模块清单中的 [options] 提供默认值,站点 tola.toml 中的 [modules.<别名>.options] 覆盖默认值。字典表按键递归合并,标量与数组整体替换。站点级通用数据放在顶层 [options],在 Typst 中通过 site.options 读取,在 Nu 中通过 (get-site).options 读取。
定制主题
使用外部主题时,尽量避免直接改动主题目录源码,按从轻到重的顺序定制:
- 调整
[modules.<别名>.options]开关与文案;若主题提供extra-styles选项,将补充 CSS 声明进[assets]后传入。 - 用
[modules.<别名>.assets.replace]将站点本地文件替换到主题原有的资产 URL(或设enable = false关闭全部模块资产);模块图标集同样可在[modules.<别名>.icons.collections.<名称>]中替换:
[modules.notes.assets.replace]
"theme.css" = "styles/custom-theme.css"- 需要改动页面结构时,将
[build].entry切回站点自身的"site.typ",导入主题公开的渲染函数并传入自定义布局回调:
#import "@tola-modules/notes:0.0.0": article, render-site
#let article-with-banner(source) = [
#html.aside(class: "notice")[内容持续更新中。]
#article(source)
]
#render-site(article: article-with-banner)