function
markdown.render
Render markdown with custom JavaScript callbacks for each element.
Each callback receives the accumulated children as a string and optional metadata, and returns a string. Return null or undefined to omit an element. If no callback is registered, children pass through unchanged.
Parser options are passed as a separate third argument.
The markdown string to render
Callbacks for each element type
Parser options
The accumulated string output
// Custom HTML with classes
const html = Bun.markdown.render("# Title\n\nHello **world**", {
heading: (children, { level }) => `<h${level} class="title">${children}</h${level}>`,
paragraph: (children) => `<p>${children}</p>`,
strong: (children) => `<b>${children}</b>`,
});
// ANSI terminal output
const ansi = Bun.markdown.render("# Hello\n\n**bold**", {
heading: (children) => `\x1b[1;4m${children}\x1b[0m\n`,
paragraph: (children) => children + "\n",
strong: (children) => `\x1b[1m${children}\x1b[22m`,
});
// With parser options as third argument
const text = Bun.markdown.render("Visit www.example.com", {
link: (children, { href }) => `[${children}](${href})`,
paragraph: (children) => children,
}, { autolinks: true });Referenced types
interface RenderCallbacks
Callbacks for render(). Each callback receives the accumulated children as a string and optional metadata, and returns a string.
Return null or undefined to omit the element from the output. If no callback is registered for an element, its children pass through unchanged.
- code?: (children: string, meta?: CodeBlockMeta) => undefined | null | string
Code block.
meta.languageis the info-string (e.g."js"). Only passed for fenced code blocks with a language. - heading?: (children: string, meta: HeadingMeta) => undefined | null | string
Heading (level 1–6).
idis set whenheadings: { ids: true }is enabled. - listItem?: (children: string, meta: ListItemMeta) => undefined | null | string
List item.
metaalways includes{index, depth, ordered}.meta.startis set for ordered lists;meta.checkedis set for task list items.
interface Options
Options for configuring the markdown parser.
By default, GFM extensions (tables, strikethrough, task lists) are enabled.
- 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 } } - 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.