@tola/document

读取当前文档信息、列出文档标题、查询跨文档引用及提取纯文本。

导入

typst
#import "@tola/document:0.0.0": *

current-document、headings 与 references 均在 #document(...) 正文的 context 中调用。建议按成员导入(或为包起别名),避免覆盖 Typst 内置的 document(...)。

导出成员

plain-text

typc
let plain-text(
  value: any,
) = str;

从字符串或 Typst 内容提取纯文本,不执行排版与 show rule。

保留文本、符号、空格、代码与弯引号,递归展开格式、链接与数学元素,跳过 metadata 与无法提取文本的空元素。

位置参数

  • value any 必填:待提取文本的字符串或 Typst 内容。

提取富文本内容中的纯文本

typst
#import "@tola/document:0.0.0": plain-text

#let text = plain-text([Hello, *world*!])

current-document

typc
let current-document() = dictionary;

在 context 中返回当前所在 #document(...) 的信息。

返回包含以下字段的字典:

  • output:相对于发布目录的输出路径(例如 "notes/index.html")。
  • route:不含部署前缀的站点根路由(例如 "/notes/")。
  • location:当前文档的 Typst location,可传给 link(...) 或 query(selector(...).within(current-document().location))。

将 Typst 查询限定在当前文档内

typst
#import "@tola/document:0.0.0": current-document

#document("index.html")[
  = 概览
  #context {
    let here = current-document()
    let local-headings = query(selector(heading).within(here.location))
    [路由:#here.route(共 #local-headings.len() 个标题)]
  }
]

references

typc
let references(
  from: any = none,
  from-within: any = none,
  to: any = none,
  to-within: any = none,
) = array;

按文档顺序查询整个 Bundle 中的 Typst link 与 ref(例如实现反向链接)。

在 context 中调用。四个筛选参数均接受 label、location、可定位元素函数、selector、auto(仅 from/to)或 none,多个条件需同时满足。结果仅包含 show rule 生效后仍为最外层 link/ref 的元素,不含手写 HTML 链接(完整 HTML 校验请运行 tola check)。

结果字段含义
elementshow rule 生效后的 link 或 ref 元素。
document来源文档的 (output, route, location) 字典。
destination元素上声明的链接目标。
resolution"found"、"external" 或 "unresolved"。
reason仅在 "unresolved" 时提供:(tag, message, help)(outside-mount、invalid-destination、no-such-target、positional-destination、label-has-no-location、ambiguous-label)。
target目标字典 (kind, output, route, query, fragment, location)(kind 为 "output"、"element" 或 "url"),不可用时为 none。

若根据查询结果动态渲染新链接,请将其置于 from-within 范围之外以保持查询结果稳定。

命名参数

  • from any 默认值: none:仅查询所选元素所在文档中的链接(auto 表示当前文档,none 表示全部文档)。
  • from-within any 默认值: none:仅查询出现在所选祖先元素范围内的链接。
  • to any 默认值: none:仅查询指向所选文档、资源或元素的链接(auto 表示当前文档)。
  • to-within any 默认值: none:仅查询指向所选祖先范围内 Typst 元素的链接。

列出链接到当前页面的反向链接

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

#document("a.html", title: [A])[#link(output-url("b.html"))[查看 B]]
#document("b.html", title: [B])[
  #context {
    let incoming = references(to: auto)
    for item in incoming [来自 #item.document.route 的引用]
  }
]

headings

typc
let headings(
  depth: none | int = none,
) = array;

按文档顺序列出当前文档的所有标题。

在 context 中调用。列出当前文档内的全部标题,包括 outlined: false 的标题(如需与 Typst outline() 保持一致,按 h.outlined 过滤)。

字段含义
level计入 heading.offset 后的实际层级。
nesting在大纲中的嵌套层级;不参与大纲时为 none。
number编号结果,无编号时为 none。
body / textshow rule 生效前的标题内容与纯文本。
label标题标签,无标签时为 none。
location标题的 Typst location,传给 link(...) 时 HTML 导出会生成锚点 ID。
outlined是否参与大纲。

命名参数

  • depth none | int 默认值: none:可选正整数,限制包含 heading.offset 后的最大层级;省略或传 none 返回全部标题。

生成二级页内目录

typst
#import "@tola/document:0.0.0": headings

#document("index.html")[
  #context {
    let toc = headings(depth: 2).filter(h => h.outlined)
    for h in toc [#link(h.location, h.body) ]
  }
  = 简介
  == 细节
]

站内搜索

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

输入关键词开始搜索。