function
markdown.react
Render markdown to React JSX elements.
Returns a React Fragment containing the parsed markdown as children. Can be returned directly from a component or passed to renderToString().
Override any HTML element with a custom component by passing it in the second argument, keyed by tag name. Custom components receive the same props the default elements would (e.g. href for links, language for code blocks).
Parser options (including reactVersion) are passed as a separate third argument. Uses Symbol.for('react.transitional.element') by default (React 19). Pass reactVersion: 18 for React 18 and older.
The markdown string or buffer to parse
Component overrides keyed by HTML tag name
Parser options and element symbol configuration
A React Fragment element containing the parsed markdown
// Use directly as a component return value
function Markdown({ text }: { text: string }) {
return Bun.markdown.react(text);
}
// Server-side rendering
import { renderToString } from "react-dom/server";
const html = renderToString(Bun.markdown.react("# Hello **world**"));
// Custom components receive element props
function Code({ language, children }: { language?: string; children: React.ReactNode }) {
return <pre data-language={language}><code>{children}</code></pre>;
}
function Link({ href, children }: { href: string; children: React.ReactNode }) {
return <a href={href} target="_blank">{children}</a>;
}
const el = Bun.markdown.react(text, { pre: Code, a: Link });
// For React 18 and older
const el18 = Bun.markdown.react(text, undefined, { reactVersion: 18 });Referenced types
interface ComponentOverrides
Component overrides for react().
Replace default HTML tags with custom React components. Each override receives the same props the default element would get.
function Code({ language, children }: { language?: string; children: React.ReactNode }) {
return <pre data-language={language}><code>{children}</code></pre>;
}
Bun.markdown.react(text, { pre: Code });interface ReactOptions
Options for react() — parser options and element symbol configuration.
- autolinks?: boolean | { email: boolean; url: boolean; www: boolean }
Enable autolinks. Pass
trueto enable all autolink types (URL, WWW, email), or an object to enable individually.// Enable all autolinks { autolinks: true } // Enable only URL and email autolinks { autolinks: { url: true, email: true } } - headings?: boolean | { autolink: boolean; ids: boolean }
Configure heading IDs and autolink headings. Pass
trueto enable both heading IDs and autolink headings, or an object to configure individually.// Enable both heading IDs and autolink headings { headings: true } // Enable only heading IDs { headings: { ids: true } } - reactVersion?: 18 | 19
Which
$$typeofsymbol to use on the generated elements.19(default):Symbol.for('react.transitional.element')18:Symbol.for('react.element')— use this for React 18 and older
- tagFilter?: boolean
Enable the GFM tag filter, which replaces
<with<for disallowed HTML tags (e.g.<script>,<style>,<iframe>). Default:false. - underline?: boolean
Enable underline syntax (
__text__renders as<u>instead of<strong>). Default:false.