Skip to content

Commit 30464cc

Browse files
Fix the rendering of shortcuts with branch bundles
1 parent 02d8fa7 commit 30464cc

1 file changed

Lines changed: 41 additions & 7 deletions

File tree

assets/js/shortcuts.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ function scrollHeaders() {
3333
const headers = Array.from(
3434
document.querySelectorAll(":is(h1, h2, h3, h4, h5, h6)[id]"),
3535
);
36-
const allShortcuts = Array.from(
37-
document.querySelectorAll("#shortcuts > div"),
38-
);
36+
const shortcuts = document.getElementById("shortcuts");
37+
38+
// Exit early if shortcuts container doesn't exist
39+
if (!shortcuts) return;
40+
41+
const allShortcuts = Array.from(shortcuts.querySelectorAll("div"));
3942

4043
headers.map((currentSection) => {
4144
// get the position of the section
@@ -81,6 +84,18 @@ function remToPx(rem) {
8184
}
8285

8386
function setupShortcuts(shortcutDepth = 2) {
87+
// Find the shortcut target container in the sidebar
88+
const shortcutsTarget = document.getElementById("shortcuts");
89+
90+
/*
91+
* Exit early if shortcuts container doesn't exist
92+
* This is important to avoid errors when the theme
93+
* is used on pages that don't have the shortcuts sidebar
94+
*/
95+
if (!shortcutsTarget) {
96+
return;
97+
}
98+
8499
shortcutDepth += 1; // to account for the page title
85100

86101
// Build a class selector for each header type, and concatenate with commas
@@ -92,11 +107,25 @@ function setupShortcuts(shortcutDepth = 2) {
92107
classes += " .content-container :not([role='tabpanel']) > h" + i;
93108
}
94109

95-
// Content Page Shortcuts
96-
const shortcutsTarget = document.getElementById("shortcuts");
97-
if (shortcutsTarget) {
98-
const classElements = Array.from(document.querySelectorAll(classes));
110+
/*
111+
* Add selectors for branch bundles (_index.md files). In this case,
112+
* branch bundles often have a different DOM structure than leaf bundles,
113+
* so we need to include headers that are direct children of the content
114+
* container here.
115+
*/
116+
for (let i = 2; i <= shortcutDepth; i++) {
117+
classes += ", .content-container > h" + i;
118+
}
119+
120+
const classElements = Array.from(document.querySelectorAll(classes));
121+
122+
// Only proceed if we found headers (to avoid creating an empty shortcuts section)
123+
if (classElements.length > 0) {
99124
classElements.map((el) => {
125+
if (!el.id) {
126+
return;
127+
}
128+
100129
const title = el.innerHTML;
101130
const elId = el.id;
102131
// Gets the element type (e.g. h2, h3)
@@ -144,6 +173,11 @@ function setupShortcuts(shortcutDepth = 2) {
144173
if (shortcutsContainer) {
145174
shortcutsContainer.style.display = "none";
146175
}
176+
} else {
177+
const shortcutsContainer = document.getElementById("shortcuts-container");
178+
if (shortcutsContainer && shortcutsContainer.style.display === "none") {
179+
shortcutsContainer.style.display = "";
180+
}
147181
}
148182

149183
bindScroll();

0 commit comments

Comments
 (0)