@unifast/core

unifast runtime packages में साझा TypeScript type definitions, HAST utilities, और error classes

अवलोकन

@unifast/core compile options, results, HAST (HTML Abstract Syntax Tree) nodes, plugin interface, और error classes के लिए साझा TypeScript type definitions प्रदान करता है। यह @unifast/node जैसे runtime packages की एक dependency है — आपको आमतौर पर इसे सीधे install करने की ज़रूरत नहीं होती

कंपाइलर और अंतर्निहित plugins के लिए, @unifast/node का उपयोग करें।

इंस्टॉलेशन

Type Definitions

CompileOptions

compile() function के लिए configuration।

PropertyTypeDefaultविवरण
inputKind"md" | "mdx""md"Input format
outputKind"html" | "hast" | "mdast" | "mdxJs""html"Output format
gfmobject-GitHub Flavored Markdown settings
gfm.tablesboolean-GFM tables enable करें
gfm.taskListboolean-task list checkboxes enable करें
gfm.strikethroughboolean-~~strikethrough~~ enable करें
gfm.footnotesboolean-footnotes enable करें
gfm.autolinkboolean-URL auto-linking enable करें
frontmatterobject-Frontmatter parsing settings
frontmatter.yamlboolean-YAML frontmatter parse करें
frontmatter.tomlboolean-TOML frontmatter parse करें
frontmatter.jsonboolean-JSON frontmatter parse करें
rawHtml"disallow" | "allowDangerous" | "parseAndSanitize"-source में raw HTML को कैसे handle करें
sanitizeobject-HTML sanitization settings
sanitize.enabledboolean-sanitization enable करें
sanitize.schemaSanitizeSchema-custom sanitization rules
highlightobject-Syntax highlighting settings
highlight.enabledboolean-syntax highlighting enable करें
highlight.engine"none" | "syntect" | "treeSitter"-Highlighting engine
slugobject-Heading slug generation settings
slug.mode"github" | "unicode"-Slug generation algorithm
tocobject-Table of contents settings
toc.enabledboolean-TOC extraction enable करें
toc.maxDepthnumber-शामिल करने के लिए अधिकतम heading depth
diagnosticsobject-Diagnostic output settings
diagnostics.format"compact" | "verbose"-Diagnostic format
cacheobject-Caching settings
cache.enabledboolean-result caching enable करें
cache.dirstring-Cache directory path
pluginsUnifastPlugin[][]लागू करने के लिए plugins का array

CompileResult

compile() function द्वारा returned।

PropertyTypeविवरण
outputstring | objectCompiled output (outputKind के आधार पर HTML string, HAST JSON, या MDAST JSON)
frontmatterRecord<string, unknown>Parsed frontmatter metadata
diagnosticsDiagnostic[]warnings और errors का array
stats{ parseMs, transformMs, emitMs }milliseconds में timing breakdown
tocTocEntry[]निकाले गए table of contents entries

Diagnostic

PropertyTypeविवरण
level"error" | "warn"Severity level
messagestringमानव-पठनीय message
startnumber | undefinedsource में Start byte offset
endnumber | undefinedsource में End byte offset
linenumber | undefinedLine number (1-based)
columnnumber | undefinedColumn number (1-based)

TocEntry

Table of contents के लिए निकाला गया एक heading।

PropertyTypeविवरण
depthnumberHeading level (1-6)
textstringheading का plain text content
slugstringanchor linking के लिए उत्पन्न slug

SanitizeSchema

Custom sanitization rules।

PropertyTypeविवरण
allowedTagsstring[]अनुमत HTML tags
allowedAttributesRecord<string, string[]>प्रति tag अनुमत attributes
allowedProtocolsRecord<string, string[]>प्रति attribute अनुमत URL protocols

UnifastPlugin

unifast को विस्तारित करने के लिए plugin interface।

PropertyTypeविवरण
namestringविशिष्ट plugin नाम
options?Partial<CompileOptions>compile options में merge किए जाने वाले options
hastTransform?(hast: HastRoot) => HastRootcompilation के बाद HAST tree को transform करें

HAST Node Types

Typetype fieldPropertiesविवरण
HastRoot"root"children: HastNode[]tree का root node
HastElement"element"tagName, properties, childrenएक HTML element
HastText"text"value: stringएक text node
HastRaw"raw"value: stringRaw HTML passthrough
HastComment"comment"value: stringएक HTML comment
HastDoctype"doctype"-एक <!DOCTYPE html> node

HastNode union type है: HastRoot | HastElement | HastText | HastRaw | HastComment | HastDoctype

Error Classes

ClassExtendsPropertiesविवरण
UnifastErrorErrorcode?: string, span?: { start, end }सभी unifast errors के लिए base error class
ParseErrorUnifastErrorcode: "PARSE_ERROR"जब Markdown/MDX input parse नहीं हो पाता तो throw किया जाता है
CompileErrorUnifastErrorcode: "COMPILE_ERROR"parsing के बाद compilation fail होने पर throw किया जाता है
import { UnifastError, ParseError, CompileError } from "@unifast/core";

try {
  // ... कुछ compile करें
} 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}`);
  }
}

Exports सारांश

ExportKindविवरण
CompileOptionstypeCompilation configuration
CompileResulttypeCompilation result
TocEntrytypeTable of contents entry
SanitizeSchematypeSanitization rules
UnifastPlugintypePlugin interface
HastNodetypeसभी HAST node types का union
HastRoottypeHAST root node
HastElementtypeHAST element node
HastTexttypeHAST text node
HastRawtypeHAST raw HTML node
HastCommenttypeHAST comment node
HastDoctypetypeHAST doctype node
hastToHtmlfunctionHAST-to-HTML serializer
escapeHtmlfunctionHTML special characters escape करें (&, <, >, ")
extractLangfunctionएक HAST <code> element से language identifier निकालें
extractTextfunctionएक HAST node से plain text content निकालें
findCodeChildfunctionएक <pre> element के अंदर <code> child element खोजें
visitHastfunctionHAST trees को walk करने के लिए visitor pattern utility
UnifastErrorclassBase error class
ParseErrorclassParse error class
CompileErrorclassCompile error class