@@ -1,7 +1,7 @@
1import Head from "next/head";
1import Head from "next/head";
2import Link from "next/link";
2import Link from "next/link";
3import { useRouter } from "next/router";
3import { useRouter } from "next/router";
4−import { useState } from "react";
4+import { useState, useEffect, useRef } from "react";
5import classnames from "classnames";
5import classnames from "classnames";
6import { join } from "path";
6import { join } from "path";
@@ -17,6 +17,116 @@ import Sidebar from "../../components/Sidebar";
17import markdownStyles from "../../styles/markdown.module.css";
17import markdownStyles from "../../styles/markdown.module.css";
18import { decode } from "html-entities";
18import { decode } from "html-entities";
20+const TableOfContents = () => {
21+ const { nestedHeadings } = useHeadingsData();
22+ const [activeId, setActiveId] = useState();
23+ useIntersectionObserver(setActiveId);
26+ className="sticky min-h-0 overflow-y-scroll w-52 flex-shrink-0 pb-24 hidden lg:block"
27+ style={{ top: "8rem", height: "calc(100vh - 16rem)" }}
29+ <Headings headings={nestedHeadings} activeId={activeId} />
34+const getNestedHeadings = (headingElements) => {
35+ const nestedHeadings = [];
37+ headingElements.forEach((heading, index) => {
38+ const { innerText: title, id } = heading;
40+ if (heading.nodeName === "H2") {
41+ nestedHeadings.push({ id, title, items: [] });
42+ } else if (heading.nodeName === "H3" && nestedHeadings.length > 0) {
43+ nestedHeadings[nestedHeadings.length - 1].items.push({
50+ return nestedHeadings;
53+const useHeadingsData = () => {
54+ const [nestedHeadings, setNestedHeadings] = useState([]);
57+ const headingElements = Array.from(document.querySelectorAll("h2, h3"));
59+ const newNestedHeadings = getNestedHeadings(headingElements);
60+ setNestedHeadings(newNestedHeadings);
63+ return { nestedHeadings };
66+const Headings = ({ headings, activeId }) => (
68+ {headings.map((heading) => (
69+ <li key={heading.id}>
70+ <a className="font-semibold" href={`#${heading.id}`}>
73+ {heading.items.length > 0 && (
74+ <ul className="pl-2">
75+ {heading.items.map((child) => (
78+ className={child.id === activeId ? "text-green" : "text-gray"}
80+ <a href={`#${child.id}`}>{child.title}</a>
90+const useIntersectionObserver = (setActiveId) => {
91+ const headingElementsRef = useRef({});
93+ const callback = (headings) => {
94+ headingElementsRef.current = headings.reduce((map, headingElement) => {
95+ map[headingElement.target.id] = headingElement;
97+ }, headingElementsRef.current);
99+ const visibleHeadings = [];
100+ Object.keys(headingElementsRef.current).forEach((key) => {
101+ const headingElement = headingElementsRef.current[key];
102+ if (headingElement.isIntersecting) visibleHeadings.push(headingElement);
105+ const getIndexFromId = (id) =>
106+ headingElements.findIndex((heading) => heading.id === id);
108+ if (visibleHeadings.length === 1) {
109+ setActiveId(visibleHeadings[0].target.id);
110+ } else if (visibleHeadings.length > 1) {
111+ const sortedVisibleHeadings = visibleHeadings.sort(
112+ (a, b) => getIndexFromId(a.target.id) > getIndexFromId(b.target.id)
114+ setActiveId(sortedVisibleHeadings[0].target.id);
118+ const observer = new IntersectionObserver(callback, {
119+ rootMargin: "0px 0px -50% 0px",
122+ const headingElements = Array.from(document.querySelectorAll("h2, h3"));
124+ headingElements.forEach((element) => observer.observe(element));
126+ return () => observer.disconnect();
20const breadcrumbs = (posts, paths) => {
130const breadcrumbs = (posts, paths) => {
21 const results = [
131 const results = [
22 <Link href="/">Urbit</Link>,
132 <Link href="/">Urbit</Link>,
@@ -172,6 +282,7 @@ export default function DocsLayout({
174 </ContentArea>
284 </ContentArea>