@@ -13,49 +13,75 @@ class MDBookSidebarScrollbox extends HTMLElement {
13
13
let current_page = document.location.href.toString().split("#")[0];
14
14
if (current_page.endsWith("/")) {
15
15
current_page += "index.html";
16
- } else if (!current_page.endsWith(".html")) {
17
- current_page += ".html";
18
16
}
19
17
var links = Array.prototype.slice.call(this.querySelectorAll("a"));
20
- var l = links.length;
21
18
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";
27
28
}
28
29
if (href.endsWith("/")) {
29
30
href += "index.html";
30
31
} else if (!href.endsWith(".html")) {
31
32
href += ".html";
32
33
}
33
- return current_page === href;
34
+ return current_page_for_comparison === href;
34
35
}
35
36
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;
36
55
for (var i = 0; i < l; ++i) {
37
56
var link = links[i];
38
57
var href = link.getAttribute("href");
39
58
if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) {
40
59
link.href = path_to_root + href;
41
60
}
42
61
// 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;
54
80
}
55
- parent = parent.parentElement;
56
81
}
57
82
}
58
83
}
84
+
59
85
// Track and set sidebar scroll position
60
86
this.addEventListener('click', function(e) {
61
87
if (e.target.tagName === 'A') {
0 commit comments