summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src/rehype-collect-headings.ts
blob: d7564b94189fb3a77e8ef6e8271b5f9f61f192ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
import { MarkdownHeading, MarkdownVFile } from '@astrojs/markdown-remark';
import { jsToTreeNode } from './utils.js';

export function rehypeInjectHeadingsExport() {
	return function (tree: any, file: MarkdownVFile) {
		const headings: MarkdownHeading[] = file.data.__astroHeadings || [];
		tree.children.unshift(
			jsToTreeNode(`export function getHeadings() { return ${JSON.stringify(headings)} }`)
		);
	};
}