@tola/diagnostic

在 Typst 求值期间向站点作者报告自定义错误、警告、信息与提示。

导入

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

导出成员

report

typc
let report(
  message: str,
  at: content | none | auto = auto,
  code: str | none = none,
  hints: array = (),
  related: array = (),
  severity: str = "warning",
) = none;

记录一条诊断并继续求值,便于一次收集多个问题。

任意 "error" 级别诊断会在求值结束后使 tola check 与 tola build 失败;"warning"、"info" 与 "hint" 不阻断构建。仅当无法继续求值时才使用 Typst 内置的 panic(...)。

位置参数

  • message str 必填:面向站点作者的清晰问题描述。

命名参数

  • severity str 默认值: "warning":诊断级别:"error"、"warning"(默认)、"info" 或 "hint"。
  • code str | none 默认值: none:可选规则标识,由站点或模块自行命名(例如 "site.missing-description")。
  • at content | none | auto 默认值: auto:源码位置:auto(默认)指向当前 report(...) 调用,传入 Typst 内容则指向该元素的源码位置,传 none 不附带位置。
  • hints array 默认值: ():可选的修复建议字符串数组。
  • related array 默认值: ():可选的关联位置数组,每项为 (message: str, at: content | none) 字典。

文章缺少简介时发出警告

typst
#import "@tola/diagnostic:0.0.0": report

#let check-page(title, description) = {
  if description == none {
    report(
      "页面 \"" + title + "\" 缺少 description。",
      severity: "warning",
      code: "site.missing-description",
      hints: ("在 `tola-meta` 中补充 `description` 字段。",),
    )
  }
}

站内搜索

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

输入关键词开始搜索。