@tola/code
在 HTML 导出中为代码块提供语法高亮与浅色/深色主题支持。
导入
#import "@tola/code:0.0.0": *导出成员
code-themes
code-themes: dictionary = (
github: path("/code-themes/github.tmTheme"),
github-dark: path("/code-themes/github-dark.tmTheme"),
tokyo-night: path("/code-themes/tokyo-night.tmTheme"),
tokyo-night-storm: path("/code-themes/tokyo-night-storm.tmTheme"),
tokyo-night-moon: path("/code-themes/tokyo-night-moon.tmTheme"),
tokyo-night-day: path("/code-themes/tokyo-night-day.tmTheme"),
atom-one: path("/code-themes/atom-one.tmTheme"),
atom-one-light: path("/code-themes/atom-one-light.tmTheme"),
a11y: path("/code-themes/a11y.tmTheme"),
a11y-dark: path("/code-themes/a11y-dark.tmTheme"),
dracula: path("/code-themes/dracula.tmTheme"),
catppuccin-frappe: path("/code-themes/catppuccin-frappe.tmTheme"),
catppuccin-latte: path("/code-themes/catppuccin-latte.tmTheme"),
catppuccin-macchiato: path("/code-themes/catppuccin-macchiato.tmTheme"),
catppuccin-mocha: path("/code-themes/catppuccin-mocha.tmTheme"),
gruvbox-dark: path("/code-themes/gruvbox-dark.tmTheme"),
gruvbox-light: path("/code-themes/gruvbox-light.tmTheme"),
nord: path("/code-themes/nord.tmTheme"),
solarized-dark: path("/code-themes/solarized-dark.tmTheme"),
solarized-light: path("/code-themes/solarized-light.tmTheme"),
monokai: path("/code-themes/monokai.tmTheme"),
monokai-bright: path("/code-themes/monokai-bright.tmTheme"),
monokai-light: path("/code-themes/monokai-light.tmTheme"),
monokai-origin: path("/code-themes/monokai-origin.tmTheme"),
leet: path("/code-themes/leet.tmTheme"),
coldark-cold: path("/code-themes/coldark-cold.tmTheme"),
coldark-dark: path("/code-themes/coldark-dark.tmTheme"),
two-dark: path("/code-themes/two-dark.tmTheme"),
zenburn: path("/code-themes/zenburn.tmTheme"),
snazzy: path("/code-themes/snazzy.tmTheme"),
)内置 .tmTheme 主题路径字典,供 raw.theme 与 render-code(dark-theme: ...) 使用。
直接将字典成员(如 code-themes.github、code-themes.tokyo-night)传给 #set raw(theme: ...) 或 render-code(dark-theme: ...)。
code-syntaxes
code-syntaxes: dictionary = (
cuda: bytes(94833),
fennel: bytes(15004),
just: bytes(25036),
lean4: bytes(4747),
nushell: bytes(14469),
scheme: bytes(54130),
)Tola 内置附加语法定义字典,按语言标签索引。
若 raw.syntaxes 中声明了同名语法,优先使用站点提供的版本。
code-language
let code-language(
language: str,
) = str;将语言别名规范化为 Typst raw(..., lang: ...) 可识别的语法名。
位置参数
languagestr必填:源语言标签或别名(例如"sbcl"→"lisp")。
render-code
let render-code(
raw: content,
attrs: dictionary = (:),
dark-theme: none | path | bytes = none,
) = content;将 Typst 已高亮的 raw 元素渲染为带主题样式的 HTML,支持浅/深色双主题。
通过 #show raw: render-code 应用(直接调用 render-code(raw(...)) 会报错,因为尚未经过 Typst 高亮)。非 HTML 导出时原样返回 raw 元素。
浅色主题由 #set raw(theme: ...) 指定(auto 用 Typst 默认主题,none 关闭浅色与深色高亮)。请将 code-stylesheet() 放入 HTML <head>,浏览器才会加载着色样式。
位置参数
rawcontent必填:由#show raw:规则传入的raw元素。
命名参数
dark-themenone | path | bytes默认值:none:当根 HTML 元素带data-theme="dark"时生效的主题(可传code-themes.*、path或bytes);省略时浅/深色沿用同一主题。attrsdictionary默认值:(:):附加到代码元素的 HTML 属性字典(如(class: "example",))。class与style会追加到原有值;data-lang由代码语言自动设置。
为 HTML 代码启用浅/深色主题高亮
#import "@tola/code:0.0.0": code-stylesheet, code-themes, render-code
#document("index.html", title: [Code])[
#html.elem("html", attrs: (lang: "en", "data-theme": "dark"))[
#html.head[
#html.title("Code")
#code-stylesheet()
]
#html.body[
#set raw(theme: code-themes.github)
#show raw: render-code.with(dark-theme: code-themes.github-dark)
#raw("let answer = 42;", lang: "rust", block: true)
]
]
]code-stylesheet-url
let code-stylesheet-url() = str;返回共享代码高亮样式表的 URL(已包含 site.base-path)。
适合需要为 <link> 附加自定义属性(如 media)的场景;被引用时该样式表会自动发布。
code-stylesheet
let code-stylesheet() = content;返回用于放入 <head> 的代码高亮样式表 <link rel="stylesheet"> 元素。