Skip to content

Commit 6d4e297

Browse files
committed
update docs/themepagetoc.js per advice from @coderabbitai
1 parent 9bfab31 commit 6d4e297

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

docs/theme/pagetoc.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,31 @@ const updateFunction = () => {
5959
if (activeLink) activeLink.classList.add("active");
6060
}
6161
};
62+
function getHeaderText(header) {
63+
let text = header.textContent;
64+
if (text === "") {
65+
let sibling = header.nextSibling;
66+
let maxIterations = 100;
67+
while (sibling != null && maxIterations > 0) {
68+
text += sibling.textContent;
69+
sibling = sibling.nextSibling;
70+
maxIterations--;
71+
}
72+
if (maxIterations === 0) {
73+
console.warn(
74+
"Possible circular reference in DOM when extracting header text"
75+
);
76+
}
77+
}
78+
return text;
79+
}
6280

6381
const onLoad = () => {
6482
const pagetoc = getPagetoc();
6583
var headers = [...document.getElementsByClassName("header")];
6684
headers.shift();
6785
headers.forEach((header) => {
68-
var text = header.textContent;
69-
if (text === "") {
70-
sibling = header.nextSibling
71-
while (sibling != null) {
72-
text += sibling.textContent;
73-
sibling = sibling.nextSibling
74-
}
75-
}
86+
const text = getHeaderText(header);
7687
const link = Object.assign(document.createElement("a"), {
7788
textContent: text,
7889
href: header.href,

0 commit comments

Comments
 (0)