Skip to content

Commit 68e3572

Browse files
committed
Fix inadvertently broken folding behavior
1 parent 27ab7eb commit 68e3572

File tree

4 files changed

+65
-60
lines changed

4 files changed

+65
-60
lines changed

src/theme/book.js

-11
Original file line numberDiff line numberDiff line change
@@ -463,17 +463,6 @@ function playground_text(playground, hidden = true) {
463463
try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { }
464464
}
465465

466-
467-
var sidebarAnchorToggles = document.querySelectorAll('#sidebar a.toggle');
468-
469-
function toggleSection(ev) {
470-
ev.currentTarget.parentElement.classList.toggle('expanded');
471-
}
472-
473-
Array.from(sidebarAnchorToggles).forEach(function (el) {
474-
el.addEventListener('click', toggleSection);
475-
});
476-
477466
function hideSidebar() {
478467
body.classList.remove('sidebar-visible')
479468
body.classList.add('sidebar-hidden');

src/theme/index.hbs

+7-7
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@
5252
<!-- MathJax -->
5353
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
5454
{{/if}}
55-
</head>
56-
<body>
57-
<div id="body-container">
55+
5856
<!-- Provide site root to javascript -->
5957
<script>
6058
var path_to_root = "{{ path_to_root }}";
6159
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "{{ preferred_dark_theme }}" : "{{ default_theme }}";
6260
</script>
63-
61+
<!-- Start loading toc.js asap -->
62+
<script src="{{ path_to_root }}toc.js"></script>
63+
</head>
64+
<body>
65+
<div id="body-container">
6466
<!-- Work around some values being stored in localStorage wrapped in quotes -->
6567
<script>
6668
try {
@@ -107,7 +109,7 @@
107109

108110
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
109111
<!-- populated by js -->
110-
<div class="sidebar-scrollbox"></div>
112+
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
111113
<noscript>
112114
<iframe class="sidebar-iframe-outer" src="{{ path_to_root }}toc.html"></iframe>
113115
</noscript>
@@ -116,8 +118,6 @@
116118
</div>
117119
</nav>
118120

119-
<script async src="{{ path_to_root }}toc.js"></script>
120-
121121
<div id="page-wrapper" class="page-wrapper">
122122

123123
<div class="page">

src/theme/toc.js.hbs

+57-41
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,68 @@
33
// This is a script, and not included directly in the page, to control the total size of the book.
44
// The TOC contains an entry for each page, so if each page includes a copy of the TOC,
55
// the total size of the page becomes O(n**2).
6-
var sidebarScrollbox = document.querySelector("#sidebar .sidebar-scrollbox");
7-
sidebarScrollbox.innerHTML = '{{#toc}}{{/toc}}';
8-
(function() {
9-
let current_page = document.location.href.toString();
10-
if (current_page.endsWith("/")) {
11-
current_page += "index.html";
6+
class MDBookSidebarScrollbox extends HTMLElement {
7+
constructor() {
8+
super();
129
}
13-
var links = sidebarScrollbox.querySelectorAll("a");
14-
var l = links.length;
15-
for (var i = 0; i < l; ++i) {
16-
var link = links[i];
17-
var href = link.getAttribute("href");
18-
if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) {
19-
link.href = path_to_root + href;
10+
connectedCallback() {
11+
this.innerHTML = '{{#toc}}{{/toc}}';
12+
// Set the current, active page, and reveal it if it's hidden
13+
let current_page = document.location.href.toString();
14+
if (current_page.endsWith("/")) {
15+
current_page += "index.html";
2016
}
21-
// The "index" page is supposed to alias the first chapter in the book.
22-
if (link.href === current_page || (i === 0 && path_to_root === "" && current_page.endsWith("/index.html"))) {
23-
link.classList.add("active");
24-
var parent = link.parentElement;
25-
while (parent) {
26-
if (parent.tagName === "LI" && parent.previousElementSibling) {
27-
if (parent.previousElementSibling.classList.contains("chapter-item")) {
28-
parent.previousElementSibling.classList.add("expanded");
17+
var links = Array.prototype.slice.call(this.querySelectorAll("a"));
18+
var l = links.length;
19+
for (var i = 0; i < l; ++i) {
20+
var link = links[i];
21+
var href = link.getAttribute("href");
22+
if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) {
23+
link.href = path_to_root + href;
24+
}
25+
// 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"))) {
27+
link.classList.add("active");
28+
var parent = link.parentElement;
29+
if (parent && parent.classList.contains("chapter-item")) {
30+
parent.classList.add("expanded");
31+
}
32+
while (parent) {
33+
if (parent.tagName === "LI" && parent.previousElementSibling) {
34+
if (parent.previousElementSibling.classList.contains("chapter-item")) {
35+
parent.previousElementSibling.classList.add("expanded");
36+
}
2937
}
38+
parent = parent.parentElement;
3039
}
31-
parent = parent.parentElement;
3240
}
3341
}
34-
}
35-
})();
36-
37-
// Track and set sidebar scroll position
38-
sidebarScrollbox.addEventListener('click', function(e) {
39-
if (e.target.tagName === 'A') {
40-
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
41-
}
42-
}, { passive: true });
43-
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
44-
sessionStorage.removeItem('sidebar-scroll');
45-
if (sidebarScrollTop) {
46-
// preserve sidebar scroll position when navigating via links within sidebar
47-
sidebarScrollbox.scrollTop = sidebarScrollTop;
48-
} else {
49-
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
50-
var activeSection = document.querySelector('#sidebar .active');
51-
if (activeSection) {
52-
activeSection.scrollIntoView({ block: 'center' });
42+
// Track and set sidebar scroll position
43+
this.addEventListener('click', function(e) {
44+
if (e.target.tagName === 'A') {
45+
sessionStorage.setItem('sidebar-scroll', this.scrollTop);
46+
}
47+
}, { passive: true });
48+
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
49+
sessionStorage.removeItem('sidebar-scroll');
50+
if (sidebarScrollTop) {
51+
// preserve sidebar scroll position when navigating via links within sidebar
52+
this.scrollTop = sidebarScrollTop;
53+
} else {
54+
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
55+
var activeSection = document.querySelector('#sidebar .active');
56+
if (activeSection) {
57+
activeSection.scrollIntoView({ block: 'center' });
58+
}
59+
}
60+
// Toggle buttons
61+
var sidebarAnchorToggles = document.querySelectorAll('#sidebar a.toggle');
62+
function toggleSection(ev) {
63+
ev.currentTarget.parentElement.classList.toggle('expanded');
64+
}
65+
Array.from(sidebarAnchorToggles).forEach(function (el) {
66+
el.addEventListener('click', toggleSection);
67+
});
5368
}
5469
}
70+
window.customElements.define("mdbook-sidebar-scrollbox", MDBookSidebarScrollbox);

tests/rendered_output.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ fn toc_js_html() -> Result<Document> {
243243
let toc_path = temp.path().join("book").join("toc.js");
244244
let html = fs::read_to_string(toc_path).with_context(|| "Unable to read index.html")?;
245245
for line in html.lines() {
246-
if let Some(left) = line.strip_prefix("sidebarScrollbox.innerHTML = '") {
246+
if let Some(left) = line.strip_prefix(" this.innerHTML = '") {
247247
if let Some(html) = left.strip_suffix("';") {
248248
return Ok(Document::from(html));
249249
}

0 commit comments

Comments
 (0)