反向链接

整座站点在同一个 Typst Bundle 中编译,每个页面都能在 context 内查询站内原生链接的指向关系。直接反向链接用 @tola/document:0.0.0 的 references(),多跳可达性与孤立页排查用 @tola/datalog:0.0.0。

references() 扫描整个 Bundle 中的 Typst link(...) 与 @ref(不含手写 html.a(...))。给每篇正文区域贴上 <article-body> 标签,把反向链接区放在 <article-body> 之外,就能避免页眉、侧边栏以及反链列表自身污染引用统计:

typst
#import "@tola/address:0.0.0": output-url
#import "@tola/document:0.0.0": references

// 入口处先建一张 output -> page 的索引,方便反查标题与摘要
#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[反向链接]
    #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)
          }
        ]
      }
    ]
  ]
}

模板中的放置方式:

typst
#html.main[
  #html.article[
    #include page.file
  ] <article-body>
  #backlinks-section()
]

to: auto 匹配指向当前文档(含文档内任意标题锚点)的引用;同一来源页多次引用时会返回多条记录,按 item.document.output 去重。正文里的逻辑路径经 show link 改写成 link(target.label, ...) 后,只要最外层仍是原生 link,就会被 references() 纳入统计。

出链

把参数换成 from: auto 可以列出当前文档发出的全部链接,并按 item.resolution 区分状态("found"、"external"、"unresolved"):

typst
#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")
  // 按需渲染外链清单或汇总诊断
}

需要校验手写 html.a(href: ...) 和静态资源路径时,仍以 tola check 为准。

Datalog 图查询

在 Forester 风格的笔记库或大型文档站里,单跳反链往往不够:你可能想找出从某篇索引页出发任意跳可达的全部笔记、两篇笔记的共同邻域,或者完全没有入链的孤立页面。这时可以把 references() 收集到的边喂给 @tola/datalog:0.0.0:

typst
#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)
    })

  // 如果引用目标使用页面 label,可先建 label -> output 映射后再构造 edge-rel.atom(from, to)
  // 下例演示基于明确边集合的传递闭包与孤立页推理:
  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: (
      // 直接边与多跳传递闭包
      rule(reachable.atom(x, y), edge-rel.atom(x, y)),
      rule(reachable.atom(x, z), reachable.atom(x, y), edge-rel.atom(y, z)),
      // 至少有一条站内入链
      rule(has-incoming.atom(y), edge-rel.atom(x, y)),
      // 没有任何入链的孤立页(使用分层否定 negate)
      rule(orphan.atom(x), page-rel.atom(x), negate(has-incoming.atom(x))),
    ),
  )

  db
}

查询某个入口页可达的全部页面,或列出所有孤立页:

typst
#let (target,) = ("target",).map(var)

// 从 index.html 出发可达的全部页面
#let reached = db.query(select: (target,), where: (reachable.atom("index.html", target),))

// 全站孤立页
#let orphans = db.query(select: (target,), where: (orphan.atom(target),))

导出图数据

如果要在页面中绘制交互式关系图,无需在 HTML 里硬塞内联脚本,直接在 Bundle 顶层用 asset(...) 产出一份 graph.json,前端按需 fetch 即可:

typst
#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))
}

站内搜索

可以用上下方向键选择结果。

输入关键词开始搜索。