Skip to content

Commit 48310da

Browse files
committed
fix JS and CSS linting errors
1 parent 7cbda01 commit 48310da

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

docs/theme/pagetoc.css

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
21
@media only screen {
32
main {
43
position: relative;
54
}
5+
66
.sidetoc {
77
margin-left: auto;
88
margin-right: auto;
99
}
10+
1011
.pagetoc {
1112
width: auto;
1213
word-wrap: normal;
1314
}
15+
1416
.pagetoc a {
1517
color: var(--sidebar-fg) !important;
1618
display: block;
@@ -19,32 +21,41 @@
1921
text-decoration: none;
2022
margin-block-start: 0.6em;
2123
}
24+
2225
.pagetoc a:hover,
2326
.pagetoc a.active {
2427
color: var(--sidebar-active) !important;
2528
}
29+
2630
.pagetoc .active {
2731
color: var(--sidebar-active);
2832
}
33+
2934
.pagetoc .pagetoc-H2 {
3035
padding-left: 20px;
3136
}
37+
3238
.pagetoc .pagetoc-H3 {
3339
padding-left: 40px;
3440
}
41+
3542
.pagetoc .pagetoc-H4 {
3643
padding-left: 60px;
3744
}
45+
3846
.pagetoc .pagetoc-H5 {
3947
display: none;
4048
}
49+
4150
.pagetoc .pagetoc-H6 {
4251
display: none;
4352
}
53+
4454
ol.chapter a {
4555
color: var(--sidebar-fg);
4656
text-decoration: none;
4757
}
58+
4859
ol.chapter a.active {
4960
color: var(--sidebar-active);
5061
text-decoration: none;

docs/theme/pagetoc.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ let scrollTimeout;
22

33
const listenActive = () => {
44
const elems = document.querySelector(".pagetoc").children;
5-
[...elems].forEach(el => {
6-
el.addEventListener("click", (event) => {
5+
[...elems].forEach((el) => {
6+
el.addEventListener("click", () => {
77
clearTimeout(scrollTimeout);
8-
[...elems].forEach(el => el.classList.remove("active"));
8+
[...elems].forEach((el) => el.classList.remove("active"));
99
el.classList.add("active");
1010
// Prevent scroll updates for a short period
1111
scrollTimeout = setTimeout(() => {
@@ -15,16 +15,24 @@ const listenActive = () => {
1515
});
1616
};
1717

18-
const getPagetoc = () => document.querySelector(".pagetoc") || autoCreatePagetoc();
18+
const getPagetoc = () => {
19+
return document.querySelector(".pagetoc") || autoCreatePagetoc();
20+
};
1921

2022
const autoCreatePagetoc = () => {
21-
const chapter = document.querySelector("body nav#sidebar.sidebar li.chapter-item.expanded a.active");
23+
const chapter = document.querySelector(
24+
"body nav#sidebar.sidebar li.chapter-item.expanded a.active"
25+
);
2226
const content = Object.assign(document.createElement("div"), {
23-
className: "content-wrap"
27+
className: "content-wrap",
2428
});
2529
content.appendChild(chapter.cloneNode(true));
26-
const divAddedToc = Object.assign(document.createElement("div"), { className: "sidetoc" });
27-
const navAddedToc = Object.assign(document.createElement("nav"), { className: "pagetoc" });
30+
const divAddedToc = Object.assign(document.createElement("div"), {
31+
className: "sidetoc",
32+
});
33+
const navAddedToc = Object.assign(document.createElement("nav"), {
34+
className: "pagetoc",
35+
});
2836
divAddedToc.appendChild(navAddedToc);
2937
content.appendChild(divAddedToc);
3038
chapter.replaceWith(content);
@@ -35,34 +43,34 @@ const updateFunction = () => {
3543
if (scrollTimeout) return; // Skip updates if within the cooldown period from a click
3644
const headers = [...document.getElementsByClassName("header")];
3745
const scrolledY = window.scrollY;
38-
let lastHeader = null;
3946

4047
// Find the last header that is above the current scroll position
41-
for (let i = headers.length - 1; i >= 0; i--) {
42-
if (scrolledY >= headers[i].offsetTop) {
43-
lastHeader = headers[i];
44-
break;
45-
}
46-
}
48+
let headerOffsets = headers.filter((el) => {
49+
return scrolledY >= el.offsetTop;
50+
});
51+
const lastHeader = headerOffsets.reverse().shift();
4752

4853
const pagetocLinks = [...document.querySelector(".pagetoc").children];
49-
pagetocLinks.forEach(link => link.classList.remove("active"));
54+
pagetocLinks.forEach((link) => link.classList.remove("active"));
5055

5156
if (lastHeader) {
52-
const activeLink = pagetocLinks.find(link => lastHeader.href === link.href);
57+
const activeLink = pagetocLinks.find(
58+
(link) => lastHeader.href === link.href
59+
);
5360
if (activeLink) activeLink.classList.add("active");
5461
}
5562
};
5663

57-
window.addEventListener('load', () => {
64+
window.addEventListener("load", () => {
5865
const pagetoc = getPagetoc();
66+
console.log(pagetoc);
5967
var headers = [...document.getElementsByClassName("header")];
6068
headers.shift();
61-
headers.forEach(header => {
69+
headers.forEach((header) => {
6270
const link = Object.assign(document.createElement("a"), {
6371
textContent: header.text,
6472
href: header.href,
65-
className: `pagetoc-${header.parentElement.tagName}`
73+
className: `pagetoc-${header.parentElement.tagName}`,
6674
});
6775
pagetoc.appendChild(link);
6876
});

0 commit comments

Comments
 (0)