Skip to Content
Fumadocs

Markdown Rendering

Render React elements into Markdown.

Overview

fumadocs-core/server renders React Server Components, into a Markdown document:

import { renderToMarkdown } from 'fumadocs-core/server';

const text = await renderToMarkdown(
  <div>
    <h2>Hello World</h2>
    <p>
      Some <b>rich</b> content.
    </p>
  </div>,
);

It powers the component exported by Remark LLMs, and works on any server-rendered content you want to serve as Markdown.

renderToMarkdown() is server-only.

asMarkdown

Components define their own Markdown form. Calling asMarkdown() during render is the opt-in: it returns the target when the component is being rendered into Markdown, and the HTML elements it returns are converted.

import { asMarkdown, md } from 'fumadocs-core/server';

function Callout({ title, children }) {
  if (asMarkdown()) return md.linePrefix('> ')`**${title}**\n${children}`;

  return <div className="...">...</div>;
}

A component that never calls it is kept as JSX syntax with its props, like:

<Tabs items={['a', 'b']}>...</Tabs>

This includes client components, which don't execute on the server.

Targets

A tree renders for a target, the target option of renderToMarkdown():

  • markdown (default): the document for agents.
  • search: the text of search records. It has the text of links, no images or code blocks, and a component without a Markdown form is an HTML tag with its text props.

These rules apply to the elements a component returns, like <a href>. A link written as text in an md template keeps its target in search, so return the element, or check the target.

asMarkdown() returns the target, so a component can give agents more than search indexes:

import { asMarkdown, md } from 'fumadocs-core/server';

function Version({ name, released }) {
  const target = asMarkdown();
  if (target === 'search') return name;
  if (target) return md`${name} (released ${released})`;

  return <span title={released}>{name}</span>;
}

md

Tagged template for Markdown output, interpolated values can be strings, React nodes, promises, or arrays of them. Use it to stringify children before manipulating them.

Two variants return the content as a block:

  • md.linePrefix(prefix): prefix every line, e.g. '> ' for quote blocks.
  • md.indent(size): indent every line.

renderRoute

Render a page element into Markdown, or undefined when its component doesn't call asMarkdown().

import { renderRoute } from 'fumadocs-core/server';

const text = await renderRoute(<Page params={params} />);

Useful for serving a Markdown variant of a route: pages that opt in produce Markdown, other pages return undefined so you can fall back to a 404 or another source.

How is this guide?