Architecture
The MDX processing pipeline consists of three main layers:1. Remark Plugins (Markdown AST)
Remark plugins operate on the Markdown Abstract Syntax Tree (MDAST) before MDX compilation. They handle:- Syntax Extensions: Admonitions, steps, code tabs
- Content Processing: Headings, images, structure extraction
- TOC Generation: Table of contents from headings
2. Rehype Plugins (HTML AST)
Rehype plugins work on the HTML AST (HAST) after MDX compilation:- Code Highlighting: Shiki-powered syntax highlighting
- Code Transformations: Line numbers, highlighting, diff markers
- TOC Rendering: Convert heading data to navigable TOC
3. Shiki Transformers
Shiki transformers enhance code blocks with:- Language Icons: Automatic language detection and icon display
- Notation Support: Highlight, focus, diff, word highlighting
- Twoslash Integration: TypeScript type information in code
Available Plugins
Remark Plugins
| Plugin | Purpose | Location |
|---|---|---|
remarkHeading | Generate heading IDs and TOC | /packages/core/src/mdx-plugins/remark-heading.ts:41 |
remarkStructure | Extract structured data for search | /packages/core/src/mdx-plugins/remark-structure.ts:155 |
remarkImage | Optimize images for Next.js | /packages/core/src/mdx-plugins/remark-image.ts:82 |
remarkAdmonition | Docusaurus-style callouts | /packages/core/src/mdx-plugins/remark-admonition.ts:22 |
remarkDirectiveAdmonition | Directive-based admonitions | /packages/core/src/mdx-plugins/remark-directive-admonition.ts:29 |
remarkCodeTab | Tabbed code blocks | /packages/core/src/mdx-plugins/remark-code-tab.ts:178 |
remarkSteps | Numbered step sequences | /packages/core/src/mdx-plugins/remark-steps.ts:27 |
Rehype Plugins
| Plugin | Purpose | Location |
|---|---|---|
rehypeCode | Syntax highlighting with Shiki | /packages/core/src/mdx-plugins/rehype-code.ts:18 |
rehypeToc | Generate table of contents | /packages/core/src/mdx-plugins/rehype-toc.ts |
Basic Configuration
Here’s how to configure MDX processing in your Next.js application:source.config.ts
Unified Pipeline
The processing flow follows this sequence:- Parse: Markdown text → MDAST
- Remark: Transform markdown syntax
- Compile: MDAST → HAST (with MDX)
- Rehype: Transform HTML/JSX
- Output: React components