Skip to content

Add rehype-mdx-toc to export toc to list of plugins #2622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2025

Conversation

boning-w
Copy link
Contributor

@boning-w boning-w commented Jul 5, 2025

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and discussions and couldn’t find anything or linked relevant results below
  • I made sure the docs are up to date
  • I included tests (or that’s not needed)

Description of changes

Add a rehype plugin rehype-mdx-toc that allows users to export the table of contents (TOC) data as a named export from an MDX module.

Why another table of contents plugin?

Since MDX supports exporting data from MDX modules, it's much more convinent to export the TOC — so that users can use it whatever they like, just like with remark-mdx-frontmatter.

Simple usage:

import { compile, run } from "@mdx-js/mdx";
import rehypeMdxToc from "rehype-mdx-toc";
import * as runtime from "react/jsx-runtime";

const mdxContent = `
# Heading 1
## Heading 2
### Heading 3
`;

const compiled = await compile(mdxContent, {
  outputFormat: "function-body",
  rehypePlugins: [rehypeMdxToc],
});
const result = await run(compiled, { ...runtime });
const toc = result.toc;
console.log(toc);

With some bundlers — like @mdx-js/esbuild, @mdx-js/rollup, and @mdx-js/loader, we can easily import the TOC data at build time:

import MDXContent, { toc } from "./you-mdx-file.mdx";

Copy link

vercel bot commented Jul 5, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mdx ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 5, 2025 3:19am

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Jul 5, 2025
Copy link

codecov bot commented Jul 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (908ff45) to head (6a728d2).
Report is 91 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #2622   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           23        21    -2     
  Lines         2693      2650   -43     
  Branches         2         2           
=========================================
- Hits          2693      2650   -43     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wooorm wooorm merged commit c156a1f into mdx-js:main Jul 5, 2025
9 checks passed
@wooorm
Copy link
Member

wooorm commented Jul 5, 2025

Thank you, cool!

This comment has been minimized.

@wooorm wooorm added 📚 area/docs This affects documentation 💪 phase/solved Post is done labels Jul 5, 2025
@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label Jul 5, 2025
@boning-w
Copy link
Contributor Author

boning-w commented Jul 5, 2025

Thank you all!

@boning-w boning-w deleted the patch-1 branch July 5, 2025 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 area/docs This affects documentation 💪 phase/solved Post is done
Development

Successfully merging this pull request may close these issues.

4 participants