Neighbors & pagination
Previous/next links and paginated index routes both operate on an ordered sequence of pages. Docs typically follow depth-first tree order, while blog archives sort newest-first by publication date before slicing.
By list order
For flat collections such as blog posts, sort by (published, id) and pass the list to adjacent from @tola/collection:0.0.0:
#import "@tola/address:0.0.0": output-url
#import "@tola/collection:0.0.0": adjacent
#let ordered-posts = posts.sorted(key: p => (p.meta.published.rfc3339, p.id)).rev()
#let post-neighbors(page) = {
let pair = adjacent(ordered-posts, page.output, key: item => item.output)
if pair == none { return }
html.nav(aria-label: "Post navigation", class: "neighbors")[
#if "prev" in pair {
html.a(href: output-url(pair.prev.output), class: "prev")[
Newer: #pair.prev.meta.title
]
}
#if "next" in pair {
html.a(href: output-url(pair.next.output), class: "next")[
Older: #pair.next.meta.title
]
}
]
}adjacent returns none when the target key is missing, and omits prev or next at the list boundaries.
To show a slice of nearby entries instead of only immediate neighbors, use window:
#import "@tola/collection:0.0.0": window
#let nearby = window(ordered-posts, page.output, key: p => p.output, before: 2, after: 2)By tree order
In multi-level documentation, previous/next links should follow the expanded sidebar order rather than filenames or global order. Traverse the tree built in Trees & breadcrumbs in pre-order to collect real pages into reading-order, then feed that array to adjacent:
#let collect-pages(tree, key) = {
let found = ()
let node = tree.nodes.at(key)
if node.page != none { found.push(node.page) }
for child in tree.children.at(key, default: ()) {
found += collect-pages(tree, child.key)
}
found
}
#let reading-order = collect-pages(doc-tree, "/docs/")If an auto-generated API reference sits in the same tree, split out reference pages before calling adjacent so the last authored guide page does not link directly into dozens of generated entries.
Pagination
Because the site entry calls document(...) in ordinary Typst loops, pagination needs no special config: slice sorted posts by page size, emitting page 1 at /blog/ and subsequent pages at /blog/page/2/, /blog/page/3/, etc.
#import "@tola/address:0.0.0": output-of, output-url
#let page-size = 10
#let blog-page-output(page-num) = {
if page-num == 1 { output-of("/blog/") }
else { output-of("/blog/page/" + str(page-num) + "/") }
}
#let chunk(items, size) = {
if items.len() == 0 { return ((),) }
let total = calc.ceil(items.len() / size)
range(0, total).map(i => {
let start = i * size
let end = calc.min(start + size, items.len())
items.slice(start, end)
})
}
#let render-pager(current, total) = {
if total <= 1 { return }
html.nav(aria-label: "Pagination", class: "pagination")[
#if current > 1 {
html.a(href: output-url(blog-page-output(current - 1)), rel: "prev")[Previous]
}
#for n in range(1, total + 1) {
let attrs = (href: output-url(blog-page-output(n)))
if n == current { attrs.insert("aria-current", "page") }
html.a(..attrs, str(n))
}
#if current < total {
html.a(href: output-url(blog-page-output(current + 1)), rel: "next")[Next]
}
]
}
#let slices = chunk(ordered-posts, page-size)
#let total-pages = slices.len()
#for (index, batch) in slices.enumerate() {
let page-num = index + 1
let title = if page-num == 1 { "Blog" } else { "Blog - Page " + str(page-num) }
let out = blog-page-output(page-num)
document(out, format: "html", title: title)[
#html.h1(title)
#html.ul[
#for post in batch {
html.li[
#html.a(href: output-url(post.output), post.meta.title)
#html.time(datetime: post.meta.published.rfc3339, post.meta.published.text)
]
}
]
#render-pager(page-num, total-pages)
]
}