From 47ba2ab451028a541657ba314e8066487bc28f3c Mon Sep 17 00:00:00 2001 From: Congon4tor Date: Fri, 3 Jan 2025 03:30:44 +0100 Subject: [PATCH] Fix page index with links --- theme/pagetoc.js | 96 ++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/theme/pagetoc.js b/theme/pagetoc.js index 5962db9f3..a51ea29ae 100644 --- a/theme/pagetoc.js +++ b/theme/pagetoc.js @@ -1,68 +1,76 @@ -let scrollTimeout; +let scrollTimeout const listenActive = () => { - const elems = document.querySelector(".pagetoc").children; - [...elems].forEach(el => { + const elems = document.querySelector(".pagetoc").children + ;[...elems].forEach((el) => { el.addEventListener("click", (event) => { - clearTimeout(scrollTimeout); - [...elems].forEach(el => el.classList.remove("active")); - el.classList.add("active"); + clearTimeout(scrollTimeout) + ;[...elems].forEach((el) => el.classList.remove("active")) + el.classList.add("active") // Prevent scroll updates for a short period scrollTimeout = setTimeout(() => { - scrollTimeout = null; - }, 100); // Adjust timing as needed - }); - }); -}; + scrollTimeout = null + }, 100) // Adjust timing as needed + }) + }) +} -const getPagetoc = () => document.querySelector(".pagetoc") || autoCreatePagetoc(); +const getPagetoc = () => + document.querySelector(".pagetoc") || autoCreatePagetoc() const autoCreatePagetoc = () => { - const main = document.querySelector("#content > main"); + const main = document.querySelector("#content > main") const content = Object.assign(document.createElement("div"), { - className: "content-wrap" - }); - content.append(...main.childNodes); - main.prepend(content); - main.insertAdjacentHTML("afterbegin", '
'); - return document.querySelector(".pagetoc"); -}; + className: "content-wrap", + }) + content.append(...main.childNodes) + main.prepend(content) + main.insertAdjacentHTML( + "afterbegin", + '
' + ) + return document.querySelector(".pagetoc") +} const updateFunction = () => { - if (scrollTimeout) return; // Skip updates if within the cooldown period from a click - const headers = [...document.getElementsByClassName("header")]; - const scrolledY = window.scrollY; - let lastHeader = null; + if (scrollTimeout) return // Skip updates if within the cooldown period from a click + const headers = [...document.getElementsByClassName("header")] + const scrolledY = window.scrollY + let lastHeader = null // Find the last header that is above the current scroll position for (let i = headers.length - 1; i >= 0; i--) { if (scrolledY >= headers[i].offsetTop) { - lastHeader = headers[i]; - break; + lastHeader = headers[i] + break } } - const pagetocLinks = [...document.querySelector(".pagetoc").children]; - pagetocLinks.forEach(link => link.classList.remove("active")); + const pagetocLinks = [...document.querySelector(".pagetoc").children] + pagetocLinks.forEach((link) => link.classList.remove("active")) if (lastHeader) { - const activeLink = pagetocLinks.find(link => lastHeader.href === link.href); - if (activeLink) activeLink.classList.add("active"); + const activeLink = pagetocLinks.find( + (link) => lastHeader.href === link.href + ) + if (activeLink) activeLink.classList.add("active") } -}; +} -window.addEventListener('load', () => { - const pagetoc = getPagetoc(); - const headers = [...document.getElementsByClassName("header")]; - headers.forEach(header => { +window.addEventListener("load", () => { + const pagetoc = getPagetoc() + const headers = [...document.getElementsByClassName("header")] + headers.forEach((header) => { const link = Object.assign(document.createElement("a"), { textContent: header.text, href: header.href, - className: `pagetoc-${header.parentElement.tagName}` - }); - pagetoc.appendChild(link); - }); - updateFunction(); - listenActive(); - window.addEventListener("scroll", updateFunction); -}); - + className: `pagetoc-${header.parentElement.tagName}`, + }) + if (header.parentElement.querySelectorAll("a").length === 2) { + link.textContent = header.parentElement.querySelectorAll("a")[1].text + } + pagetoc.appendChild(link) + }) + updateFunction() + listenActive() + window.addEventListener("scroll", updateFunction) +})