@@ -48,15 +48,34 @@ const toc = generateToc(headings)
48
48
constructor() {
49
49
super()
50
50
51
- // Initialize the headings and tocLinks
51
+ // 1. Get the correct top-level headings for interaction.
52
52
this.headings = Array.from(
53
- document.querySelectorAll('article h1, article h2, article h3, article h4, article h5, article h6')
54
- )
55
- this.tocLinks = Array.from(this.querySelectorAll('a[href^="#"]')).map((link) => ({
53
+ document.querySelectorAll('#content > h1, #content > h2, #content > h3, #content > h4, #content > h5, #content > h6')
54
+ ) as HTMLElement[];
55
+
56
+ const validSlugs = new Set(this.headings.map(h => h.id));
57
+
58
+ // 2. Filter the visible TOC links based on the valid slugs.
59
+ const allLinks = Array.from(this.querySelectorAll('a[href^="#"]'));
60
+ allLinks.forEach(link => {
61
+ const slug = (link.getAttribute('href') || '').substring(1);
62
+ if (!validSlugs.has(slug)) {
63
+ // Hide the parent <li> element of the invalid link.
64
+ link.parentElement?.style.setProperty('display', 'none');
65
+ }
66
+ });
67
+
68
+ // 3. Initialize the tocLinks array with only the visible links.
69
+ this.tocLinks = allLinks
70
+ .filter(link => {
71
+ const slug = (link.getAttribute('href') || '').substring(1);
72
+ return validSlugs.has(slug);
73
+ })
74
+ .map((link) => ({
56
75
element: link as HTMLAnchorElement,
57
76
progressBar: link.previousElementSibling as HTMLElement,
58
77
slug: (link.getAttribute('href') || '').substring(1)
59
- }))
78
+ }));
60
79
}
61
80
62
81
updatePositionAndStyle = () => {
0 commit comments