|
| 1 | +document.addEventListener("DOMContentLoaded", function () { |
| 2 | + // Check if current page has opted out of the TOC |
| 3 | + if (document.body.classList.contains("no-right-toc")) { |
| 4 | + return; |
| 5 | + } |
| 6 | + |
| 7 | + const content = document.querySelector(".rst-content"); |
| 8 | + if (!content) return; |
| 9 | + |
| 10 | + // Find all headers in the main content |
| 11 | + const headers = Array.from( |
| 12 | + content.querySelectorAll("h1:not(.document-title), h2, h3"), |
| 13 | + ).filter((header) => !header.classList.contains("no-toc")); |
| 14 | + |
| 15 | + // Only create TOC if there are headers |
| 16 | + if (headers.length === 0) return; |
| 17 | + |
| 18 | + // Create TOC container |
| 19 | + const toc = document.createElement("div"); |
| 20 | + toc.className = "right-toc"; |
| 21 | + toc.innerHTML = |
| 22 | + '<div class="right-toc-header">' + |
| 23 | + '<div class="right-toc-title">On This Page</div>' + |
| 24 | + '<div class="right-toc-buttons">' + |
| 25 | + '<button class="right-toc-toggle-btn" title="Toggle TOC visibility">−</button>' + |
| 26 | + "</div></div>" + |
| 27 | + '<div class="right-toc-content"><ul class="right-toc-list"></ul></div>'; |
| 28 | + |
| 29 | + const tocList = toc.querySelector(".right-toc-list"); |
| 30 | + const tocContent = toc.querySelector(".right-toc-content"); |
| 31 | + const tocToggleBtn = toc.querySelector( |
| 32 | + ".right-toc-toggle-btn", |
| 33 | + ); |
| 34 | + |
| 35 | + // Set up the toggle button |
| 36 | + tocToggleBtn.addEventListener("click", function () { |
| 37 | + if (tocContent.style.display === "none") { |
| 38 | + tocContent.style.display = "block"; |
| 39 | + tocToggleBtn.textContent = "−"; |
| 40 | + toc.classList.remove("right-toc-collapsed"); |
| 41 | + localStorage.setItem("tocVisible", "true"); |
| 42 | + } else { |
| 43 | + tocContent.style.display = "none"; |
| 44 | + tocToggleBtn.textContent = "+"; |
| 45 | + toc.classList.add("right-toc-collapsed"); |
| 46 | + localStorage.setItem("tocVisible", "false"); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + // Check saved state |
| 51 | + if (localStorage.getItem("tocVisible") === "false") { |
| 52 | + tocContent.style.display = "none"; |
| 53 | + tocToggleBtn.textContent = "+"; |
| 54 | + toc.classList.add("right-toc-collapsed"); |
| 55 | + } |
| 56 | + |
| 57 | + // Track used IDs to avoid duplicates |
| 58 | + const usedIds = new Set(); |
| 59 | + |
| 60 | + // Get all existing IDs in the document |
| 61 | + document.querySelectorAll("[id]").forEach((el) => { |
| 62 | + usedIds.add(el.id); |
| 63 | + }); |
| 64 | + |
| 65 | + // Generate unique IDs for headers that need them |
| 66 | + headers.forEach((header, index) => { |
| 67 | + // If header already has a unique ID, use that |
| 68 | + if (header.id && !usedIds.has(header.id)) { |
| 69 | + usedIds.add(header.id); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Create a slug from the header text |
| 74 | + let headerText = header.textContent || ""; |
| 75 | + |
| 76 | + // Clean the text (remove icons and special characters) |
| 77 | + headerText = headerText.replace(/\s*\uf0c1\s*$/, ""); |
| 78 | + headerText = headerText.replace(/\s*[¶§#†‡]\s*$/, ""); |
| 79 | + headerText = headerText.trim(); |
| 80 | + |
| 81 | + let slug = headerText |
| 82 | + .toLowerCase() |
| 83 | + .replace(/[^\w\s-]/g, "") |
| 84 | + .replace(/\s+/g, "-") |
| 85 | + .replace(/--+/g, "-") |
| 86 | + .trim(); |
| 87 | + |
| 88 | + // Make sure slug is not empty |
| 89 | + if (!slug) { |
| 90 | + slug = "section"; |
| 91 | + } |
| 92 | + |
| 93 | + // Ensure the ID is unique |
| 94 | + let uniqueId = slug; |
| 95 | + let counter = 1; |
| 96 | + |
| 97 | + while (usedIds.has(uniqueId)) { |
| 98 | + uniqueId = `${slug}-${counter}`; |
| 99 | + counter++; |
| 100 | + } |
| 101 | + |
| 102 | + // Set the unique ID and add to our tracking set |
| 103 | + header.id = uniqueId; |
| 104 | + usedIds.add(uniqueId); |
| 105 | + }); |
| 106 | + |
| 107 | + // Add entries for each header |
| 108 | + headers.forEach((header) => { |
| 109 | + const item = document.createElement("li"); |
| 110 | + const link = document.createElement("a"); |
| 111 | + |
| 112 | + link.href = "#" + header.id; |
| 113 | + |
| 114 | + // Get clean text without icons |
| 115 | + let headerText = header.textContent || ""; |
| 116 | + headerText = headerText.replace(/\s*\uf0c1\s*$/, ""); |
| 117 | + headerText = headerText.replace(/\s*[¶§#†‡]\s*$/, ""); |
| 118 | + |
| 119 | + link.textContent = headerText.trim(); |
| 120 | + link.className = |
| 121 | + "right-toc-link right-toc-level-" + |
| 122 | + header.tagName.toLowerCase(); |
| 123 | + |
| 124 | + item.appendChild(link); |
| 125 | + tocList.appendChild(item); |
| 126 | + }); |
| 127 | + |
| 128 | + // Add TOC to page |
| 129 | + document.body.appendChild(toc); |
| 130 | + |
| 131 | + // Add active link highlighting |
| 132 | + const tocLinks = document.querySelectorAll(".right-toc-link"); |
| 133 | + const headerElements = Array.from(headers); |
| 134 | + |
| 135 | + if (tocLinks.length > 0 && headerElements.length > 0) { |
| 136 | + // Highlight the current section on scroll |
| 137 | + window.addEventListener( |
| 138 | + "scroll", |
| 139 | + debounce(function () { |
| 140 | + let currentSection = null; |
| 141 | + let smallestDistanceFromTop = Infinity; |
| 142 | + |
| 143 | + headerElements.forEach((header) => { |
| 144 | + const distance = Math.abs( |
| 145 | + header.getBoundingClientRect().top, |
| 146 | + ); |
| 147 | + if (distance < smallestDistanceFromTop) { |
| 148 | + smallestDistanceFromTop = distance; |
| 149 | + currentSection = header.id; |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + tocLinks.forEach((link) => { |
| 154 | + link.classList.remove("active"); |
| 155 | + if ( |
| 156 | + link.getAttribute("href") === `#${currentSection}` |
| 157 | + ) { |
| 158 | + link.classList.add("active"); |
| 159 | + } |
| 160 | + }); |
| 161 | + }, 100), |
| 162 | + ); |
| 163 | + } |
| 164 | +}); |
| 165 | + |
| 166 | +// Debounce function to limit scroll event firing |
| 167 | +function debounce(func, wait) { |
| 168 | + let timeout; |
| 169 | + return function () { |
| 170 | + const context = this; |
| 171 | + const args = arguments; |
| 172 | + clearTimeout(timeout); |
| 173 | + timeout = setTimeout(() => func.apply(context, args), wait); |
| 174 | + }; |
| 175 | +} |
0 commit comments