Backlinks
Because the entire site compiles as one Typst Bundle, any page can inspect native link edges inside context. Direct backlinks only need references() from @tola/document:0.0.0; multi-hop reachability and orphan detection pair it with @tola/datalog:0.0.0.
Backlinks
references() scans native Typst link(...) and @ref elements across the Bundle (excluding raw html.a(...)). Label each article body with <article-body> and place the backlinks list outside that label so headers, sidebars, and the backlinks section itself do not count as references:
#import "@tola/address:0.0.0": output-url
#import "@tola/document:0.0.0": references
// Build an output -> page map once at the site entry
#let pages-by-output = index-by(pages, key: p => p.output)
#let backlinks-section() = context {
let seen = (:)
let incoming = ()
for item in references(to: auto, from-within: <article-body>) {
let src = item.document.output
if src in seen { continue }
seen.insert(src, true)
let page = pages-by-output.at(src, default: none)
if page != none { incoming.push(page) }
}
if incoming.len() == 0 { return }
html.section(class: "backlinks")[
#html.h2[Backlinks]
#html.ul[
#for page in incoming {
html.li[
#html.a(href: output-url(page.output), page.meta.title)
#if page.meta.description != "" {
html.p(page.meta.description)
}
]
}
]
]
}In your page template:
#html.main[
#html.article[
#include page.file
] <article-body>
#backlinks-section()
]to: auto matches links pointing to the current document (including any heading anchor inside it). Multiple citations from the same page yield multiple records, so deduplicate on item.document.output. Logical links rewritten by a show link rule to link(target.label, ...) still remain native link elements and are included automatically.
Outgoing links
Pass from: auto to inspect outgoing links from the current document and split them by item.resolution ("found", "external", "unresolved"):
#let outgoing-links() = context {
let links = references(from: auto, from-within: <article-body>)
let internal = links.filter(it => it.resolution == "found")
let external = links.filter(it => it.resolution == "external")
let broken = links.filter(it => it.resolution == "unresolved")
// Render link lists or report diagnostics as needed
}To verify raw html.a(href: ...) tags and static asset paths, rely on tola check.
Datalog graph queries
In a Forester-style digital garden or large knowledge base, single-hop backlinks are often not enough—you may want every page reachable from an index page, shared neighbors, or orphan pages with zero incoming links. Feed collected edges into @tola/datalog:0.0.0:
#import "@tola/datalog:0.0.0": database, negate, relation, rule, var
#import "@tola/document:0.0.0": references
#let build-link-graph(pages) = context {
let page-rel = relation("page", 1)
let edge-rel = relation("edge", 2)
let reachable = relation("reachable", 2)
let has-incoming = relation("has-incoming", 1)
let orphan = relation("orphan", 1)
let (x, y, z) = ("x", "y", "z").map(var)
let page-facts = pages.map(p => page-rel.atom(p.output))
let edge-facts = references(from-within: <article-body>)
.filter(r => r.resolution == "found")
.map(r => {
let from = r.document.output
let to = r.destination
(from, to)
})
// If links target page labels, resolve label -> output first to populate resolved-edges
let db = database(
(page-rel, edge-rel, reachable, has-incoming, orphan),
facts: page-facts + resolved-edges.map(((a, b)) => edge-rel.atom(a, b)),
rules: (
// Direct edge and transitive closure
rule(reachable.atom(x, y), edge-rel.atom(x, y)),
rule(reachable.atom(x, z), reachable.atom(x, y), edge-rel.atom(y, z)),
// At least one internal incoming link
rule(has-incoming.atom(y), edge-rel.atom(x, y)),
// Pages with no incoming links (stratified negation)
rule(orphan.atom(x), page-rel.atom(x), negate(has-incoming.atom(x))),
),
)
db
}Query reachable pages or all orphans:
#let (target,) = ("target",).map(var)
// All pages reachable from index.html
#let reached = db.query(select: (target,), where: (reachable.atom("index.html", target),))
// All orphan pages across the site
#let orphans = db.query(select: (target,), where: (orphan.atom(target),))Export graph JSON
To render an interactive graph on the client, emit a standalone graph.json at Bundle top level with asset(...) and fetch it on demand:
#import "@tola/address:0.0.0": output-url
#context {
let payload = (
nodes: pages.map(p => (id: p.output, title: p.meta.title, url: output-url(p.output))),
edges: resolved-edges.map(((from, to)) => (source: from, target: to)),
)
asset("graph.json", json.encode(payload))
}