Files
hacktricks-cloud/theme/toc.js.hbs
2026-01-26 16:08:11 +01:00

86 lines
3.9 KiB
Handlebars

// Populate the sidebar
//
// This is a script, and not included directly in the page, to control the total size of the book.
// The TOC contains an entry for each page, so if each page includes a copy of the TOC,
// the total size of the page becomes O(n**2).
class MDBookSidebarScrollbox extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
// Modify TOC to support external links
var toc = '{{#toc}}{{/toc}}';
// Handle both old mdbook (<div>) and new mdbook 0.5 (<span>) formats
// External links open in new tab with external link icon
toc = toc.replace(/<(div|span)>([^$<>]*)\$\$external:([^$<>]*)\$\$<\/(div|span)>/g, '<a class="external-link" href="$3" target="_blank" rel="noopener noreferrer">$2 ↗</a>')
this.innerHTML = toc
// Set the current, active page, and reveal it if it's hidden
let current_page = document.location.href.toString();
if (current_page.endsWith("/")) {
current_page += "index.html";
}
var links = Array.prototype.slice.call(this.querySelectorAll("a"));
var l = links.length;
for (var i = 0; i < l; ++i) {
var link = links[i];
var href = link.getAttribute("href");
if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) {
link.href = path_to_root + href;
}
// The "index" page is supposed to alias the first chapter in the book.
if (link.href === current_page || (i === 0 && path_to_root === "" && current_page.endsWith("/index.html"))) {
link.classList.add("active");
var parent = link.parentElement;
if (parent && parent.classList.contains("chapter-item")) {
parent.classList.add("expanded");
}
while (parent) {
if (parent.tagName === "LI" && parent.classList.contains("chapter-item")) {
parent.classList.add("expanded");
}
parent = parent.parentElement;
}
}
}
// Track and set sidebar scroll position
this.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
sessionStorage.setItem('sidebar-scroll', this.scrollTop);
}
}, { passive: true });
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
sessionStorage.removeItem('sidebar-scroll');
if (sidebarScrollTop) {
// preserve sidebar scroll position when navigating via links within sidebar
this.scrollTop = sidebarScrollTop;
} else {
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
var activeSection = document.querySelector('#sidebar .active');
if (activeSection) {
activeSection.scrollIntoView({ block: 'center' });
}
}
// Toggle buttons (delegate to avoid navigation)
var sidebarRoot = document.getElementById('sidebar');
if (sidebarRoot) {
sidebarRoot.addEventListener('click', function (ev) {
var toggle = ev.target.closest('a.chapter-fold-toggle');
if (!toggle) { return; }
ev.preventDefault();
ev.stopPropagation();
var parentLi = toggle.closest('li.chapter-item');
if (parentLi) {
parentLi.classList.toggle('expanded');
}
}, true);
sidebarRoot.addEventListener('pointerdown', function (ev) {
var toggle = ev.target.closest('a.chapter-fold-toggle');
if (!toggle) { return; }
ev.preventDefault();
ev.stopPropagation();
}, true);
}
}
}
window.customElements.define("mdbook-sidebar-scrollbox", MDBookSidebarScrollbox);