Code blocks
@tola/code:0.0.0 converts Typst’s native syntax highlighting into HTML with semantic classes and CSS custom properties alongside one shared stylesheet, requiring no client-side highlighter.
Light & dark themes
Place code-stylesheet() in <head>, set the light theme with set raw(theme: ...), and pass dark-theme to render-code in show raw:
#import "@tola/code:0.0.0": code-stylesheet, code-themes, render-code
#document("index.html", format: "html", title: "Code example")[
#html.html(lang: "en")[
#html.head[
#html.title[Code example]
#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() returns the <link rel="stylesheet"> tag (with base-path included). Dark colors activate whenever the root element has data-theme="dark"; the toggle button and outer frame styles are up to your site CSS.
Backtick inline code defaults to no language tag and skips highlighting; pass a language explicitly with #raw("let x = 1", lang: "rust").
Line highlights & diff
Typst’s fenced language tag accepts only [A-Za-z0-9_-], not Markdown suffixes like {2,4-5}. Pass options on the line before a block using a short-lived state, and apply CSS classes inside show raw.line:
#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)
}
}#code-lines("hl:2 add:4-5 del:3")
```toml
[site]
title = "My Site"
base-path = "/"
origin = "https://example.com"
base-path = "/docs/"
```Call reset-code-lines() at the start of every document so pending options cannot leak across pages.
Dispatching code blocks
When custom block languages such as ```mermaid map to specialized diagram components, look up source.lang in a handler table before falling back to render-code:
#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)
}Custom syntaxes
To add a language label and copy button around render-code, emit html.elem("div", attrs: (class: "code-toolbar"), { ... }) in code mode (avoiding stray <p> tags) and wire a small client script that copies <code>‘s textContent.
Tola bundles common extra syntaxes such as nu (see code-syntaxes), so ```nu works out of the box. To highlight additional languages, add .sublime-syntax files to your project and set #set raw(syntaxes: ("/syntaxes/my-lang.sublime-syntax",)); site definitions override built-ins of the same name. Custom .tmTheme paths also work anywhere code-themes does.