Skip to content

rehype MDX plugin to export a JavaScript array of headings

License

Notifications You must be signed in to change notification settings

kazushisan/rehype-mdx-headings

Repository files navigation

rehype-mdx-headings

CI npm version

rehype MDX plugin to export a JavaScript array of headings

Install

This package is ESM only.

npm install

Usage

This plugin will look for headings (h1, h2, h3, ... , h6) in your markdown file and export a list of headings as a constant.

import { compile } from '@mdx-js/mdx';
import rehypeMdxHeadings from 'rehype-mdx-headings';

const content = `
# Hello

Some content here.

## World

Some more content here.
`;

await compile(content, {
  rehypePlugins: [rehypeMdxHeadings],
});

will roughly yield

export const headings = [{ value: 'Hello', depth: 1 }, { value: 'World', depth: 2 }];

# Hello

Some content here.

## World

Some more content here.

License

MIT © Kazushi Konosu