fix #1016 - exclude headings in tabs or collapsibles from table of contents

This commit is contained in:
James Fenn
2024-01-21 01:33:47 -05:00
parent f494fa40af
commit deac45cd00

View File

@@ -2,7 +2,7 @@ import { headingRank } from "hast-util-heading-rank";
import { hasProperty } from "hast-util-has-property";
import { toString } from "hast-util-to-string";
import { Root, Parent } from "hast";
import { visit } from "unist-util-visit";
import { visit, SKIP } from "unist-util-visit";
/**
* Plugin to add `data-header-text`s to headings.
@@ -12,6 +12,14 @@ export const rehypeHeaderText = () => {
const headingsWithId = (file.data.astro.frontmatter.headingsWithId = []);
visit(tree, "element", (node: Parent["children"][number]) => {
// Don't descend into tab containers or collapsible <details> elements
if (
"properties" in node &&
(node.properties["role"] === "tabpanel" || node.tagName === "details")
) {
return SKIP;
}
if (
headingRank(node) &&
"properties" in node &&