温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/bun/markdown
点击访问原文链接

Bun.markdown object | API Reference | Bun

Bun.markdown object | API Reference | BunBuildDocsReferenceGuidesBlogDiscord/Bun/markdownFansiFhtmlFreactFrender

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/Bun/markdownFansiFhtmlFreactFrender

namespace

markdownnamespace markdown

Markdown related APIs.

Provides fast markdown parsing and rendering with three output modes:

html() — render to an HTML stringrender() — render with custom callbacks for each elementreact() — parse to React-compatible JSX elements

Supports GFM extensions (tables, strikethrough, task lists, autolinks) and component overrides to replace default HTML tags with custom components.

// Render markdown to HTML
const html = Bun.markdown.html("# Hello **world**");
// "Hello world\n"

// Render with custom callbacks
const ansi = Bun.markdown.render("# Hello **world**", {
heading: (children, { level }) => `\x1b[1m${children}\x1b[0m\n`,
strong: (children) => `\x1b[1m${children}\x1b[22m`,
paragraph: (children) => children + "\n",
});

// Render as a React component
function Markdown({ text }: { text: string }) {
return Bun.markdown.react(text);
}

// With component overrides
const element = Bun.markdown.react("# Hello", { h1: MyHeadingComponent });
interface AnsiTheme

Theme for ANSI terminal rendering.

colors?: boolean

Emit ANSI color + styling escape sequences. When false, the renderer falls back to plain ASCII chrome (no box drawing, no emoji, no escape codes).

columns?: number

Line width used for word-wrapping paragraphs and headings and for the horizontal rule. Pass 0 to disable wrapping.

hyperlinks?: boolean

Emit OSC 8 hyperlinks (clickable links in modern terminals). When false, links render as text (url).

kittyGraphics?: boolean

Inline images using the Kitty Graphics Protocol when the src resolves to a local file on disk. Falls through to the text alt for remote URLs. Supported by Kitty, WezTerm, and Ghostty.

light?: boolean

True when the terminal background is light. Affects the color palette chosen for inline code backgrounds. Defaults to detecting from the COLORFGBG environment variable.

interface CellMeta

Meta passed to th and td callbacks.

align?: 'left' | 'center' | 'right'

Column alignment.

interface CellPropsalign?: 'left' | 'center' | 'right'

Column alignment.

children: unknown[]interface ChildrenPropschildren: unknown[]interface CodeBlockMeta

Meta passed to the code callback.

language?: string

The info-string language (e.g. "js").

interface CodeBlockPropschildren: unknown[]language?: string

The info-string language (e.g. "js").

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 });
a?: ComponentLinkProps>blockquote?: ComponentChildrenProps>br?: Component{}>code?: ComponentChildrenProps>del?: ComponentChildrenProps>em?: ComponentChildrenProps>h1?: ComponentHeadingProps>h2?: ComponentHeadingProps>h3?: ComponentHeadingProps>h4?: ComponentHeadingProps>h5?: ComponentHeadingProps>h6?: ComponentHeadingProps>hr?: Component{}>html?: ComponentChildrenProps>img?: ComponentImageProps>li?: ComponentListItemProps>math?: ComponentChildrenProps>ol?: ComponentOrderedListProps>p?: ComponentChildrenProps>pre?: ComponentCodeBlockProps>strong?: ComponentChildrenProps>table?: ComponentChildrenProps>tbody?: ComponentChildrenProps>td?: ComponentCellProps>th?: ComponentCellProps>thead?: ComponentChildrenProps>tr?: ComponentChildrenProps>u?: ComponentChildrenProps>ul?: ComponentChildrenProps>interface HeadingMeta

Meta passed to the heading callback.

id?: string

Heading ID slug. Set when headings: { ids: true } is enabled.

level: number

Heading level (1–6).

interface HeadingPropschildren: unknown[]id?: string

Heading ID slug. Set when headings: { ids: true } is enabled.

interface ImageMeta

Meta passed to the image callback.

src: string

Image URL.

title?: string

Image title attribute.

interface ImagePropsalt?: string

Alt text.

src: string

Image URL.

title?: string

Image title attribute.

interface LinkMeta

Meta passed to the link callback.

href: string

Link URL.

title?: string

Link title attribute.

interface LinkPropschildren: unknown[]href: string

Link URL.

title?: string

Link title attribute.

interface ListItemMeta

Meta passed to the listItem callback.

checked?: boolean

Task list checked state. Set for - [x] / - [ ] items.

depth: number

Nesting depth of the parent list. 0 for items in a top-level list.

index: number

0-based index of this item within its parent list.

ordered: boolean

Whether the parent list is ordered.

start?: number

The start number of the parent list (only set when ordered is true).

interface ListItemPropschecked?: boolean

Task list checked state. Set for - [x] / - [ ] items.

children: unknown[]interface ListMeta

Meta passed to the list callback.

depth: number

Nesting depth. 0 for a top-level list, 1 for a list inside a list item, etc.

ordered: boolean

Whether this is an ordered list.

start?: number

The start number for ordered lists.

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 true to 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 } }
collapseWhitespace?: boolean

Collapse whitespace in text content. Default: false.

hardSoftBreaks?: boolean

Treat soft line breaks as hard line breaks. Default: false.

headings?: boolean | { autolink: boolean; ids: boolean }

Configure heading IDs and autolink headings. Pass true to 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 } }
latexMath?: boolean

Enable LaTeX math ($inline$ and $$display$$). Default: false.

noHtmlBlocks?: boolean

Disable HTML blocks. Default: false.

noHtmlSpans?: boolean

Disable inline HTML spans. Default: false.

noIndentedCodeBlocks?: boolean

Disable indented code blocks. Default: false.

permissiveAtxHeaders?: boolean

Allow ATX headers without a space after #. Default: false.

strikethrough?: boolean

Enable GFM strikethrough (~~text~~). Default: true.

tables?: boolean

Enable GFM tables. Default: true.

collapseWhitespace?: boolean

Collapse whitespace in text content. Default: false.

hardSoftBreaks?: boolean

Treat soft line breaks as hard line breaks. Default: false.

headings?: boolean | { autolink: boolean; ids: boolean }

Configure heading IDs and autolink headings. Pass true to 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 } }
latexMath?: boolean

Enable LaTeX math ($inline$ and $$display$$). Default: false.

noHtmlBlocks?: boolean

Disable HTML blocks. Default: false.

noHtmlSpans?: boolean

Disable inline HTML spans. Default: false.

noIndentedCodeBlocks?: boolean

Disable indented code blocks. Default: false.

permissiveAtxHeaders?: boolean

Allow ATX headers without a space after #. Default: false.

reactVersion?: 18 | 19

Which $$typeof symbol 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
strikethrough?: boolean

Enable GFM strikethrough (~~text~~). Default: true.

tables?: boolean

Enable GFM tables. Default: true.

function html(input: string | ArrayBufferLike | TypedArrayArrayBufferLike> | DataViewArrayBuffer>,options?: Options): string;

Render markdown to an HTML string.

@param input

The markdown string or buffer to render

@param options

Parser options

@returns

An HTML string

const html = Bun.markdown.html("# Hello **world**");
// "Hello world\n"

// With options
const html = Bun.markdown.html("## Hello", { headings: { ids: true } });
// 'Hello\n'
function react(input: string | ArrayBufferLike | TypedArrayArrayBufferLike> | DataViewArrayBuffer>,components?: ComponentOverrides,options?: ReactOptions): unknown;

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.

@param input

The markdown string or buffer to parse

@param components

Component overrides keyed by HTML tag name

@param options

Parser options and element symbol configuration

@returns

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 });
function render(input: string | ArrayBufferLike | TypedArrayArrayBufferLike> | DataViewArrayBuffer>,callbacks?: RenderCallbacks,options?: Options): string;

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.

@param input

The markdown string to render

@param callbacks

Callbacks for each element type

@param options

Parser options

@returns

The accumulated string output

// Custom HTML with classes
const html = Bun.markdown.render("# Title\n\nHello **world**", {
heading: (children, { level }) => `${level} class="title">${children}${level}>`,
paragraph: (children) => `

${children}

`, strong: (children) => `${children}`, }); // 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 });

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

Bun.markdown object | API Reference | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    Markdown related APIs. Provides fast markdown parsing and rendering with three output modes: - `html()` — render to an HTML string - `render()` — render with custom callbacks for each element - `react()` — parse to React-compatible JSX elements Supports GFM extensions (tables, strikethrough, task lists, autolinks) and component overrides to replace default HTML tags with custom components.