hastToReact()

Mengonversi node root HAST menjadi elemen React. Fungsi tingkat rendah untuk saat Anda membutuhkan kontrol penuh atas pipeline kompilasi.

import { hastToReact } from "@unifast/react";

Signature

function hastToReact(
  hast: HastRoot,
  options: HastToReactOptions,
): unknown

Parameter

hast

PropertiTipeDefaultDeskripsi
type"root"Identifier tipe node
childrenHastNode[]Node anak dari pohon

options

Konfigurasi pembuatan elemen React

PropertiTipeDefaultDeskripsi
createElementCreateElementFungsi createElement dari React
FragmentunknownKomponen Fragment dari React
components?ComponentMapPemetaan dari nama tag HTML ke komponen React kustom

Penggunaan

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>,
  },
});

Contoh

Penggunaan dasar

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>;
}

Dengan HAST yang sudah di-highlight Shiki

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

Perilaku

  • Penggantian nama properti: Atribut HTML diganti namanya menjadi setara React (class menjadi className, for menjadi htmlFor, dll.)

  • Parsing style: String style CSS di-parse menjadi objek style React

  • Array className: Array className HAST digabungkan dengan spasi

  • Atribut boolean: true merender atribut, false/null/undefined menghilangkannya

  • Node mentah: Dirender sebagai HTML inline - gunakan plugin sanitize (dari @unifast/node) saat memproses input yang tidak terpercaya

  • Komentar dan doctype: Diabaikan (mengembalikan null)