Table of contents
On-page tables of contents and heading permalinks read the current document’s outline. headings() from @tola/document:0.0.0 restricts results to the enclosing document(...) rather than the whole Bundle.
Nested TOC
Import only the symbols you need from @tola/document (or alias the module) so you do not shadow the built-in document(...) function. Call headings(depth: 2) and filter on h.outlined to skip headings marked outlined: false:
#import "@tola/document:0.0.0": headings
#let group-headings(items) = {
let groups = ()
for item in items {
if item.nesting == 1 or groups.len() == 0 {
groups.push((heading: item, children: ()))
} else {
groups.last().children.push(item)
}
}
groups
}
#let page-toc(title: "On this page") = context {
let items = headings(depth: 2).filter(h => h.outlined)
if items.len() == 0 { return }
html.aside(class: "toc")[
#html.nav(aria-label: title)[
#html.h2(title)
#html.ul[
#for group in group-headings(items) {
html.li[
#link(group.heading.location, group.heading.text)
#if group.children.len() > 0 {
html.ul[
#for sub in group.children {
html.li(link(sub.location, sub.text))
}
]
}
]
}
]
]
]
}Each record returned by headings() includes:
| Field | Meaning |
|---|---|
level | Effective heading level after heading.offset. |
nesting | Outline nesting depth starting at 1, or none when outlined: false. |
text / body | Plain text and rich content before show rules run. |
label | Explicit <label> attached to the heading, or none. |
location | Typst location; passing it to link(h.location, ...) emits an #anchor link. |
outlined | Whether the heading appears in outline(). |
Stable anchors
Auto-generated slugs break when heading wording changes or when non-ASCII titles are edited. Attach explicit English <label> tags while writing:
= Static assets <static-assets>
== Directory trees <asset-trees>In a custom show heading rule, prefer section.label as the HTML id and fall back to slugify(section.text, mode: "ascii") with -2, -3 deduplication when no label is attached:
#import "@tola/address:0.0.0": slugify
#import "@tola/document:0.0.0": headings
#let resolve-heading-ids(sections) = {
let used = (:)
for s in sections {
if s.label != none { used.insert(str(s.label), true) }
}
let ids = ()
for s in sections {
if not s.outlined or s.level > 5 {
ids.push(none)
continue
}
if s.label != none {
ids.push(str(s.label))
continue
}
let base = if s.text.contains(regex("[\\p{L}\\p{N}]")) {
slugify(s.text, mode: "ascii")
} else { "section" }
let id = base
let n = 2
while id in used {
id = base + "-" + str(n)
n += 1
}
used.insert(id, true)
ids.push(id)
}
ids
}Heading permalinks
Inside the article’s show heading rule, look up the matching entry in headings() via source.location() and emit <h2>–<h6> with an id and self-link:
#let heading-with-anchor(source) = context {
if target() != "html" or not source.outlined or source.level > 5 {
return source
}
let sections = headings()
let index = sections.position(s => s.location == source.location())
let section = sections.at(index)
let id = if section.label != none {
str(section.label)
} else {
resolve-heading-ids(sections).at(index)
}
let tag = "h" + str(section.level + 1)
let body = if section.number != none { section.number + [ ] + source.body } else { source.body }
let anchor = html.a(href: "#" + id, class: "heading-anchor", aria-label: "Permalink: " + section.text)[]
html.elem(tag, attrs: (id: id), body + anchor)
}Performance
Recomputing the full deduplicated slug list inside every single heading on a 200-heading reference page adds noticeable work. Take a fast path (str(section.label)) whenever section.label != none, and call resolve-heading-ids(sections) only when a heading lacks an explicit label. See theme/components/content/heading.typ in this repository for the full implementation.