{
  "url": "https://unifast.dev/zh-TW/docs/packages/core/overview/",
  "locale": "zh-TW",
  "title": "@unifast/core",
  "description": "在 unifast 執行時期套件之間共用的 TypeScript 型別定義、HAST 工具函式與錯誤類別",
  "section": "packages",
  "body": "## 概觀\n\n`@unifast/core` 提供了共用的 TypeScript 型別定義，涵蓋 compile 選項、結果、HAST（HTML Abstract Syntax Tree）節點、外掛介面以及錯誤類別。它是 `@unifast/node` 等執行時期套件的相依套件──您通常**不**需要直接安裝它。\n\n若要使用編譯器與內建外掛，請改用 [`@unifast/node`](/docs/packages/node/overview)。\n\n## 安裝\n\n## 型別定義\n\n### CompileOptions\n\n`compile()` 函式的設定選項。\n\n| 屬性 | 型別 | 預設值 | 說明 |\n|----------|------|---------|-------------|\n| `inputKind` | `\"md\" \\| \"mdx\"` | `\"md\"` | 輸入格式 |\n| `outputKind` | `\"html\" \\| \"hast\" \\| \"mdast\" \\| \"mdxJs\"` | `\"html\"` | 輸出格式 |\n| `gfm` | `object` | - | GitHub Flavored Markdown 設定 |\n| `gfm.tables` | `boolean` | - | 啟用 GFM 表格 |\n| `gfm.taskList` | `boolean` | - | 啟用任務清單核取方塊 |\n| `gfm.strikethrough` | `boolean` | - | 啟用 `~~strikethrough~~` |\n| `gfm.footnotes` | `boolean` | - | 啟用註腳 |\n| `gfm.autolink` | `boolean` | - | 啟用 URL 自動連結 |\n| `frontmatter` | `object` | - | Frontmatter 解析設定 |\n| `frontmatter.yaml` | `boolean` | - | 解析 YAML frontmatter |\n| `frontmatter.toml` | `boolean` | - | 解析 TOML frontmatter |\n| `frontmatter.json` | `boolean` | - | 解析 JSON frontmatter |\n| `rawHtml` | `\"disallow\" \\| \"allowDangerous\" \\| \"parseAndSanitize\"` | - | 原始碼中原始 HTML 的處理方式 |\n| `sanitize` | `object` | - | HTML 消毒處理設定 |\n| `sanitize.enabled` | `boolean` | - | 啟用消毒處理 |\n| `sanitize.schema` | `SanitizeSchema` | - | 自訂消毒規則 |\n| `highlight` | `object` | - | 語法高亮設定 |\n| `highlight.enabled` | `boolean` | - | 啟用語法高亮 |\n| `highlight.engine` | `\"none\" \\| \"syntect\" \\| \"treeSitter\"` | - | 高亮引擎 |\n| `slug` | `object` | - | 標題 slug 產生設定 |\n| `slug.mode` | `\"github\" \\| \"unicode\"` | - | slug 產生演算法 |\n| `toc` | `object` | - | 目錄設定 |\n| `toc.enabled` | `boolean` | - | 啟用目錄擷取 |\n| `toc.maxDepth` | `number` | - | 要納入的最大標題深度 |\n| `diagnostics` | `object` | - | 診斷訊息輸出設定 |\n| `diagnostics.format` | `\"compact\" \\| \"verbose\"` | - | 診斷訊息格式 |\n| `cache` | `object` | - | 快取設定 |\n| `cache.enabled` | `boolean` | - | 啟用結果快取 |\n| `cache.dir` | `string` | - | 快取目錄路徑 |\n| `plugins` | `UnifastPlugin[]` | `[]` | 要套用的外掛陣列 |\n\n### CompileResult\n\n`compile()` 函式的回傳值。\n\n| 屬性 | 型別 | 說明 |\n|----------|------|-------------|\n| `output` | `string \\| object` | 編譯後的輸出（依 `outputKind` 而定，可能是 HTML 字串、HAST JSON 或 MDAST JSON） |\n| `frontmatter` | `Record<string, unknown>` | 已解析的 frontmatter 後設資料 |\n| `diagnostics` | `Diagnostic[]` | 警告與錯誤的陣列 |\n| `stats` | `{ parseMs, transformMs, emitMs }` | 各階段耗時（單位為毫秒） |\n| `toc` | `TocEntry[]` | 擷取而得的目錄項目 |\n\n### Diagnostic\n\n| 屬性 | 型別 | 說明 |\n|----------|------|-------------|\n| `level` | `\"error\" \\| \"warn\"` | 嚴重度層級 |\n| `message` | `string` | 人類可讀的訊息內容 |\n| `start` | `number \\| undefined` | 在原始碼中的起始位元組位移 |\n| `end` | `number \\| undefined` | 在原始碼中的結束位元組位移 |\n| `line` | `number \\| undefined` | 行號（從 1 開始） |\n| `column` | `number \\| undefined` | 欄位編號（從 1 開始） |\n\n### TocEntry\n\n為目錄擷取出的單一標題項目。\n\n| 屬性 | 型別 | 說明 |\n|----------|------|-------------|\n| `depth` | `number` | 標題層級（1-6） |\n| `text` | `string` | 標題的純文字內容 |\n| `slug` | `string` | 用於錨點連結的 slug |\n\n### SanitizeSchema\n\n自訂的消毒規則。\n\n| 屬性 | 型別 | 說明 |\n|----------|------|-------------|\n| `allowedTags` | `string[]` | 允許的 HTML 標籤 |\n| `allowedAttributes` | `Record<string, string[]>` | 每個標籤允許的屬性 |\n| `allowedProtocols` | `Record<string, string[]>` | 每個屬性允許的 URL 協定 |\n\n### UnifastPlugin\n\n用於擴充 unifast 的外掛介面。\n\n| 屬性 | 型別 | 說明 |\n|----------|------|-------------|\n| `name` | `string` | 唯一的外掛名稱 |\n| `options?` | `Partial<CompileOptions>` | 要合併至 compile 選項中的設定 |\n| `hastTransform?` | `(hast: HastRoot) => HastRoot` | 在編譯完成後轉換 HAST 樹 |\n\n### HAST 節點型別\n\n| 型別 | `type` 欄位 | 屬性 | 說明 |\n|------|-------------|------------|-------------|\n| `HastRoot` | `\"root\"` | `children: HastNode[]` | 樹的根節點 |\n| `HastElement` | `\"element\"` | `tagName`、`properties`、`children` | HTML 元素 |\n| `HastText` | `\"text\"` | `value: string` | 文字節點 |\n| `HastRaw` | `\"raw\"` | `value: string` | 原始 HTML 直通節點 |\n| `HastComment` | `\"comment\"` | `value: string` | HTML 註解 |\n| `HastDoctype` | `\"doctype\"` | - | `<!DOCTYPE html>` 節點 |\n\n`HastNode` 是聯集型別：`HastRoot | HastElement | HastText | HastRaw | HastComment | HastDoctype`\n\n## 錯誤類別\n\n| 類別 | 繼承自 | 屬性 | 說明 |\n|-------|---------|------------|-------------|\n| `UnifastError` | `Error` | `code?: string`、`span?: { start, end }` | 所有 unifast 錯誤的基底類別 |\n| `ParseError` | `UnifastError` | `code: \"PARSE_ERROR\"` | 無法解析 Markdown/MDX 輸入時丟出 |\n| `CompileError` | `UnifastError` | `code: \"COMPILE_ERROR\"` | 在解析之後編譯失敗時丟出 |\n\n```ts\n\ntry {\n  // ... compile something\n} catch (err) {\n  if (err instanceof ParseError) {\n    console.error(`Parse error at ${err.span?.start}: ${err.message}`);\n  } else if (err instanceof CompileError) {\n    console.error(`Compile error: ${err.message}`);\n  }\n}\n```\n\n## 匯出列表\n\n| 匯出名稱 | 類別 | 說明 |\n|--------|------|-------------|\n| `CompileOptions` | type | 編譯設定 |\n| `CompileResult` | type | 編譯結果 |\n| `TocEntry` | type | 目錄項目 |\n| `SanitizeSchema` | type | 消毒規則 |\n| `UnifastPlugin` | type | 外掛介面 |\n| `HastNode` | type | 所有 HAST 節點型別的聯集 |\n| `HastRoot` | type | HAST 根節點 |\n| `HastElement` | type | HAST 元素節點 |\n| `HastText` | type | HAST 文字節點 |\n| `HastRaw` | type | HAST 原始 HTML 節點 |\n| `HastComment` | type | HAST 註解節點 |\n| `HastDoctype` | type | HAST doctype 節點 |\n| `hastToHtml` | function | HAST 轉 HTML 的序列化器 |\n| `escapeHtml` | function | 跳脫 HTML 特殊字元（`&`、`<`、`>`、`\"`） |\n| `extractLang` | function | 從 HAST `<code>` 元素中擷取語言識別字 |\n| `extractText` | function | 從 HAST 節點擷取純文字內容 |\n| `findCodeChild` | function | 在 `<pre>` 元素中尋找 `<code>` 子元素 |\n| `visitHast` | function | 用於走訪 HAST 樹的 visitor 工具 |\n| `UnifastError` | class | 基底錯誤類別 |\n| `ParseError` | class | 解析錯誤類別 |\n| `CompileError` | class | 編譯錯誤類別 |",
  "alternates": [
    {
      "locale": "en",
      "url": "https://unifast.dev/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/docs/packages/core/overview.json"
    },
    {
      "locale": "ja",
      "url": "https://unifast.dev/ja/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/ja/docs/packages/core/overview.json"
    },
    {
      "locale": "zh-CN",
      "url": "https://unifast.dev/zh-CN/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/zh-CN/docs/packages/core/overview.json"
    },
    {
      "locale": "zh-TW",
      "url": "https://unifast.dev/zh-TW/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/zh-TW/docs/packages/core/overview.json"
    },
    {
      "locale": "ko",
      "url": "https://unifast.dev/ko/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/ko/docs/packages/core/overview.json"
    },
    {
      "locale": "fr",
      "url": "https://unifast.dev/fr/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/fr/docs/packages/core/overview.json"
    },
    {
      "locale": "it",
      "url": "https://unifast.dev/it/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/it/docs/packages/core/overview.json"
    },
    {
      "locale": "es",
      "url": "https://unifast.dev/es/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/es/docs/packages/core/overview.json"
    },
    {
      "locale": "pt-BR",
      "url": "https://unifast.dev/pt-BR/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/pt-BR/docs/packages/core/overview.json"
    },
    {
      "locale": "de",
      "url": "https://unifast.dev/de/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/de/docs/packages/core/overview.json"
    },
    {
      "locale": "ru",
      "url": "https://unifast.dev/ru/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/ru/docs/packages/core/overview.json"
    },
    {
      "locale": "hi",
      "url": "https://unifast.dev/hi/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/hi/docs/packages/core/overview.json"
    },
    {
      "locale": "id",
      "url": "https://unifast.dev/id/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/id/docs/packages/core/overview.json"
    },
    {
      "locale": "tr",
      "url": "https://unifast.dev/tr/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/tr/docs/packages/core/overview.json"
    },
    {
      "locale": "vi",
      "url": "https://unifast.dev/vi/docs/packages/core/overview/",
      "api": "https://unifast.dev//api/vi/docs/packages/core/overview.json"
    }
  ]
}
