Skip to content

Commit

Permalink
Fix visible headings sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
doup committed Dec 5, 2024
1 parent 4e48213 commit bdad1a0
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions static/on-this-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ let otp_state = new Map();
* @param {string | HTMLElement} id_or_node
*/
function otp_set_active(id_or_node){
let id = "";
if(typeof id_or_node == "object"){
id = id_or_node.getAttribute("id");
} else {
id = id_or_node;
}
id = "#" + id;
let id = `#${id_or_node instanceof HTMLElement ? id_or_node.getAttribute("id") : id_or_node}`;

document.querySelectorAll(".on-this-page a").forEach(a => {
a.setAttribute("data-active", a.getAttribute("href") == id);
});
Expand All @@ -39,10 +34,13 @@ let otp_observer = new IntersectionObserver(
entries.forEach(entry => {
otp_state.set(entry.target, entry.isIntersecting);
});

let intersecting = Array.from(otp_state)
.filter(([_el, inter]) => inter)
.map(([el, _inter]) => el);
intersecting.sort((element) => -element.getBoundingClientRect().y);

intersecting.sort((a, b) => a.getBoundingClientRect().y - b.getBoundingClientRect().y);

if (intersecting.length > 0) {
otp_set_active(intersecting[0]);
}
Expand Down

0 comments on commit bdad1a0

Please sign in to comment.