.mdx template against a props object. The snippets below use the Node API (load + transform + stringify). If your runtime can’t read templates from disk (browser, edge), precompile them to JSON ASTs at build time; see the last section.
Install
- npm
- yarn
- pnpm
Render a template (Node)
Givenmy-template.mdx:
my-template.mdx
load(path): parses the.mdxfile from disk into an AST.transform(ast, props): evaluates expressions and tag plugins againstprops.stringify(ast): serializes the rendered AST back to a Markdown/text string.
Render a precompiled template (browser / edge)
transform() consumes a TemplateDX AST, not a compiled MDX component. Standard MDX loaders (@mdx-js/loader, @next/mdx, Vite MDX plugins) compile .mdx into a component function, which transform() can’t use. To render templates where you can’t read from disk, precompile them to JSON in a build step:
precompile.ts
load:
render.ts
load bundles imported components into the AST, so precompiled templates keep working when they import other .mdx files.
Next steps
Syntax
Tags, expressions, components, and XML passthrough
Variables
Props and scope resolution
Custom tags
Write your own
<Tag> pluginsCustom filters
Extend expression evaluation with filters