编写模块

模块在 tola-module.toml 中按需声明能力,各个入口彼此独立:

清单表作用
[typst]entry 导出公共 Typst 库(@tola-modules/<别名>:0.0.0);fonts 声明编译字体目录。
[build]entry 提供可被 build.entry = { module = "<别名>" } 选用的整站入口。
[nu]entry 导出公共 Nu 库(use tola-modules <别名> 或 tola nu @tola-modules/<别名>)。
[options] / [assets] / [icons]声明默认选项、模块静态资源(支持 enabled-by)与模块图标集。
[queries]注册 tola query <别名>.<名称> 命名查询,并可接入编辑器补全、悬停、跳转与重命名。
[commands]注册 tola <别名> <命令> 子命令,支持 nu、argv、query、skill。
[init]注册站点初始化器,供 tola init <目录> --from <来源> --alias <别名> 调用。
[pack] / [[hooks.before-pack]]声明发版前资产编译与打包白名单,供 tola module pack 生成发行目录。

最小主题

只含清单和 lib.typ 就是一个组件库;补上 site.typ、[options] 和 [assets],即可作为完整主题使用:

toml
[typst]
entry = "lib.typ"

[build]
entry = "site.typ"

[options]
show-title = true
extra-styles = []

[assets]
files = [{ source = "assets/theme.css", url = "/theme.css" }]
typst
#import "@tola/address:0.0.0": asset-url, join-route, output-of
#import "@tola/module:0.0.0": get-module
#import "@tola/schema:0.0.0" as s
#import "@tola/site:0.0.0": site
#import "@tola/source:0.0.0": all-sources, parse-sources, tola-meta

#let note(..fields) = tola-meta(fields.named())
#let page-schema = s.schema((title: str, draft: s.optional(bool, default: false)))

#let pages() = {
  parse-sources(all-sources(), page-schema)
    .filter(source => not source.meta.draft)
    .map(source => (source: source, output: output-of(join-route(source.route-segments))))
}

#let article(source) = [
  #if get-module().options.show-title { title(source.meta.title) }
  #include source.file
]

#let render-site(article: article) = {
  let mod = get-module()
  set text(
    lang: site.language.lang,
    script: site.language.script,
    region: site.language.region,
  )
  for page in pages() {
    document(page.output, format: "html", title: page.source.meta.title)[
      #html.html(lang: site.language.tag)[
        #html.head[
          #html.meta(charset: "utf-8")
          #html.meta(name: "viewport", content: "width=device-width, initial-scale=1")
          #html.title(page.source.meta.title)
          #html.link(rel: "stylesheet", href: mod.assets.urls.at("theme.css"))
          #for url in mod.options.extra-styles {
            html.link(rel: "stylesheet", href: asset-url(url))
          }
        ]
        #html.body[
          #html.main(class: "page", article(page.source))
        ]
      ]
    ]
  }
}
typst
#import "lib.typ": render-site
#render-site()

运行 tola module check ./theme 校验清单与文件声明,再挂载到真实测试站点检查输出。

选项与资源

清单选项绑定

在 tola-module.toml 中,钩子的 dev、order、rerun-on、args、generates、outputs,资源的 minify、include、exclude,以及命令的 args、projection 都可以通过 { option = "..." } 直接绑定合并后的模块选项:

toml
[options.search]
enabled = true
dev = "skip"

[[hooks.generate-outputs]]
name = "search"
enabled-by = "search.enabled"
dev = { option = "search.dev" }
nu = "tools/search.nu"
outputs = [{ tree = "_tola-modules/{name}/search" }]

模块路径

模块 [assets] 相对模块根目录解析,发布在 /_tola-modules/<别名>/ 下,在 get-module().assets.urls 中的键不含前导斜杠(如 "theme.css")。单条资源可用 enabled-by 按布尔选项开关(即便默认为 false,发版时仍需包含该源文件)。

模块代码里 / 指向模块根目录;读取消费站点中的文件(如钩子生成的 JSON)需用 @tola/site 的 project-path("generated/...")。

编译字体与图标

模块自带编译字体通过 [typst] fonts = ["typst-fonts"] 声明,消费站点在 [typst.fonts].modules 中启用。模块图标集在清单 [icons.collections] 中声明,Typst 中用 icon.with(module: get-module().name) 绑定到当前实例。

命名查询

在 [queries.<名称>] 中声明查询入口与标签,直接复用 lib.typ 的 pages(),确保查询与构建看到同一批页面:

toml
[queries.index]
entry = "queries/index.typ"
label = "page-index"

[[queries.index.references]]
export = "page-link"
parameter = { positional = 0 }

[[queries.index.definitions]]
export = "page"
parameter = { named = "id" }
typst
// theme/queries/index.typ
#import "../lib.typ": pages
#import "@tola/address:0.0.0": output-url

#metadata(pages().map(p => (
  id: p.source.id,
  name: p.source.meta.title,
  source: p.source.file,
  url: output-url(p.output),
))) <page-index>

当查询结果为记录数组且字段限定为 id(唯一非空字符串)、name(字符串)以及可选的 description、source(原生 path)、url 时,声明 references 与 definitions 后,tola lsp 会为对应导出函数的参数提供补全、悬停文档、跳转定义与重命名。

子命令

[commands.<名称>] 接受 nu、argv、query、skill 四者之一,消费站点以 tola <别名> <名称> 调用:

toml
[commands.setup]
argv = ["npm", "--prefix", "{module}", "ci"]
help = "安装主题的 Node.js 依赖"

[commands.pages]
query = "index"
projection = ["id", "name", "url"]

[commands.guide]
skill = "README.md"
help = "阅读主题使用指南"

nu 和 argv 命令在站点根目录运行,args 会前置到用户输入的参数之前,支持 {module}、{project}、{tola} 占位符。skill 命令复用 tola skill 的 --list、--all、-o 分节浏览参数。

站点初始化器

声明 [init] nu = "tools/init.nu" 后,用户可通过 tola init my-site --from <来源> --alias notes [-- 额外参数](或在已有站点中运行 tola notes init new-site)初始化新站点:

nu
use tola get-init

def main [] {
  let init = get-init
  {
    build: {entry: {module: $init.module.name}}
    modules: {($init.module.name): {path: $init.module.root}}
  } | to toml | save --raw ($init.output-dir | path join "tola.toml")

  mkdir ($init.output-dir | path join "content")
  [
    $'#import "@tola-modules/($init.module.name):0.0.0": note'
    '#note(title: "欢迎")'
  ] | str join (char nl) | save --raw ($init.output-dir | path join "content" "index.typ")
}

所有生成文件必须写入 $init.output-dir(临时暂存目录,对 --dry-run 同样安全),$init.target-dir 仅用于计算相对路径。Tola 会改写初始化器自身的模块来源声明以保留用户传入的路径、Git 选择器或归档 URL,并将远程依赖锁进 tola.lock。初始化阶段不可调用 get-site 与 get-module;若需终端交互提问,先检查 $init.interactive。

打包发布

当模块包含需预先编译的前端资源时,在 [[hooks.before-pack]] 中执行编译,并在 [pack] 中列出随发行包交付的文件:

toml
[pack]
include = ["lib.typ", "site.typ", "assets", "queries", "tools", "README.md"]
exclude = ["assets/**/*.map"]

[[hooks.before-pack]]
name = "compile-assets"
nu = "author/assets.nu"

before-pack 脚本通过 get-context | get hook.inputDir 读取模块源码目录并写入编好的产物。打包、校验并制作发行归档:

sh
mkdir -p dist
tola module pack ./theme --output ./dist/theme
tola module check ./dist/theme
tar -czf dist/theme-v1.0.0.tar.gz -C dist theme

tola module pack 会自动复制 tola-module.toml 并移除其中的 [pack] 和 before-pack,消费方不需要安装作者侧构建工具。压缩归档必须保留外层目录(内含 tola-module.toml);由于 GitHub 自动生成的源码归档不会执行 before-pack,需将打包后的归档附在 Release 上,或将打包目录提交到仓库子目录供 git.subdir 引用。

发布前建议在独立测试站点验证:更换模块别名、覆盖选项、配置非根 base-path,并在不含作者侧构建工具的环境里跑通 tola check 与 tola preview。

站内搜索

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

输入关键词开始搜索。