Skip to content

Markdown

A reply's Markdown, rendered safely as it streams in.

To point www.example.com at your site, add a CNAME record at your DNS provider:

Then check that it has propagated:

bash

A few things to keep in mind:

  • Remove any A or AAAA records for www first, since a name can't have both.
  • Verification usually takes a few minutes, but some providers cache records for up to an hour.
  • See Custom domains for the apex domain.

Installation

  1. Add the registryOnce per project
  2. Add markdown

Usage

Markdown renders GitHub Flavored Markdown with TanStack Markdown (opens in a new tab), styled by Prose and sized for a reply: headings stay close to the text, long URLs and code wrap rather than widening the reply, and tables scroll horizontally. Code blocks render as a Code Block, labeled with their title, as in ```ts title="app.ts", or else their language. TanStack Highlight (opens in a new tab) highlights them in the theme's syntax colors, and lineNumbers adds line numbers. Links render as a Link, bare URLs included. Links to other sites open in a new tab with an arrow and rel="nofollow". Task lists render a read-only Checkbox, which screen readers announce with the item's text.

Markdown renders on the server as well as in the browser. In a project with React Server Components, such as a Next.js app, rendering Markdown from a server component parses and highlights it on the server. The browser receives only the parts that need it: each code block's copy and wrap buttons, a table's scroll area and a task list's checkboxes. In a server component, options can be functions, such as a urlTransform or your own components. For a reply that streams in, render Markdown in a client component instead, where it parses the text as it arrives.

A model's reply can be steered by content its tools read, so Markdown renders only what Markdown syntax produces. Raw HTML shows as text, links that would run script are dropped, and images show as their alt text, since loading an image can send the conversation's contents to any site.

TanStack Markdown's options pass through: extensions for more syntax, urlTransform to rewrite or drop URLs after unsafe ones are removed, and headingIds, headingAnchors and frontmatter, which are off by default for replies. allowHtml renders raw HTML, so use it only for Markdown you wrote yourself.

Each Markdown scopes the ids of its footnotes and headings, so several replies on one page don't link to each other's. A reply's # heading is an h1. On a page that has its own h1, pass headingOffset to move the reply's headings below it: with 1, # renders as an h2 that looks the same.

Examples

Streaming

While a reply streams in, set streaming and pass all of the text received so far on each update. Markdown shows only content that can already render in its final form. A code block appears once its opening line, with the language, has arrived. A table appears once its header and the row of dashes under it have arrived, and a heading or list item once it has some text. In the block still arriving, Markdown closes bold, italics, strikethrough and code as they're typed, and shows a link as plain text until its URL is complete. A caret follows the end of the text, or sits below a code block that's still arriving.

While streaming is set, Markdown sets aria-busy, so screen readers wait for the finished reply. It also parses only the end of the reply on each update. Paragraphs, lists and other blocks that a new line can no longer change keep the result of their first parse.

More languages

The default highlighter supports the languages models write most: CSS, diffs, Go, HTML, JavaScript, JSON, JSX, Markdown, Python, Rust, shell, SQL, TypeScript, TSX and YAML. Code in any other language stays plain unless you pass your own highlighter, made with TanStack Highlight's createHighlighter, to highlighter. Yours replaces the default, so list every language you want, as in createHighlighter({ languages: [ts, tsx, kotlin, ruby] }). Pass false to leave all code plain.

Here's the same check in Kotlin:

kotlin

And in Ruby:

ruby

Replacing elements

components replaces the elements Markdown renders, by tag name, with your own components or other tags. The example below renders tables with a Table. Your components take precedence over Markdown's own code blocks, links and images, so { img: "img" } shows images again for Markdown you trust. Define the object outside your component, as in the example, so Markdown doesn't re-render every reply whenever your component renders.

A link component you pass still receives Markdown's scoped footnote and heading ids, and target and rel for links to other sites. To render only some links your own way, fall back to MarkdownLink for the rest, as Citation does.

These are the records example.com needs:

Props

MarkdownLink is the link Markdown renders by default, a Link that adds nofollow to the rel of links to other sites. Your own link component can fall back to it.

Markdown