@unifast/core

在 unifast 執行時期套件之間共用的 TypeScript 型別定義、HAST 工具函式與錯誤類別

概觀

@unifast/core 提供了共用的 TypeScript 型別定義,涵蓋 compile 選項、結果、HAST(HTML Abstract Syntax Tree)節點、外掛介面以及錯誤類別。它是 @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>要合併至 compile 選項中的設定
hastTransform?(hast: HastRoot) => HastRoot在編譯完成後轉換 HAST 樹

HAST 節點型別

型別type 欄位屬性說明
HastRoot"root"children: HastNode[]樹的根節點
HastElement"element"tagNamepropertieschildrenHTML 元素
HastText"text"value: string文字節點
HastRaw"raw"value: string原始 HTML 直通節點
HastComment"comment"value: stringHTML 註解
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 樹的 visitor 工具
UnifastErrorclass基底錯誤類別
ParseErrorclass解析錯誤類別
CompileErrorclass編譯錯誤類別