@unifast/core

unifast 各运行时包共享的 TypeScript 类型定义、HAST 工具函数和错误类。

概述

@unifast/core 提供了共享的 TypeScript 类型定义,涵盖编译选项、编译结果、HAST(HTML 抽象语法树)节点、插件接口以及错误类。它是 @unifast/node 等运行时包的依赖 —— 通常无需直接安装。

要使用编译器和内置插件,请使用 @unifast/node

安装

类型定义

CompileOptions

compile() 函数的配置。

属性类型默认值描述
inputKind"md" | "mdx""md"输入格式
outputKind"html" | "hast" | "mdast" | "mdxJs""html"输出格式
gfmobject-GitHub Flavored Markdown 相关设置
gfm.tablesboolean-启用 GFM 表格
gfm.taskListboolean-启用任务列表复选框
gfm.strikethroughboolean-启用 ~~strikethrough~~
gfm.footnotesboolean-启用脚注
gfm.autolinkboolean-启用 URL 自动链接
frontmatterobject-Frontmatter 解析设置
frontmatter.yamlboolean-解析 YAML frontmatter
frontmatter.tomlboolean-解析 TOML frontmatter
frontmatter.jsonboolean-解析 JSON frontmatter
rawHtml"disallow" | "allowDangerous" | "parseAndSanitize"-控制源码中原始 HTML 的处理方式
sanitizeobject-HTML 净化设置
sanitize.enabledboolean-启用净化
sanitize.schemaSanitizeSchema-自定义净化规则
highlightobject-语法高亮设置
highlight.enabledboolean-启用语法高亮
highlight.engine"none" | "syntect" | "treeSitter"-高亮引擎
slugobject-标题 slug 生成设置
slug.mode"github" | "unicode"-slug 生成算法
tocobject-目录设置
toc.enabledboolean-启用目录提取
toc.maxDepthnumber-要包含的最大标题层级
diagnosticsobject-诊断输出设置
diagnostics.format"compact" | "verbose"-诊断格式
cacheobject-缓存设置
cache.enabledboolean-启用结果缓存
cache.dirstring-缓存目录路径
pluginsUnifastPlugin[][]要应用的插件数组

CompileResult

compile() 函数返回。

属性类型描述
outputstring | object编译输出(根据 outputKind 可能是 HTML 字符串、HAST JSON 或 MDAST JSON)
frontmatterRecord<string, unknown>解析后的 frontmatter 元数据
diagnosticsDiagnostic[]警告和错误的数组
stats{ parseMs, transformMs, emitMs }各阶段耗时(毫秒)
tocTocEntry[]提取出的目录条目

Diagnostic

属性类型描述
level"error" | "warn"严重级别
messagestring面向人类阅读的消息
startnumber | undefined源码中的起始字节偏移
endnumber | undefined源码中的结束字节偏移
linenumber | undefined行号(从 1 开始)
columnnumber | undefined列号(从 1 开始)

TocEntry

从目录中提取出的单条标题记录。

属性类型描述
depthnumber标题层级(1-6)
textstring标题的纯文本内容
slugstring用于锚点链接的 slug

SanitizeSchema

自定义净化规则。

属性类型描述
allowedTagsstring[]允许的 HTML 标签
allowedAttributesRecord<string, string[]>每个标签允许的属性
allowedProtocolsRecord<string, string[]>每个属性允许的 URL 协议

UnifastPlugin

用于扩展 unifast 的插件接口。

属性类型描述
namestring插件的唯一名称
options?Partial<CompileOptions>要合并到编译选项中的配置
hastTransform?(hast: HastRoot) => HastRoot在编译后转换 HAST 树

HAST 节点类型

类型type 字段属性描述
HastRoot"root"children: HastNode[]树的根节点
HastElement"element"tagNamepropertieschildren一个 HTML 元素
HastText"text"value: string一个文本节点
HastRaw"raw"value: string原样透传的 HTML
HastComment"comment"value: string一条 HTML 注释
HastDoctype"doctype"-一个 <!DOCTYPE html> 节点

HastNode 是联合类型:HastRoot | HastElement | HastText | HastRaw | HastComment | HastDoctype

错误类

继承自属性描述
UnifastErrorErrorcode?: stringspan?: { start, end }unifast 所有错误的基类
ParseErrorUnifastErrorcode: "PARSE_ERROR"当 Markdown/MDX 输入无法解析时抛出
CompileErrorUnifastErrorcode: "COMPILE_ERROR"当解析后编译失败时抛出
import { UnifastError, ParseError, CompileError } from "@unifast/core";

try {
  // ... compile something
} catch (err) {
  if (err instanceof ParseError) {
    console.error(`Parse error at ${err.span?.start}: ${err.message}`);
  } else if (err instanceof CompileError) {
    console.error(`Compile error: ${err.message}`);
  }
}

导出列表

导出类别描述
CompileOptionstype编译配置
CompileResulttype编译结果
TocEntrytype目录条目
SanitizeSchematype净化规则
UnifastPlugintype插件接口
HastNodetype所有 HAST 节点类型的联合
HastRoottypeHAST 根节点
HastElementtypeHAST 元素节点
HastTexttypeHAST 文本节点
HastRawtypeHAST 原始 HTML 节点
HastCommenttypeHAST 注释节点
HastDoctypetypeHAST doctype 节点
hastToHtmlfunctionHAST 到 HTML 的序列化函数
escapeHtmlfunction转义 HTML 特殊字符(&<>"
extractLangfunction从 HAST <code> 元素中提取语言标识
extractTextfunction从 HAST 节点中提取纯文本内容
findCodeChildfunction查找 <pre> 元素内的 <code> 子元素
visitHastfunction用于遍历 HAST 树的访问者工具
UnifastErrorclass错误基类
ParseErrorclass解析错误类
CompileErrorclass编译错误类