Skip to content

Commit 9c23e86

Browse files
Added dynamic table of content
1 parent a217520 commit 9c23e86

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// @ts-check
22

33
/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
4-
const sidebars = {
4+
export default {
55
user: [{ type: "autogenerated", dirName: "user" }],
66
contributing: [{ type: "autogenerated", dirName: "contributing" }],
77
};
8-
9-
export default sidebars;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { useEffect } from "react";
2+
3+
export default function DynamicTOC() {
4+
useEffect(() => {
5+
const row = document.querySelector("main .row");
6+
7+
if (row == null) {
8+
console.error("No row found in the main element");
9+
return;
10+
}
11+
12+
row.appendChild(getTOC());
13+
}, []);
14+
15+
return null;
16+
}
17+
18+
function getTOC() {
19+
const headingElements = Array.from(document.querySelectorAll("h3")).map(
20+
(el) => ({
21+
id: el.id,
22+
text: el.textContent?.replace("#", "") ?? "",
23+
}),
24+
);
25+
26+
const col = document.createElement("div");
27+
col.className = "col col--3";
28+
29+
const toc = document.createElement("div");
30+
toc.className = "tableOfContents_tkZC thin-scrollbar";
31+
toc.style.position = "fixed";
32+
toc.style.right = "1rem";
33+
toc.style.width = "14rem";
34+
col.appendChild(toc);
35+
36+
const ul = document.createElement("ul");
37+
ul.className = "table-of-contents table-of-contents__left-border";
38+
toc.appendChild(ul);
39+
40+
headingElements.forEach((h) => {
41+
const li = document.createElement("li");
42+
43+
const a = document.createElement("a");
44+
a.href = `#${h.id}`;
45+
a.className = "table-of-contents__link";
46+
a.textContent = h.text;
47+
48+
li.appendChild(a);
49+
ul.appendChild(li);
50+
});
51+
52+
return col;
53+
}

packages/cursorless-org-docs/src/docs/user/languages/components/Language.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import DynamicTOC from "./DynamicTOC";
23
import { ScopeSupport } from "./ScopeSupport";
34

45
interface Props {
@@ -8,6 +9,7 @@ interface Props {
89
export function Language({ languageId }: Props) {
910
return (
1011
<>
12+
<DynamicTOC />
1113
<ScopeSupport languageId={languageId} />
1214
</>
1315
);

0 commit comments

Comments
 (0)