-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathTOC.tsx
87 lines (81 loc) · 2.52 KB
/
TOC.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React, { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
const TOC: React.FC = () => {
const router = useRouter();
const [headings, setHeadings] = useState<
{ id: string; text: string; level: number }[]
>([]);
const [activeId, setActiveId] = useState<string | null>(null);
useEffect(() => {
const mainContent = document.getElementById('main-content');
const elements = mainContent
? mainContent.querySelectorAll('h1, h2, h3, h4')
: [];
const newHeadings: { id: string; text: string; level: number }[] = [];
elements.forEach((el) => {
const text = el.textContent || '';
if (text.trim().toLowerCase() === 'on this page') return;
if (el.closest('#sidebar')) return;
const currentFolder = router.pathname.split('/').pop()?.toLowerCase();
if (text.trim().toLowerCase() === currentFolder) return;
if (
text.includes('/') ||
text.includes('\\') ||
/\.md$|\.tsx$|\.jsx$|\.js$/i.test(text.trim())
)
return;
const level = parseInt(el.tagName.replace('H', ''));
if (!el.id) {
const generatedId = text
.toLowerCase()
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w-]+/g, '');
if (generatedId) {
el.id = generatedId;
}
}
newHeadings.push({
id: el.id,
text: text,
level,
});
});
setHeadings(newHeadings);
const handleScroll = () => {
let currentId: string | null = null;
elements.forEach((el) => {
const rect = (el as HTMLElement).getBoundingClientRect();
if (rect.top <= 100) {
currentId = el.id;
}
});
setActiveId(currentId);
};
window.addEventListener('scroll', handleScroll);
handleScroll();
return () => window.removeEventListener('scroll', handleScroll);
}, [router.asPath]);
return (
<nav className='w-full p-4 sticky top-24 overflow-y-auto scrollbar-hide'>
<h2 className='text-xl font-bold uppercase text-slate-400 mb-4'>
On this page
</h2>
<ul className=''>
{headings.map((heading) => (
<li
key={heading.id}
className={`pl-${(heading.level - 1) * 2} ${
activeId === heading.id
? 'text-primary font-semibold'
: 'text-slate-600'
}`}
>
<a href={`#${heading.id}`}>{heading.text}</a>
</li>
))}
</ul>
</nav>
);
};
export default TOC;