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,
): unknown

Parameters

hast

PropertyTypeDefaultDescription
type"root"Node type identifier
childrenHastNode[]Child nodes of the tree

options

React element creation config

PropertyTypeDefaultDescription
createElementCreateElementReact’s createElement function
FragmentunknownReact’s Fragment component
components?ComponentMapMap 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 SSR

Behavior

  • Property renaming: HTML attributes are renamed to React equivalents (class to className, for to htmlFor, etc.)

  • Style parsing: CSS style strings are parsed into React style objects

  • className arrays: HAST className arrays are joined with spaces

  • Boolean attributes: true renders the attribute, false/null/undefined omits it

  • Raw nodes: Rendered as inline HTML - use the sanitize plugin (from @unifast/node) when processing untrusted input

  • Comments and doctype: Ignored (return null)