@unifast/react

Convert unifast HAST output to React elements with component mapping

Overview

@unifast/react converts unifast HAST (HTML Abstract Syntax Tree) output into React elements. It provides two main functions:

  • compileToReact() - High-level: compile Markdown/MDX and return React elements directly

  • hastToReact() - Low-level: convert a HAST tree into React elements

The package automatically handles HTML-to-React property renaming (class to className, for to htmlFor, etc.) and CSS style string parsing.

Installation

Peer dependency: @unifast/node is required for compileToReact.

Type Definitions

CompileToReactOptions

Extends CompileOptions with React-specific fields.

PropertyTypeDescription
createElementCreateElementReact’s createElement function
FragmentunknownReact’s Fragment component
components?ComponentMapMap of HTML tag names to React components
(all CompileOptions)-Inherited from CompileOptions

CompileToReactResult

PropertyTypeDescription
elementunknownThe root React element
frontmatterRecord<string, unknown>Parsed frontmatter metadata
diagnosticsDiagnostic[]Warnings and errors
stats{ parseMs, transformMs, emitMs }Timing breakdown (ms)
tocTocEntry[]Extracted table of contents

HastToReactOptions

PropertyTypeDescription
createElementCreateElementReact’s createElement function
FragmentunknownReact’s Fragment component
components?ComponentMapMap of HTML tag names to custom React components

CreateElement

type CreateElement = (type: any, props: any, ...children: any[]) => any;

ComponentMap

type ComponentMap = Record<string, unknown>;

Property Conversion

HTML AttributeReact Prop
classclassName
forhtmlFor
accesskeyaccessKey
autocompleteautoComplete
autofocusautoFocus
autoplayautoPlay
charsetcharSet
colspancolSpan
contenteditablecontentEditable
crossorigincrossOrigin
datetimedateTime
enctypeencType
formactionformAction
hreflanghrefLang
httpequivhttpEquiv
inputmodeinputMode
maxlengthmaxLength
minlengthminLength
novalidatenoValidate
readonlyreadOnly
referrerpolicyreferrerPolicy
rowspanrowSpan
spellcheckspellCheck
srcdocsrcDoc
srclangsrcLang
srcsetsrcSet
tabindextabIndex
usemapuseMap

CSS style strings are automatically parsed into React style objects (e.g., "font-size: 16px" becomes { fontSize: "16px" }).

Exports Summary

ExportKindDescription
compileToReactfunctionCompile Markdown/MDX to React elements
hastToReactfunctionConvert HAST to React elements
CompileToReactOptionstypeOptions for compileToReact
CompileToReactResulttypeResult of compileToReact
HastToReactOptionstypeOptions for hastToReact
CreateElementtypeReact createElement signature
ComponentMaptypeHTML tag to component map