Add superpowers to your MDX with remark and rehype plugins — tables, math, syntax highlighting, auto-linked headings. Plus how to write your own.
MDX sits on top of the unified ecosystem. That means you get access to hundreds of remark and rehype plugins that transform your content at build time. Tables, math, syntax highlighting, auto-linked headings — you can add all of these without touching your MDX source files.
The Unified Pipeline
Here's how MDX processes your content:
Example
// The MDX compilation pipeline
import { compile } from '@mdx-js/mdx';
const result = await compile(mdxSource, {
remarkPlugins: [/* Transform Markdown AST */],
rehypePlugins: [/* Transform HTML AST */],
recmaPlugins: [/* Transform JS AST (advanced) */],
});
Two plugin types to know:
Remark plugins — work on the Markdown AST (before HTML conversion)
Rehype plugins — work on the HTML AST (after conversion)
Remark Plugins
These run on the Markdown tree. They shape your content before it becomes HTML.
remark-gfm (GitHub Flavored Markdown)
Gives you tables, strikethrough, task lists, and autolinks:
| Feature | Support |
|---------|---------|
| Tables | ❌ Not rendered |
- [ ] Task lists don't work
- [x] Checkboxes are plain text
~~Strikethrough~~ is ignored.
After (with remark-gfm):
Example
| Feature | Support |
|---------|---------|
| Tables | ✅ Fully rendered |
- [ ] Task lists render as checkboxes
- [x] Completed items are checked
~~Strikethrough~~ renders with line-through styling.
remark-math
Adds LaTeX math support to your MDX:
bash example
npm install remark-math rehype-katex katex
Example
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
export default {
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
};
Note: You also need to import the KaTeX CSS in your layout: import 'katex/dist/katex.min.css'
remark-toc (Table of Contents)
Auto-generates a table of contents from your headings:
bash example
npm install remark-toc
Example
import remarkToc from 'remark-toc';
export default {
remarkPlugins: [
[remarkToc, {
heading: 'Table of Contents', // Marker heading
maxDepth: 3, // Include h1-h3
tight: true, // Compact list
ordered: false, // Unordered list
}],
],
};
mdx example
## Table of Contents
{/* TOC is auto-generated here */}
## Introduction
Content here...
## Getting Started
More content...
### Installation
Sub-section...
remark-slug
Adds id attributes to headings so you can link to them:
bash example
npm install remark-slug
Example
import remarkSlug from 'remark-slug';
export default {
remarkPlugins: [remarkSlug],
};
This is where people get tripped up. Plugins run in order, and some depend on earlier plugins having already done their work.
Example
// ✅ CORRECT ORDER
rehypePlugins: [
rehypeSlug, // 1. First: add IDs to headings
rehypeAutolinkHeadings, // 2. Then: add links (needs IDs from step 1)
rehypeHighlight, // 3. After: highlight code blocks
rehypeCodeMeta, // 4. Last: process meta attributes
]
// ❌ WRONG ORDER - autolink won't find heading IDs
rehypePlugins: [
rehypeAutolinkHeadings, // Runs first but headings have no IDs yet!
rehypeSlug, // IDs are added too late
]
Dependency Chain
remarkFrontmatter
remarkMdxFrontmatter (needs parsed frontmatter)
remarkSlug
remarkToc (TOC needs heading slugs)
rehypeSlug
rehypeAutolinkHeadings (links need heading IDs)
remarkMath
rehypeKatex (katex renders parsed math nodes)
If something isn't working, check your plugin order first. It's almost always that.
Performance Considerations
Plugin
Build Impact
Recommendation
remark-gfm
Minimal (~5ms)
Always include
remark-math + rehype-katex
Moderate (~50ms)
Only if using math
rehype-highlight
High (~100ms/file)
Consider Shiki for large sites
rehype-prism-plus
Moderate (~60ms/file)
Good balance of speed/features
Custom plugins
Varies
Profile with console.time()
Tip: For sites with 100+ MDX pages, cache the compiled output. Each plugin adds processing time per file during build. Profile your pipeline with unified().use(reporter) to find bottlenecks.
Plugins are what make MDX truly powerful. Combine community plugins with your own custom transformers, and you can build a content pipeline that handles anything — math, callouts, reading time, code meta — all processed at build time with zero runtime cost.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for MDX Plugins.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this MDX Tutorial topic.