快速上手

本节从空目录出发,先用 tola init 跑起一个最小站点,再逐步加上页面元数据、公共外壳与 CSS 样式,最后校验并构建发布目录。

初始化项目

运行 tola init 创建新站点(可以先加 --dry-run 预览将要生成的文件):

sh
tola init my-site --dry-run
tola init my-site
cd my-site

init 默认要求目标目录不存在或为空(已有其他文件时可传 --force,不会覆盖已有同名文件)。默认生成的目录结构如下:

text
my-site/
├── tola.toml        # 站点配置
├── site.typ         # 默认入口程序
├── content/         # 内容源文件目录
├── .gitignore
└── .ignore

生成的 site.typ 已经包含基本的源扫描循环,可以把 content/ 下的 .typ 文件自动编译为 HTML 并附带一个默认 404.html。如果要直接从某个主题或脚手架模块创建站点,可运行 tola init my-site --from <路径或地址> --alias <别名>,见 模块与主题。

编写页面

在 content/ 下新建首页与一篇子页面:

typst
= 我的站点

欢迎来到我的第一个 Tola 站点。查看 #link("/guide/install/")[安装说明]。
typst
= 安装说明

运行 `tola --version` 检查本地版本。

$ a^2 + b^2 = c^2 $

启动开发服务器:

sh
tola dev

打开终端打印的地址(默认 http://127.0.0.1:5277/),点击“安装说明”即可访问 /guide/install/。修改并保存 content/guide/install.typ,浏览器会自动刷新,不需要重启服务。

布局与样式

下面为每篇页面补充标题元数据,并加上统一的 HTML 外壳与样式表。

先给两篇内容顶部加上 tola-meta 并改用 #title() 输出 <h1>:

typst
#import "@tola/source:0.0.0": tola-meta
#tola-meta((title: "我的站点"))

#title()

欢迎来到我的第一个 Tola 站点。查看 #link("/guide/install/")[安装说明]。
typst
#import "@tola/source:0.0.0": tola-meta
#tola-meta((title: "安装说明"))

#title()

运行 `tola --version` 检查本地版本。

$ a^2 + b^2 = c^2 $

接着准备样式表、在 tola.toml 声明静态资产,并更新 site.typ 校验元数据与输出统一外壳:

css
body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  line-height: 1.7;
  color: #1f2937;
}
main {
  max-width: 44rem;
  margin: 3rem auto;
  padding: 0 1.25rem;
}
toml
[site]
title = "我的站点"
language = "zh-Hans"

[assets]
files = [{ source = "static/site.css", url = "/site.css" }]
typst
#import "@tola/address:0.0.0": asset-url, join-route, output-of
#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

#let page-schema = s.schema((title: s.non-empty(s.trim(str))))
#let sources = parse-sources(all-sources(), page-schema)

#set text(
  lang: site.language.lang,
  script: site.language.script,
  region: site.language.region,
)

#for source in sources {
  let output = output-of(join-route(source.route-segments))
  document(output, format: "html", title: 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[#source.meta.title | #site.title]
        #html.link(rel: "stylesheet", href: asset-url("/site.css"))
      ]
      #html.body[
        #html.main[#include source.file]
      ]
    ]
  ]
}

#document("404.html", format: "html", title: "找不到页面")[
  #html.main[= 找不到页面]
]

保存后回到浏览器,两篇页面都会自动应用新布局和样式。

校验构建

可以试着把首页里的 #link("/guide/install/") 临时改成 #link("/guide/missing/") 并保存,终端会立即报告断链;改回正确路径后即恢复。

按 Ctrl+C 停止开发服务器,运行全站校验与生产构建:

sh
tola check
tola build

tola check 完整编译并校验输出路径、静态资源和站内链接,不会改动 public/。tola build 在校验通过后把产物原子写入 public/:

text
public/
├── index.html
├── 404.html
├── site.css
└── guide/
    └── install/
        └── index.html

站内搜索

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

输入关键词开始搜索。