hastToReact()
Convert a HAST root node into React elements. The low-level function for when you need full control over the compilation pipeline.
import { hastToReact } from "@unifast/react";Signature
function hastToReact(
hast: HastRoot,
options: HastToReactOptions,
): unknownParameters
hast
| Property | Type | Default | Description |
|---|---|---|---|
type | "root" | — | Node type identifier |
children | HastNode[] | — | Child nodes of the tree |
options
React element creation config
| Property | Type | Default | Description |
|---|---|---|---|
createElement | CreateElement | — | React’s createElement function |
Fragment | unknown | — | React’s Fragment component |
components? | ComponentMap | — | Map of HTML tag names to custom React components |
Usage
import { createElement, Fragment } from "react";
import { compile } from "@unifast/node";
import { hastToReact } from "@unifast/react";
import type { HastRoot } from "@unifast/core";
const result = compile(md, { outputKind: "hast" });
const hast: HastRoot = JSON.parse(result.output as string);
const element = hastToReact(hast, {
createElement,
Fragment,
components: {
pre: ({ children, ...props }) => <pre className="code-block" {...props}>{children}</pre>,
a: ({ children, ...props }) => <a target="_blank" rel="noopener" {...props}>{children}</a>,
},
});Examples
Basic usage
import { createElement, Fragment } from "react";
import { compile } from "@unifast/node";
import { hastToReact } from "@unifast/react";
import type { HastRoot } from "@unifast/core";
const result = compile("# Hello, **world**!", { outputKind: "hast" });
const hast: HastRoot = JSON.parse(result.output as string);
const element = hastToReact(hast, { createElement, Fragment });
function Page() {
return <div>{element}</div>;
}With Shiki-highlighted HAST
import { createElement, Fragment } from "react";
import { compile } from "@unifast/node";
import { createShikiTransformer } from "@unifast/shiki";
import { hastToReact } from "@unifast/react";
import type { HastRoot } from "@unifast/core";
const transformer = await createShikiTransformer({
themes: ["github-dark"],
langs: ["typescript"],
});
const result = compile(md, { outputKind: "hast" });
const hast: HastRoot = JSON.parse(result.output as string);
const highlighted = transformer.transform(hast);
const element = hastToReact(highlighted, { createElement, Fragment });Server-side rendering
import { createElement, Fragment } from "react";
import { renderToString } from "react-dom/server";
import { compile } from "@unifast/node";
import { hastToReact } from "@unifast/react";
import type { HastRoot } from "@unifast/core";
const result = compile(md, { outputKind: "hast" });
const hast: HastRoot = JSON.parse(result.output as string);
const element = hastToReact(hast, { createElement, Fragment });
const html = renderToString(element);
console.log(html);
// Rendered HTML string for SSRBehavior
Property renaming: HTML attributes are renamed to React equivalents (
classtoclassName,fortohtmlFor, etc.)Style parsing: CSS style strings are parsed into React style objects
className arrays: HAST
classNamearrays are joined with spacesBoolean attributes:
truerenders the attribute,false/null/undefinedomits itRaw nodes: Rendered as inline HTML - use the
sanitizeplugin (from@unifast/node) when processing untrusted inputComments and doctype: Ignored (return
null)