Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into vale/refine-rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Meschreiber committed Jan 10, 2024
2 parents ec959fc + 326e67e commit 1280790
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/components/TableOfContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,37 @@ export default function TableOfContents({
}) {
const [activeId, setActiveId] = useState(null);
const scrollDisabled = useRef(false);
const [pageHeadings, setPageHeadings] = useState(headings);
useEffect(() => {
setPageHeadings(
[
...document
.querySelector('main')
.querySelectorAll('h2[id],h3[id],h4[id],h5[id],h6[id]')
.values()
].map(heading => {
const link = heading.querySelector('a');
return {
value: heading.title || link?.innerText || heading.innerText,
depth: parseInt(heading.tagName[1]),
id: heading.id
};
})
);
}, []);

const toc = useMemo(() => {
const slugger = new GithubSlugger();
return headings
return pageHeadings
.filter(
heading =>
heading.depth >= MIN_HEADING_DEPTH && heading.depth <= headingDepth
)
.map(heading => ({
...heading,
id: slugger.slug(heading.value)
id: slugger.slug(heading.value),
...heading
}));
}, [headings, headingDepth]);
}, [pageHeadings, headingDepth]);

useEffect(() => {
function handleScroll(event) {
Expand Down

0 comments on commit 1280790

Please sign in to comment.