aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src/rehype-collect-headings.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/mdx/src/rehype-collect-headings.ts')
-rw-r--r--packages/integrations/mdx/src/rehype-collect-headings.ts11
1 files changed, 11 insertions, 0 deletions
diff --git a/packages/integrations/mdx/src/rehype-collect-headings.ts b/packages/integrations/mdx/src/rehype-collect-headings.ts
new file mode 100644
index 000000000..a51e8e9f0
--- /dev/null
+++ b/packages/integrations/mdx/src/rehype-collect-headings.ts
@@ -0,0 +1,11 @@
+import type { VFile } from 'vfile';
+import { jsToTreeNode } from './utils.js';
+
+export function rehypeInjectHeadingsExport() {
+ return function (tree: any, file: VFile) {
+ const headings = file.data.astro?.headings ?? [];
+ tree.children.unshift(
+ jsToTreeNode(`export function getHeadings() { return ${JSON.stringify(headings)} }`),
+ );
+ };
+}