compileToReact()
Compila entrada Markdown ou MDX e retorna elementos React diretamente, junto com frontmatter, diagnósticos, stats e TOC.
import { compileToReact } from "@unifast/react";Assinatura
function compileToReact(
input: string,
options: CompileToReactOptions,
): CompileToReactResultParâmetros
input
| Propriedade | Tipo | Padrão | Descrição |
|---|---|---|---|
input | string | — | Fonte Markdown ou MDX |
options
Opções de compilação mais a configuração específica do React
| Propriedade | Tipo | Padrão | Descrição |
|---|---|---|---|
createElement | CreateElement | — | A função createElement do React |
Fragment | unknown | — | O componente Fragment do React |
components? | ComponentMap | — | Mapa de nomes de tags HTML para componentes React |
Estende
CompileOptions— todas as outras opções de compilação também são aceitas.
Retorna
| Propriedade | Tipo | Descrição |
|---|---|---|
element | unknown | O elemento React raiz |
frontmatter | Record<string, unknown> | Metadados de frontmatter parseados |
diagnostics | Diagnostic[] | Avisos e erros |
stats | { parseMs, transformMs, emitMs } | Detalhamento de tempo (ms) |
toc | TocEntry[] | Table of contents extraída |
Uso
import { createElement, Fragment } from "react";
import { compileToReact } from "@unifast/react";
import { gfm, frontmatter, toc } from "@unifast/node";
const { element, frontmatter: fm, toc: tocEntries, diagnostics, stats } = compileToReact(
md,
{
createElement,
Fragment,
components: {
h1: ({ children, ...props }) => <h1 className="title" {...props}>{children}</h1>,
a: ({ children, ...props }) => <a target="_blank" {...props}>{children}</a>,
},
plugins: [gfm(), frontmatter(), toc()],
},
);Exemplos
Uso básico
import { createElement, Fragment } from "react";
import { compileToReact } from "@unifast/react";
const { element } = compileToReact(
"# Hello, **world**!",
{ createElement, Fragment },
);
function Page() {
return <div>{element}</div>;
}Com componentes customizados
import { createElement, Fragment } from "react";
import { compileToReact } from "@unifast/react";
const { element } = compileToReact(md, {
createElement,
Fragment,
components: {
h1: ({ children, ...props }) => (
<h1 className="text-4xl font-bold" {...props}>{children}</h1>
),
a: ({ children, ...props }) => (
<a className="text-blue-500 underline" target="_blank" {...props}>{children}</a>
),
code: ({ children, ...props }) => (
<code className="bg-gray-100 rounded px-1" {...props}>{children}</code>
),
},
});Server-side rendering
import { createElement, Fragment } from "react";
import { renderToString } from "react-dom/server";
import { compileToReact } from "@unifast/react";
const { element } = compileToReact(md, { createElement, Fragment });
const html = renderToString(element);
console.log(html);
// Rendered HTML string for SSRUsando frontmatter e TOC
import { createElement, Fragment } from "react";
import { compileToReact } from "@unifast/react";
import { frontmatter, toc } from "@unifast/node";
const md = `---
title: My Page
---
# Introduction
## Setup
## Usage
`;
const result = compileToReact(md, {
createElement,
Fragment,
plugins: [frontmatter(), toc()],
});
console.log(result.frontmatter);
// { title: "My Page" }
console.log(result.toc);
// [
// { depth: 1, text: "Introduction", slug: "introduction" },
// { depth: 2, text: "Setup", slug: "setup" },
// { depth: 2, text: "Usage", slug: "usage" },
// ]