Newer
Older
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React from "react";
import { useEffect, useState } from "react";
let navulliStyle = {
marginBottom: "15px",
} as React.CSSProperties;
const getClassName = (level) => {
switch (level) {
case 1:
navulliStyle = {
...navulliStyle,
};
return "head1";
case 2:
navulliStyle = {
...navulliStyle,
marginLeft: "10px",
};
return "head2";
case 3:
navulliStyle = {
...navulliStyle,
marginLeft: "20px",
};
return "head3";
case 4:
navulliStyle = {
...navulliStyle,
marginLeft: "30px",
};
return "head4";
default:
return undefined;
}
};
const navStyle = {
width: "220px",
minWidth: "220px",
padding: "16px",
alignSelf: "flex-start",
position: "-webkit-sticky",
top: "48px",
maxHeight: "calc(100vh - 70px)",
overflow: "auto",
marginTop: "150px",
} as React.CSSProperties;
interface Props {
children?: React.ReactNode;
className?: string;
}
const Toc = (props: Props) => {
const { children, className = "" } = props;
const [headings, setHeadings] = useState<any>([]);
useEffect(() => {
const elements = Array.from(
document.querySelectorAll("h1, h2, h3, h4")
).map((elem) => ({
id: Math.random().toString(16).slice(-4),
text: elem.textContent,
level: Number(elem.nodeName.charAt(1)),
}));
console.log(elements);
setHeadings(elements);
}, []);
return (
<nav
style={navStyle}
className={`absolute inset-y-0 right-0 w-16 ${className}`}
>
<ul style={navulliStyle}>
{headings.map((heading) => (
<li
key={heading.id}
className={getClassName(heading.level)}
style={navulliStyle}
>
<a
href={`#${heading.id}`}
onClick={(e) => {
e.preventDefault();
document.querySelector(`#${heading.id}`)?.scrollIntoView({
behavior: "smooth",
});
}}
>
{children}
{heading.text}
</a>
</li>
))}
</ul>
</nav>
);
};
export default Toc;