{
  "url": "https://unifast.dev/it/docs/packages/react/compile-to-react/",
  "locale": "it",
  "title": "compileToReact()",
  "description": "Compila input Markdown o MDX e restituisce direttamente elementi React, insieme a frontmatter, diagnostica, statistiche e TOC.",
  "section": "packages",
  "body": "```ts\n\n```\n\n## Firma\n\n```ts\nfunction compileToReact(\n  input: string,\n  options: CompileToReactOptions,\n): CompileToReactResult\n```\n\n## Parametri\n\n### input\n\n| Proprietà | Tipo | Predefinito | Descrizione |\n|-----------|------|-------------|-------------|\n| `input` | `string` | — | Sorgente Markdown o MDX |\n\n### options\n\nOpzioni di compilazione più configurazione specifica per React\n\n| Proprietà | Tipo | Predefinito | Descrizione |\n|-----------|------|-------------|-------------|\n| `createElement` | `CreateElement` | — | Funzione createElement di React |\n| `Fragment` | `unknown` | — | Componente Fragment di React |\n| `components?` | `ComponentMap` | — | Mappa dei nomi dei tag HTML verso componenti React |\n\n> Estende [`CompileOptions`](/docs/packages/core/overview): sono accettate anche tutte le altre opzioni di compilazione.\n\n## Valore restituito\n\n| Proprietà | Tipo | Descrizione |\n|-----------|------|-------------|\n| `element` | `unknown` | L'elemento React radice |\n| `frontmatter` | `Record<string, unknown>` | Metadati del frontmatter analizzati |\n| `diagnostics` | `Diagnostic[]` | Avvisi ed errori |\n| `stats` | `{ parseMs, transformMs, emitMs }` | Dettaglio dei tempi (ms) |\n| `toc` | `TocEntry[]` | Tabella dei contenuti estratta |\n\n## Utilizzo\n\n```tsx\n\nconst { element, frontmatter: fm, toc: tocEntries, diagnostics, stats } = compileToReact(\n  md,\n  {\n    createElement,\n    Fragment,\n    components: {\n      h1: ({ children, ...props }) => <h1 className=\"title\" {...props}>{children}</h1>,\n      a: ({ children, ...props }) => <a target=\"_blank\" {...props}>{children}</a>,\n    },\n    plugins: [gfm(), frontmatter(), toc()],\n  },\n);\n```\n\n## Esempi\n\n### Utilizzo di base\n\n```tsx\n\nconst { element } = compileToReact(\n  \"# Hello, **world**!\",\n  { createElement, Fragment },\n);\n\nfunction Page() {\n  return <div>{element}</div>;\n}\n```\n\n### Con componenti personalizzati\n\n```tsx\n\nconst { element } = compileToReact(md, {\n  createElement,\n  Fragment,\n  components: {\n    h1: ({ children, ...props }) => (\n      <h1 className=\"text-4xl font-bold\" {...props}>{children}</h1>\n    ),\n    a: ({ children, ...props }) => (\n      <a className=\"text-blue-500 underline\" target=\"_blank\" {...props}>{children}</a>\n    ),\n    code: ({ children, ...props }) => (\n      <code className=\"bg-gray-100 rounded px-1\" {...props}>{children}</code>\n    ),\n  },\n});\n```\n\n### Rendering lato server\n\n```tsx\n\nconst { element } = compileToReact(md, { createElement, Fragment });\nconst html = renderToString(element);\n\nconsole.log(html);\n// Rendered HTML string for SSR\n```\n\n### Utilizzo di frontmatter e TOC\n\n```tsx\n\nconst md = `---\ntitle: My Page\n---\n\n# Introduction\n\n## Setup\n\n## Usage\n`;\n\nconst result = compileToReact(md, {\n  createElement,\n  Fragment,\n  plugins: [frontmatter(), toc()],\n});\n\nconsole.log(result.frontmatter);\n// { title: \"My Page\" }\n\nconsole.log(result.toc);\n// [\n//   { depth: 1, text: \"Introduction\", slug: \"introduction\" },\n//   { depth: 2, text: \"Setup\", slug: \"setup\" },\n//   { depth: 2, text: \"Usage\", slug: \"usage\" },\n// ]\n```",
  "alternates": [
    {
      "locale": "en",
      "url": "https://unifast.dev/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "ja",
      "url": "https://unifast.dev/ja/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/ja/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "zh-CN",
      "url": "https://unifast.dev/zh-CN/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/zh-CN/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "zh-TW",
      "url": "https://unifast.dev/zh-TW/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/zh-TW/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "ko",
      "url": "https://unifast.dev/ko/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/ko/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "fr",
      "url": "https://unifast.dev/fr/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/fr/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "it",
      "url": "https://unifast.dev/it/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/it/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "es",
      "url": "https://unifast.dev/es/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/es/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "pt-BR",
      "url": "https://unifast.dev/pt-BR/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/pt-BR/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "de",
      "url": "https://unifast.dev/de/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/de/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "ru",
      "url": "https://unifast.dev/ru/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/ru/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "hi",
      "url": "https://unifast.dev/hi/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/hi/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "id",
      "url": "https://unifast.dev/id/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/id/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "tr",
      "url": "https://unifast.dev/tr/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/tr/docs/packages/react/compile-to-react.json"
    },
    {
      "locale": "vi",
      "url": "https://unifast.dev/vi/docs/packages/react/compile-to-react/",
      "api": "https://unifast.dev//api/vi/docs/packages/react/compile-to-react.json"
    }
  ]
}
