Skip to content

Commit 81a07a5

Browse files
committed
add more edge case
1 parent 75f6f6c commit 81a07a5

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

src/theme/toc.js.hbs

+47-21
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,75 @@ 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";
1816
}
1917
var links = Array.prototype.slice.call(this.querySelectorAll("a"));
20-
var l = links.length;
2118

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;
19+
function isCurrentPage(href, current_page_for_comparison) {
20+
if (!href) {
21+
return false;
22+
}
23+
if (!current_page_for_comparison) {
24+
current_page_for_comparison = current_page;
25+
}
26+
if (!current_page_for_comparison.endsWith(".html")) {
27+
current_page_for_comparison += ".html";
2728
}
2829
if (href.endsWith("/")) {
2930
href += "index.html";
3031
} else if (!href.endsWith(".html")) {
3132
href += ".html";
3233
}
33-
return current_page === href;
34+
return current_page_for_comparison === href;
3435
}
3536

37+
function setActivePage(link) {
38+
link.classList.add("active");
39+
var parent = link.parentElement;
40+
if (parent && parent.classList.contains("chapter-item")) {
41+
parent.classList.add("expanded");
42+
}
43+
while (parent) {
44+
if (parent.tagName === "LI" && parent.previousElementSibling) {
45+
if (parent.previousElementSibling.classList.contains("chapter-item")) {
46+
parent.previousElementSibling.classList.add("expanded");
47+
}
48+
}
49+
parent = parent.parentElement;
50+
}
51+
}
52+
53+
let foundActivePage = false;
54+
var l = links.length;
3655
for (var i = 0; i < l; ++i) {
3756
var link = links[i];
3857
var href = link.getAttribute("href");
3958
if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) {
4059
link.href = path_to_root + href;
4160
}
4261
// The "index" page is supposed to alias the first chapter in the book.
43-
if (isCurrentPage(i)) {
44-
link.classList.add("active");
45-
var parent = link.parentElement;
46-
if (parent && parent.classList.contains("chapter-item")) {
47-
parent.classList.add("expanded");
48-
}
49-
while (parent) {
50-
if (parent.tagName === "LI" && parent.previousElementSibling) {
51-
if (parent.previousElementSibling.classList.contains("chapter-item")) {
52-
parent.previousElementSibling.classList.add("expanded");
53-
}
62+
if (isCurrentPage(link.href) || (i === 0 && path_to_root === "" && current_page.endsWith("/index.html"))) {
63+
foundActivePage = true;
64+
setActivePage(link);
65+
}
66+
}
67+
68+
if (!foundActivePage) {
69+
// If the current page is not found, there is a possibility
70+
// that the service has redirected /foo/index.html to /foo (without the trailing slash)
71+
// try to find the active page again using this fallback
72+
if (!current_page.endsWith(".html") && !current_page.endsWith("/")) {
73+
let current_page_fallback = current_page + "/index.html";
74+
let l = links.length;
75+
for (let i = 0; i < l; ++i) {
76+
let link = links[i];
77+
if (isCurrentPage(link.href, current_page_fallback)) {
78+
setActivePage(link);
79+
break;
5480
}
55-
parent = parent.parentElement;
5681
}
5782
}
5883
}
84+
5985
// Track and set sidebar scroll position
6086
this.addEventListener('click', function(e) {
6187
if (e.target.tagName === 'A') {

0 commit comments

Comments
 (0)