hastToReact()
एक HAST root node को React elements में convert करें। low-level function जब आपको compilation pipeline पर पूर्ण नियंत्रण चाहिए।
import { hastToReact } from "@unifast/react";Signature
function hastToReact(
hast: HastRoot,
options: HastToReactOptions,
): unknownParameters
hast
| Property | Type | Default | विवरण |
|---|---|---|---|
type | "root" | — | Node type identifier |
children | HastNode[] | — | tree के Child nodes |
options
React element creation config
| Property | Type | Default | विवरण |
|---|---|---|---|
createElement | CreateElement | — | React का createElement function |
Fragment | unknown | — | React का Fragment component |
components? | ComponentMap | — | HTML tag names का custom React components पर map |
उपयोग
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>,
},
});उदाहरण
मूल उपयोग
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-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);
// SSR के लिए Rendered HTML stringव्यवहार
Property renaming: HTML attributes का नाम React समकक्षों में बदल दिया जाता है (
classसेclassName,forसेhtmlFor, आदि)Style parsing: CSS style strings को React style objects में parse किया जाता है
className arrays: HAST
classNamearrays को spaces के साथ join किया जाता हैBoolean attributes:
trueattribute को render करता है,false/null/undefinedइसे omit कर देते हैंRaw nodes: inline HTML के रूप में Rendered — अविश्वसनीय input process करते समय
sanitizeplugin (@unifast/nodeसे) का उपयोग करेंComments और doctype: Ignore कर दिए जाते हैं (
nullreturn)