compileToReact()

Markdown या MDX input को compile करें और frontmatter, diagnostics, stats, और TOC के साथ सीधे React elements return करें।

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

Signature

function compileToReact(
  input: string,
  options: CompileToReactOptions,
): CompileToReactResult

Parameters

input

PropertyTypeDefaultविवरण
inputstringMarkdown या MDX source

options

Compile options plus React-specific config

PropertyTypeDefaultविवरण
createElementCreateElementReact का createElement function
FragmentunknownReact का Fragment component
components?ComponentMapHTML tag names का React components पर map

CompileOptions को extend करता है — अन्य सभी compile options भी स्वीकार किए जाते हैं।

Returns

PropertyTypeविवरण
elementunknownroot React element
frontmatterRecord<string, unknown>Parsed frontmatter metadata
diagnosticsDiagnostic[]Warnings और errors
stats{ parseMs, transformMs, emitMs }Timing breakdown (ms)
tocTocEntry[]निकाला गया table of contents

उपयोग

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

उदाहरण

मूल उपयोग

import { createElement, Fragment } from "react";
import { compileToReact } from "@unifast/react";

const { element } = compileToReact(
  "# Hello, **world**!",
  { createElement, Fragment },
);

function Page() {
  return <div>{element}</div>;
}

custom components के साथ

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);
// SSR के लिए Rendered HTML string

frontmatter और 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" },
// ]