Skip to content

Commit 75f6f6c

Browse files
committed
make condition to add mark active page in toc looser
1 parent b0cf568 commit 75f6f6c

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/theme/toc.js.hbs

+18-1
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,34 @@ class MDBookSidebarScrollbox extends HTMLElement {
1313
let current_page = document.location.href.toString().split("#")[0];
1414
if (current_page.endsWith("/")) {
1515
current_page += "index.html";
16+
} else if (!current_page.endsWith(".html")) {
17+
current_page += ".html";
1618
}
1719
var links = Array.prototype.slice.call(this.querySelectorAll("a"));
1820
var l = links.length;
21+
22+
function isCurrentPage(i) {
23+
let href = links[i].href;
24+
// special case for root
25+
if (i === 0 && path_to_root === "" && current_page.endsWith("/index.html")) {
26+
return true;
27+
}
28+
if (href.endsWith("/")) {
29+
href += "index.html";
30+
} else if (!href.endsWith(".html")) {
31+
href += ".html";
32+
}
33+
return current_page === href;
34+
}
35+
1936
for (var i = 0; i < l; ++i) {
2037
var link = links[i];
2138
var href = link.getAttribute("href");
2239
if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) {
2340
link.href = path_to_root + href;
2441
}
2542
// The "index" page is supposed to alias the first chapter in the book.
26-
if (link.href === current_page || (i === 0 && path_to_root === "" && current_page.endsWith("/index.html"))) {
43+
if (isCurrentPage(i)) {
2744
link.classList.add("active");
2845
var parent = link.parentElement;
2946
if (parent && parent.classList.contains("chapter-item")) {

0 commit comments

Comments
 (0)