@@ -1,4 +1,3 @@
1−import { useState, useEffect, useRef } from "react";
21import { useRouter } from "next/router";
32import Head from "next/head";
43import ErrorPage from "../pages/404";
@@ -9,116 +8,7 @@ import SingleColumn from "./SingleColumn";
98import Section from "./Section";
109import markdownStyles from "../styles/markdown.module.css";
1110import { decode } from "html-entities";
12−
13−const TableOfContents = () => {
14− const { nestedHeadings } = useHeadingsData();
15− const [activeId, setActiveId] = useState();
16− useIntersectionObserver(setActiveId);
17− return (
18− <nav
19− className="sticky min-h-0 overflow-y-scroll w-52 flex-shrink-0 pb-24 hidden lg:block"
20− style={{ top: "8rem", height: "calc(100vh - 16rem)" }}
21− >
22− <Headings headings={nestedHeadings} activeId={activeId} />
23− </nav>
24− );
25−};
26−
27−const getNestedHeadings = (headingElements) => {
28− const nestedHeadings = [];
29−
30− headingElements.forEach((heading, index) => {
31− const { innerText: title, id } = heading;
32−
33− if (heading.nodeName === "H2") {
34− nestedHeadings.push({ id, title, items: [] });
35− } else if (heading.nodeName === "H3" && nestedHeadings.length > 0) {
36− nestedHeadings[nestedHeadings.length - 1].items.push({
37− id,
38− title,
39− });
40− }
41− });
42−
43− return nestedHeadings;
44−};
45−
46−const useHeadingsData = () => {
47− const [nestedHeadings, setNestedHeadings] = useState([]);
48−
49− useEffect(() => {
50− const headingElements = Array.from(document.querySelectorAll("h2, h3"));
51−
52− const newNestedHeadings = getNestedHeadings(headingElements);
53− setNestedHeadings(newNestedHeadings);
54− }, []);
55−
56− return { nestedHeadings };
57−};
58−
59−const Headings = ({ headings, activeId }) => (
60− <ul>
61− {headings.map((heading) => (
62− <li key={heading.id}>
63− <a className="font-semibold" href={`#${heading.id}`}>
64− {heading.title}
65− </a>
66− {heading.items.length > 0 && (
67− <ul className="pl-2">
68− {heading.items.map((child) => (
69− <li
70− key={child.id}
71− className={child.id === activeId ? "text-green" : "text-gray"}
72− >
73− <a href={`#${child.id}`}>{child.title}</a>
74− </li>
75− ))}
76− </ul>
77− )}
78− </li>
79− ))}
80− </ul>
81−);
82−
83−const useIntersectionObserver = (setActiveId) => {
84− const headingElementsRef = useRef({});
85− useEffect(() => {
86− const callback = (headings) => {
87− headingElementsRef.current = headings.reduce((map, headingElement) => {
88− map[headingElement.target.id] = headingElement;
89− return map;
90− }, headingElementsRef.current);
91−
92− const visibleHeadings = [];
93− Object.keys(headingElementsRef.current).forEach((key) => {
94− const headingElement = headingElementsRef.current[key];
95− if (headingElement.isIntersecting) visibleHeadings.push(headingElement);
96− });
97−
98− const getIndexFromId = (id) =>
99− headingElements.findIndex((heading) => heading.id === id);
100−
101− if (visibleHeadings.length === 1) {
102− setActiveId(visibleHeadings[0].target.id);
103− } else if (visibleHeadings.length > 1) {
104− const sortedVisibleHeadings = visibleHeadings.sort(
105− (a, b) => getIndexFromId(a.target.id) > getIndexFromId(b.target.id)
106− );
107− setActiveId(sortedVisibleHeadings[0].target.id);
108− }
109− };
110−
111− const observer = new IntersectionObserver(callback, {
112− rootMargin: "0px 0px -50% 0px",
113− });
114−
115− const headingElements = Array.from(document.querySelectorAll("h2, h3"));
116−
117− headingElements.forEach((element) => observer.observe(element));
118−
119− return () => observer.disconnect();
120− }, [setActiveId]);
121−};
11+import { TableOfContents } from "./TableOfContents";
12212
12313export default function PageWithIndex({ post, markdown, search }) {
12414 const router = useRouter();