hastToReact()
Bir HAST kök düğümünü React elemanlarına dönüştürür. Derleme ardışık düzeni üzerinde tam kontrole ihtiyacınız olduğunda kullanacağınız düşük seviyeli fonksiyon.
import { hastToReact } from "@unifast/react";İmza
function hastToReact(
hast: HastRoot,
options: HastToReactOptions,
): unknownParametreler
hast
| Özellik | Tür | Varsayılan | Açıklama |
|---|---|---|---|
type | "root" | — | Düğüm türü tanımlayıcısı |
children | HastNode[] | — | Ağacın alt düğümleri |
options
React eleman oluşturma yapılandırması
| Özellik | Tür | Varsayılan | Açıklama |
|---|---|---|---|
createElement | CreateElement | — | React’in createElement fonksiyonu |
Fragment | unknown | — | React’in Fragment bileşeni |
components? | ComponentMap | — | HTML etiket adlarının özel React bileşenlerine eşlemesi |
Kullanım
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>,
},
});Örnekler
Temel kullanım
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>;
}Shiki ile vurgulanmış 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 });Sunucu tarafı render
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);
// SSR için render edilmiş HTML string'iDavranış
Özellik yeniden adlandırma: HTML öznitelikleri React karşılıklarına yeniden adlandırılır (
class→className,for→htmlFor, vb.)Stil ayrıştırma: CSS stil string’leri React stil nesnelerine ayrıştırılır
className dizileri: HAST
classNamedizileri boşluklarla birleştirilirBoolean öznitelikler:
trueözniteliği render eder,false/null/undefinedonu atlarRaw düğümler: Satır içi HTML olarak render edilir - güvenilmeyen girdileri işlerken
sanitizeplugin’ini (@unifast/node‘dan) kullanınYorumlar ve doctype: Yok sayılır (
nulldöndürür)