import Link from 'next/link'; import { cx } from '@nx/nx-dev/ui-primitives'; import { useHeadingsObserver } from './use-headings-observer'; interface Heading { id: string; level: number; title: string; } export function collectHeadings( node: any, sections: Heading[] = [] ): Heading[] { if (node) { if (node.name && node.name === 'Heading') { const title = node.children[0]; if (typeof title === 'string') { sections.push({ id: node.attributes['id'], level: node.attributes['level'], title, }); } } if (node.children) { for (const child of node.children) { collectHeadings(child, sections); } } } return sections; } export function TableOfContents({ elementRef, headings, path, children, }: { elementRef: any; headings: Heading[]; path: string; children: React.ReactNode; }): JSX.Element { const headingLevelTargets: number[] = [1, 2, 3]; // matching to: H1, H2, H3... const items = headings.filter( (item) => item.id && headingLevelTargets.includes(item.level) ); const activeId = useHeadingsObserver( elementRef, { threshold: [0, 0.25, 0.5, 0.75, 1], root: null, rootMargin: '-10% 0% -45% 0%', }, headings.find((i) => i.level === 1)?.title || null ); return ( <>