代码高亮

@tola/code:0.0.0 将 Typst 原生语法高亮结果转为带语义类名和 CSS 变量的 HTML,并输出一份共享样式表,不需要在前端加载 Prism 或 Highlight.js。

亮暗主题

在 <head> 放入 code-stylesheet(),在正文作用域里用 set raw(theme: ...) 指定亮色主题,再用 show raw 绑定 render-code 并传入 dark-theme:

typst
#import "@tola/code:0.0.0": code-stylesheet, code-themes, render-code

#document("index.html", format: "html", title: "代码示例")[
  #html.html(lang: "zh-Hans")[
    #html.head[
      #html.title[代码示例]
      #code-stylesheet()
    ]
    #html.body[
      #set raw(theme: code-themes.github)
      #show raw: render-code.with(dark-theme: code-themes.github-dark)

      ```rust
      fn main() {
          println!("hello");
      }
      ```
    ]
  ]
]

code-stylesheet() 返回指向共享 CSS 的 <link rel="stylesheet"> 标签(已含 base-path)。当根元素带有 data-theme="dark" 时,暗色高亮配色自动生效;主题切换按钮与代码块外框(圆角、边距、背景)由站点 CSS 自行控制。

反引号行内代码默认不含语言标识,不会高亮;需要行内高亮时使用函数形式:#raw("let x = 1", lang: "rust")。

行高亮与 Diff

Typst 代码块的语言标记只允许 [A-Za-z0-9_-],不能像 Markdown 那样在语言名后面直接写 {2,4-5}。可以用一个短生命周期 state 函数在代码块前一行传入选项,并在 show raw.line 里给对应行加上 CSS 类:

typst
#import "@tola/code:0.0.0": render-code

#let _pending = state("code-lines", none)

#let code-lines(spec) = _pending.update(spec)
#let reset-code-lines() = _pending.update(none)

#let _parse-range(spec) = {
  let nums = ()
  for part in spec.split(",") {
    let bounds = part.trim().split("-")
    let start = int(bounds.first())
    let end = if bounds.len() > 1 { int(bounds.at(1)) } else { start }
    nums += range(start, end + 1)
  }
  nums
}

#let _decorate(body, hl: (), add: (), del: ()) = {
  show raw.line: line => {
    let cls = "line"
    if line.number in hl { cls += " hl" }
    if line.number in add { cls += " diff-add" }
    if line.number in del { cls += " diff-del" }
    html.span(class: cls, line.body)
  }
  body
}

#let render-raw(source, dark-theme: none) = {
  if not source.block { return render-code(source, dark-theme: dark-theme) }
  context {
    let spec = _pending.get()
    if spec != none { _pending.update(none) }
    let block = render-code(source, dark-theme: dark-theme)
    if spec == none { return block }
    let opts = (hl: (), add: (), del: ())
    for token in spec.split(regex("\\s+")) {
      if token == "" { continue }
      let (kind, rest) = token.split(":", limit: 2)
      opts.at(kind) = _parse-range(rest)
    }
    _decorate(block, ..opts)
  }
}
typst
#code-lines("hl:2 add:4-5 del:3")
```toml
[site]
title = "我的站点"
base-path = "/"
origin = "https://example.com"
base-path = "/docs/"
```

每篇文档开头记得调用 reset-code-lines(),避免状态跨文档泄露。

分发代码块

如果站点把 ```mermaid 这类代码块交给专门的绘图组件处理,在统一的 show raw 入口里先按 source.lang 查表分发,未命中再交给 render-code:

typst
#let fence-handlers = (
  "mermaid": mermaid-block,
)

#let render-raw(source, dark-theme: none) = {
  let handler = if source.lang != none { fence-handlers.at(source.lang, default: none) }
  if handler != none { return handler(source) }
  render-code(source, dark-theme: dark-theme)
}

自定义语法

包裹 render-code 时,可以用 html.elem("div", attrs: (class: "code-toolbar"), { ... }) 输出语言名与复制按钮(在代码模式 { ... } 中拼接控件,避免 Typst 生成多余的 <p> 标签),再用前端脚本监听点击复制 <code> 的 textContent。

Tola 已内置 nu 等常见语法定义(见 code-syntaxes),直接写 ```nu 即可生效。需要高亮其他语言时,将 .sublime-syntax 文件放进站点目录并设置 #set raw(syntaxes: ("/syntaxes/my-lang.sublime-syntax",)),站点提供的同名语法优先于内置定义。配色方面,除 code-themes 内置主题外,任意 .tmTheme 路径都能直接传给 set raw(theme: ...) 与 render-code(dark-theme: ...)。

站内搜索

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

输入关键词开始搜索。