Skip to content

Commit

Permalink
fix: fix incorrect TOC highlight (fix #249)
Browse files Browse the repository at this point in the history
  • Loading branch information
saicaca committed Dec 7, 2024
1 parent e446283 commit cbf17e8
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/components/widget/TOC.astro
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,19 @@ class TableOfContents extends HTMLElement {

this.activeIndicator = document.getElementById("active-indicator");

this.sections = Array.from(
document.querySelectorAll("section")
);

this.tocEntries = Array.from(
document.querySelectorAll<HTMLAnchorElement>("#toc a[href^='#']")
);

this.sections = new Array(this.tocEntries.length);
this.headings = new Array(this.tocEntries.length);
for (let i = 0; i < this.tocEntries.length; i++) {
const id = decodeURIComponent(this.tocEntries[i].hash?.substring(1));
const section = document.getElementById(id)?.parentElement;
if (section instanceof HTMLElement) {
this.headings[i] = section;
const heading = document.getElementById(id);
const section = heading?.parentElement;
if (heading instanceof HTMLElement && section instanceof HTMLElement) {
this.headings[i] = heading;
this.sections[i] = section;
this.headingIdxMap.set(id, i);
}
}
Expand Down

0 comments on commit cbf17e8

Please sign in to comment.