Skip to content

SFM renderer API

[!WARNING] The SFM npm package has not been released. This API reference describes the current browser preview. Interfaces may change before the public release.

For the first integration, follow Get started with SFM.

Entry point Interface
/renderer render() and the renderer’s TypeScript types
/outline mountOutline() for a table of contents in a host-provided element
Package root and /syntax normalizeMarkdownOptions(value: unknown): MarkdownOptions and splitFrontMatter(source: string): FrontMatter | null
/styles/document.css Document styles scoped to the data-sfm-reader mount

The subpaths identify modules in the preview. The syntax helpers normalize Markdown settings and return the format, raw front matter, and remaining body when a leading front matter block is recognized.

The renderer requires a browser DOM element and a Markdown string. Pass a dedicated element for the document body. Rendering replaces its contents, and destroying the instance empties it.

function render(
container: Element,
source: string,
options?: RendererOptions,
resolveImage?: ImageResolver,
resolveMedia?: MediaResolver,
resolveLink?: LinkResolver,
): RendererInstance;
interface RendererInstance {
getResult(): RenderResult;
update(source: string, options?: RendererOptions): RenderResult;
destroy(): void;
}

For example, a client component can mount on creation, supply a new Markdown snapshot when its source changes, and destroy the reader on unmount. This snippet assumes the preview’s render function and stylesheet are already available in the host.

const options: RendererOptions = {
appearance: "auto",
outlineMode: "off",
codeLineNumbers: true,
};
const reader = render(container, markdown, options);
reader.update(nextMarkdown, options);
const result = reader.getResult();
reader.destroy();

Pass the desired options on each update(). An update without options uses the defaults. Resolver functions are supplied when mounting and are reused on updates. Updates and teardown cancel pending resource requests and release the reader’s temporary URLs.

Options Behavior
appearance "auto" (default), "light", or "dark". Auto uses an appearance already set on the container or follows the system.
theme background and foreground accept six-digit hex colors such as #ffffff. Rendering and themes lists the diagram CSS properties.
reading.fontFamily "system", "systemSerif", or a single font family name. Defaults to the system font.
reading.fontSizePx 12–20 px. Defaults to 14 px.
reading.lineHeightPx Integer pixels from 1.2× to 2.2× the font size, with both bounds rounded. Defaults to 1.7× the font size, rounded.
reading.contentWidthPx Integer pixels from 25× to 80× the font size, with both bounds rounded. Unset by default so the available width determines the text column.
reading.sideInsetPx 8–64 px. Defaults to 28 px.
reading.paragraphSpacingPx 0–3× the font size in pixels. Defaults to 0.8× the font size.
outlineMode "wave-rail" (default), "sidebar", or "off".
numbersHeadings, headingNumberingStyle Heading numbering is off by default. When enabled, choose "outline-levels" (default) or "document-title".
inlineMath, codeLineNumbers Toggle single-dollar inline math (on by default) and code line numbers (off by default).
mediaLoading "manual" (default) waits for a click. "lazy" loads near the viewport. Playback starts when the reader chooses to play.

RenderResult contains normalized options, reading, and mediaLoading. Inspect appliedOptions, pendingOptions, and unsupportedOptions for reported option status. diagnostics reports issues with source ranges. unsupportedBlocks contains the token type, source range, and original text for blocks reported as unsupported. Source ranges use UTF-16 offsets into the Markdown string, with an exclusive end. Formula rendering can continue after the initial synchronous return. Call getResult() again to read the latest option status.

To render the outline in your own layout, set outlineMode: "off" and call mountOutline() from /outline.

function mountOutline(
root: Element,
target: Element,
options?: OutlineMountOptions,
): () => void;
interface OutlineMountOptions {
minHeadingLevel?: number;
maxHeadingLevel?: number;
titleHeading?: HTMLHeadingElement | null;
currentLabel?: HTMLElement | null;
mobile?: boolean;
}

The controller builds nested links from headings in root and marks the active link with aria-current. It uses heading levels 2 to 3 by default. titleHeading can include a page heading, and currentLabel can display the active heading outside the list. mobile: true adds a class for mobile styling. To omit a section from the outline, mark its container with data-sfm-ignore-outline. The host controls layout and interactions such as mobile disclosure. Call the returned cleanup function when removing the outline.

Pass the document’s mount and a separate navigation element to mountOutline().

const reader = render(container, source, { outlineMode: "off" });
const disposeOutline = mountOutline(container, navigation, {
minHeadingLevel: 2,
maxHeadingLevel: 4,
});
function disposeDocument() {
disposeOutline();
reader.destroy();
}
type ImageResolver = (
reference: string,
signal: AbortSignal,
) => Promise<Blob | null>;
type MediaResolver = (
reference: string,
kind: "audio" | "video",
signal: AbortSignal,
) => Promise<Blob | { url: string } | null>;
type LinkResolver = (reference: string) => string | null;
  • Images. For file and network references, resolve the original reference against the document’s base, check access, and return an authorized image Blob. The reader validates the data before displaying it. Missing or rejected images keep a text placeholder. Supported raster data already embedded in the source can be validated and displayed directly.
  • Audio and video. Authorize the source and return a Blob or an approved absolute HTTP(S) URL in { url }. The URL option lets the browser’s player fetch the media directly. Format support and seeking depend on the browser and media server. Unavailable media keeps a readable placeholder.
  • Relative links. Resolve the original reference to an approved absolute http:, https:, or mailto: URL. Return null to leave the label as plain text. Supported in-document fragments such as #heading are handled by the reader.

Accepting a reference as valid Markdown syntax grants no access to it. Apply your app’s source and access policy before reading a file, fetching a URL, or handing it to the browser. Honor the AbortSignal passed to image and media resolvers.

See Markdown syntax and live rendering examples.