Markdown and MDX
Validate Throughline code blocks and show interactive traces in Markdown-based content.
Version scope — v0.0.2 (historical schema 1). This page and its examples use @0xbuff/throughline@0.0.2, the version pinned by this documentation app. They do not describe the unreleased schema-2 API intended for v0.2.0. See compatibility and upgrading.
Throughline compiles one thl block at a time. Your Markdown or MDX host must recognize those blocks and decide how to render them. Without an integration, a thl fence remains a normal code block.
Write a trace in Markdown
Two variables can refer to the same array:
```thl bindings
let items = [1, 2]
let alias = items
alias.push(3)
```The fence name is thl, followed by one of flow, bindings, binary, matrix, or callstack. No other header options are supported in v0.0.2.
You can compile the extracted fence directly:
import { compile } from "@0xbuff/throughline";
const trace = compile("thl binary\na = 1011\nb = 0110");Do not pass an entire Markdown document to compile(). The host's Markdown parser should identify its code blocks.
Render an MDX component
For a React 19 MDX host, create a component that accepts one trace source:
// ThroughlineExample.tsx
import { useMemo } from "react";
import { compile } from "@0xbuff/throughline";
import { ThroughlinePlayer } from "@0xbuff/throughline/react";
import "@0xbuff/throughline/styles/throughline.css";
export function ThroughlineExample({ source }: { source: string }) {
const trace = useMemo(() => compile(source), [source]);
return (
<figure>
<pre><code>{source}</code></pre>
<ThroughlinePlayer trace={trace} />
</figure>
);
}Register it with your MDX component map, then use it in a page:
<ThroughlineExample
source={'thl flow\nnode n "Count" = 0\nstep "Increase"\nset n = 1'}
/>This example assumes valid source. For production content, compile during the build to catch errors before users open the page. For interactive input, handle ProgramError in the host rather than letting invalid input break rendering.
Register it with Fumadocs
If you use Fumadocs, add the component to the map supplied when rendering MDX pages. Keep any existing components in the map:
// src/components/mdx.tsx
import defaultMdxComponents from "fumadocs-ui/mdx";
import type { MDXComponents } from "mdx/types";
import { ThroughlineExample } from "./ThroughlineExample";
export function getMDXComponents(components?: MDXComponents) {
return {
...defaultMdxComponents,
ThroughlineExample,
...components,
} satisfies MDXComponents;
}This registers the component; it does not turn every fenced thl block into a player.
Validate fences during the MDX build
A remark plugin can find thl code nodes and validate them with source positions. This example assumes the host's parser puts the domain in node.meta.
// remark-validate-throughline.ts
import { compileAst } from "@0xbuff/throughline";
import type { Root } from "mdast";
import { visit } from "unist-util-visit";
export function remarkValidateThroughline() {
return (tree: Root) => {
visit(tree, "code", (node) => {
if (node.lang !== "thl") return;
const domain = node.meta?.trim();
if (!domain) {
throw new Error("Expected a Throughline domain, for example: thl flow");
}
compileAst({
kind: domain,
lines: node.value.split(/\r\n?|\n/),
source: node.position?.start.line
? { line: node.position.start.line }
: undefined,
});
});
};
}Install the remark visitor and types if needed:
npm install -D unist-util-visit @types/mdastThen register the validator in your existing Fumadocs MDX options; preserve the other plugins:
// source.config.ts (illustrative)
import { defineConfig } from "fumadocs-mdx/config";
import { remarkValidateThroughline } from "./remark-validate-throughline";
export default defineConfig({
mdxOptions: {
remarkPlugins: (existing) => [remarkValidateThroughline, ...existing],
},
});The plugin validates content but does not render it. To automatically render every thl fence, add an MDX AST transform that replaces validated code nodes with ThroughlineExample elements. Keep the literal source available in Markdown exports and copy controls. Depending on the host pipeline, syntax highlighting may consume code nodes first, so register the transform before that stage.
A build-time optimization can serialize precompiled Trace data instead of compiling inside the React component. That requires host-specific content plumbing; it is not a built-in Throughline feature.
Plain Markdown and publishing
In a non-MDX site, your Markdown renderer must explicitly recognize thl fences and mount a visualization. A plain Markdown file cannot run JSX.
For both formats, validate untrusted input, render labels and notes as plain text, and avoid repeatedly compiling unchanged source. See Errors and limits for diagnostics and Frameworks for runtime setups.