MDX compiles Markdown with JSX into JavaScript components. Under the hood it runs remark and rehype, which means any rehype plugin can transform your MDX content before it becomes React code. Next.js exposes the same option through @next/mdx.
What we tested. The MDX example below was run with @mdx-js/mdx 3.1 and rehype-slug 6 on Node.js 22. The Next.js configuration follows the official Next.js MDX documentation (last updated August 10, 2026); we checked it against those docs but did not run it inside a Next.js app.
MDX: the rehypePlugins option
npm install @mdx-js/mdx rehype-slugimport {compile} from '@mdx-js/mdx'
import rehypeSlug from 'rehype-slug'
const mdx = '# Hello MDX\n\nRegular *markdown* plus JSX: <Note>Saved</Note>'
const result = await compile(mdx, {rehypePlugins: [rehypeSlug]})
// Print the part of the compiled JavaScript that creates the heading.
const code = String(result)
const start = code.indexOf('_jsx(_components.h1')
console.log(code.slice(start, code.indexOf('})', start) + 2))The relevant part of the compiled JavaScript shows that rehype-slug ran, because the heading now gets an id prop:
_jsx(_components.h1, {
id: "hello-mdx",
children: "Hello MDX"
})The Note component in the MDX source does not appear in this excerpt; MDX expects you to provide it when rendering. rehype plugins see JSX elements as special MDX nodes and leave them alone, while ordinary Markdown becomes regular hast elements they can transform.
Passing options
Use a tuple of plugin and options:
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeSlug from 'rehype-slug'
const options = {
rehypePlugins: [rehypeSlug, [rehypeAutolinkHeadings, {behavior: 'wrap'}]]
}
// await compile(source, options)The same rehypePlugins and remarkPlugins options are accepted by MDX integrations such as @mdx-js/loader for webpack and @mdx-js/rollup.
Next.js with @next/mdx
Install the packages listed in the Next.js documentation, plus the plugins you want:
npm install @next/mdx @mdx-js/loader @mdx-js/react @types/mdx rehype-slugBecause the unified ecosystem is ESM only, the configuration file must be next.config.mjs (or next.config.ts). Pass plugins in the options object of createMDX:
import createMDX from '@next/mdx'
import rehypeSlug from 'rehype-slug'
/** @type {import('next').NextConfig} */
const nextConfig = {
pageExtensions: ['js', 'jsx', 'md', 'mdx', 'ts', 'tsx']
}
const withMDX = createMDX({
options: {
remarkPlugins: [],
rehypePlugins: [rehypeSlug]
}
})
export default withMDX(nextConfig)With the App Router, Next.js also requires an mdx-components.tsx (or .js) file at the project root that exports useMDXComponents; see the Next.js documentation for its contents.
Turbopack: plugins as strings
The Next.js documentation explains that with Turbopack, plugins must be given by package name as strings, because JavaScript functions cannot be passed to its Rust core. Options must then be serializable:
const withMDX = createMDX({
options: {
rehypePlugins: [
'rehype-slug',
['rehype-autolink-headings', {behavior: 'wrap'}]
]
}
})That rules out options containing functions, such as a custom content builder for rehype-autolink-headings. Keep those plugins on webpack, or move the logic into a component in mdx-components.
Plain React without MDX: rehype-react
If you have HTML (not MDX) and want React elements instead of an HTML string, rehype-react compiles the hast tree into React elements, which avoids dangerouslySetInnerHTML. Sanitize untrusted input first, exactly as you would for HTML output. Its readme shows how to pass the JSX runtime functions it needs.
Which plugins are popular in MDX setups?
rehype-slugandrehype-autolink-headingsfor linkable headings.rehype-highlightor Shiki-based highlighters for code.rehype-katexwithremark-mathfor formulas.