Skip to Content
Fumadocs

Stringification

How Fumadocs turns your documents into Markdown.

Remark LLMs and Remark Structure output Markdown, for LLMs and search. Instead of stringifying the syntax tree, they slice your authored source:

  • Markdown stays as you wrote it, nothing is escaped or reformatted.
  • Remark plugins change it only by recording edits.
  • It's fast, since no syntax tree is serialized.

How It Works

The output is the source of your document, with the edits of plugins applied. Nodes are located by their position:

  • A node with a position outputs its source.
  • A node without one, like those inserted by plugins, outputs nothing. Its plugin defines its Markdown instead.

For example, Remark Image turns ![alt](/image.png) into an <img> element, and the output keeps ![alt](/image.png).

Editing the Output

A plugin replacing authored content records the Markdown of its node with replaceSource():

remark-note.ts
import type { Root } from 'mdast';
import type { VFile } from 'vfile';
import { visit } from 'unist-util-visit';
import { replaceSource } from 'fumadocs-core/mdx-plugins/stringifier';

export function remarkNote() {
  return (tree: Root, file: VFile) => {
    visit(tree, 'mdxJsxFlowElement', (node) => {
      if (node.name !== 'Note') return;

      replaceSource(file, node, (s) => `**Note:**\n\n${s.inner(node)}`);
      // transform the node for rendering...
    });
  };
}
  • A string replaces the node, along with the edits inside it.
  • A function runs after every plugin. s.stringify(node) and s.inner(node) (its children) give their Markdown, with the edits inside.
  • When a node is edited more than once, the last edit wins.

To edit one output only, pass its namespace as the last argument: search for the records of Remark Structure, markdown for Remark LLMs.

// the Markdown for LLMs keeps the tag, search records don't
replaceSource(file, node, (s) => s.stringify(node).replace(' [step]', ''), 'search');

For search, a plugin adds the records of its nodes with data.structuredData.

Runtime Values

For Markdown only known at runtime, output a JS expression with s.expression(). Remark LLMs evaluates it where it exports the Markdown. Other outputs use the fallback:

import type { Expression } from 'estree';

// the URL of `import image from './image.png'` in the compiled document
const url: Expression = { type: 'Identifier', name: 'image' };

replaceSource(file, node, (s) => `![alt](${s.expression(url, './image.png')})`);

Embedded Content

Nodes parsed from another source, like an included file, have positions in that source. Embed them with embedSource() to stringify them like your own content:

import { embedSource } from 'fumadocs-core/mdx-plugins/stringifier';

// replaces `node` with the nodes parsed from `otherSource`
embedSource(file, node, otherSource, otherTree.children);

Other nodes parsed from a string must not keep their position, because it doesn't point into your document.

The helpers on this page are for unified plugins. On Sätteri, Remark LLMs and Remark Structure output Markdown the same way, with no public API to edit it.

How is this guide?