2023-03-01 13:17:50 +00:00
|
|
|
const toc = document.getElementById("TableOfContents");
|
|
|
|
|
|
|
|
const getId = (el) => {
|
|
|
|
do {
|
|
|
|
const id = el.getAttribute("id");
|
|
|
|
if (id) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
el = el.previousElementSibling;
|
|
|
|
} while (el);
|
|
|
|
};
|
|
|
|
if (toc) {
|
|
|
|
if (toc.children.length < 1) {
|
|
|
|
toc.parentElement.remove();
|
|
|
|
} else {
|
|
|
|
const observer = new IntersectionObserver((entries) => {
|
2023-03-01 13:20:15 +00:00
|
|
|
const filtered = entries.filter(
|
|
|
|
(i) => getId(i.target) && i.intersectionRatio > 0
|
|
|
|
);
|
|
|
|
if (filtered.length > 0) {
|
|
|
|
toc.querySelector(`li.active`)?.classList.remove("active");
|
|
|
|
filtered.forEach((entry) => {
|
|
|
|
const id = getId(entry.target);
|
2023-03-01 13:17:50 +00:00
|
|
|
toc
|
|
|
|
.querySelector(`li a[href="#${id}"]`)
|
|
|
|
.parentElement.classList.add("active");
|
2023-03-01 13:20:15 +00:00
|
|
|
});
|
|
|
|
}
|
2023-03-01 13:17:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Track all sections that have an `id` applied
|
|
|
|
document.querySelectorAll(".copy *").forEach((section) => {
|
|
|
|
observer.observe(section);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|