@unifast/core

Các định nghĩa kiểu TypeScript, tiện ích HAST và lớp lỗi được dùng chung giữa các gói runtime của unifast

Tổng quan

@unifast/core cung cấp các định nghĩa kiểu TypeScript dùng chung cho tùy chọn biên dịch, kết quả, các nút HAST (HTML Abstract Syntax Tree), interface plugin và các lớp lỗi. Nó là dependency của các gói runtime như @unifast/node — bạn thường không cần cài đặt trực tiếp.

Để sử dụng trình biên dịch và các plugin tích hợp sẵn, hãy dùng @unifast/node.

Cài đặt

Định nghĩa kiểu

CompileOptions

Cấu hình cho hàm compile().

Thuộc tínhKiểuMặc địnhMô tả
inputKind"md" | "mdx""md"Định dạng đầu vào
outputKind"html" | "hast" | "mdast" | "mdxJs""html"Định dạng đầu ra
gfmobject-Các thiết lập GitHub Flavored Markdown
gfm.tablesboolean-Bật bảng GFM
gfm.taskListboolean-Bật checkbox danh sách tác vụ
gfm.strikethroughboolean-Bật ~~strikethrough~~
gfm.footnotesboolean-Bật chú thích cuối
gfm.autolinkboolean-Bật auto-link cho URL
frontmatterobject-Các thiết lập parsing frontmatter
frontmatter.yamlboolean-Parse frontmatter YAML
frontmatter.tomlboolean-Parse frontmatter TOML
frontmatter.jsonboolean-Parse frontmatter JSON
rawHtml"disallow" | "allowDangerous" | "parseAndSanitize"-Cách xử lý HTML thô trong source
sanitizeobject-Thiết lập sanitization HTML
sanitize.enabledboolean-Bật sanitization
sanitize.schemaSanitizeSchema-Quy tắc sanitization tùy chỉnh
highlightobject-Thiết lập syntax highlighting
highlight.enabledboolean-Bật syntax highlighting
highlight.engine"none" | "syntect" | "treeSitter"-Engine highlight
slugobject-Thiết lập sinh slug cho tiêu đề
slug.mode"github" | "unicode"-Thuật toán sinh slug
tocobject-Thiết lập mục lục
toc.enabledboolean-Bật trích xuất TOC
toc.maxDepthnumber-Độ sâu tiêu đề tối đa được bao gồm
diagnosticsobject-Thiết lập đầu ra diagnostic
diagnostics.format"compact" | "verbose"-Định dạng diagnostic
cacheobject-Thiết lập cache
cache.enabledboolean-Bật cache kết quả
cache.dirstring-Đường dẫn thư mục cache
pluginsUnifastPlugin[][]Mảng plugin được áp dụng

CompileResult

Giá trị trả về của hàm compile().

Thuộc tínhKiểuMô tả
outputstring | objectĐầu ra đã biên dịch (chuỗi HTML, HAST JSON, hoặc MDAST JSON tùy thuộc vào outputKind)
frontmatterRecord<string, unknown>Metadata frontmatter đã được parse
diagnosticsDiagnostic[]Mảng cảnh báo và lỗi
stats{ parseMs, transformMs, emitMs }Thống kê thời gian theo mili-giây
tocTocEntry[]Các mục của bảng mục lục đã được trích xuất

Diagnostic

Thuộc tínhKiểuMô tả
level"error" | "warn"Mức độ nghiêm trọng
messagestringThông điệp dễ đọc cho con người
startnumber | undefinedOffset byte bắt đầu trong source
endnumber | undefinedOffset byte kết thúc trong source
linenumber | undefinedSố dòng (bắt đầu từ 1)
columnnumber | undefinedSố cột (bắt đầu từ 1)

TocEntry

Một tiêu đề được trích xuất cho bảng mục lục.

Thuộc tínhKiểuMô tả
depthnumberCấp độ tiêu đề (1-6)
textstringNội dung văn bản thuần của tiêu đề
slugstringSlug đã sinh để dùng làm anchor link

SanitizeSchema

Các quy tắc sanitization tùy chỉnh.

Thuộc tínhKiểuMô tả
allowedTagsstring[]Các thẻ HTML được phép
allowedAttributesRecord<string, string[]>Các thuộc tính được phép cho từng thẻ
allowedProtocolsRecord<string, string[]>Các protocol URL được phép cho từng thuộc tính

UnifastPlugin

Interface plugin để mở rộng unifast.

Thuộc tínhKiểuMô tả
namestringTên plugin duy nhất
options?Partial<CompileOptions>Các tùy chọn sẽ được gộp vào tùy chọn biên dịch
hastTransform?(hast: HastRoot) => HastRootBiến đổi cây HAST sau khi biên dịch

Các loại nút HAST

KiểuTrường typeThuộc tínhMô tả
HastRoot"root"children: HastNode[]Nút gốc của cây
HastElement"element"tagName, properties, childrenMột phần tử HTML
HastText"text"value: stringMột nút văn bản
HastRaw"raw"value: stringĐi qua HTML thô
HastComment"comment"value: stringMột comment HTML
HastDoctype"doctype"-Một nút <!DOCTYPE html>

HastNode là kiểu union: HastRoot | HastElement | HastText | HastRaw | HastComment | HastDoctype

Các lớp lỗi

LớpKế thừaThuộc tínhMô tả
UnifastErrorErrorcode?: string, span?: { start, end }Lớp lỗi cơ sở cho tất cả lỗi của unifast
ParseErrorUnifastErrorcode: "PARSE_ERROR"Được ném ra khi không thể parse đầu vào Markdown/MDX
CompileErrorUnifastErrorcode: "COMPILE_ERROR"Được ném ra khi biên dịch thất bại sau khi parse
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}`);
  }
}

Tổng hợp các export

ExportLoạiMô tả
CompileOptionstypeCấu hình biên dịch
CompileResulttypeKết quả biên dịch
TocEntrytypeMục của bảng mục lục
SanitizeSchematypeQuy tắc sanitization
UnifastPlugintypeInterface plugin
HastNodetypeUnion của tất cả các loại nút HAST
HastRoottypeNút gốc HAST
HastElementtypeNút phần tử HAST
HastTexttypeNút văn bản HAST
HastRawtypeNút HTML thô HAST
HastCommenttypeNút comment HAST
HastDoctypetypeNút doctype HAST
hastToHtmlfunctionBộ tuần tự hóa HAST-to-HTML
escapeHtmlfunctionEscape các ký tự đặc biệt của HTML (&, <, >, ")
extractLangfunctionTrích xuất định danh ngôn ngữ từ phần tử HAST <code>
extractTextfunctionTrích xuất nội dung văn bản thuần từ một nút HAST
findCodeChildfunctionTìm phần tử con <code> bên trong phần tử <pre>
visitHastfunctionTiện ích visitor pattern để duyệt cây HAST
UnifastErrorclassLớp lỗi cơ sở
ParseErrorclassLớp lỗi parse
CompileErrorclassLớp lỗi biên dịch