-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2414 from notriddle/on2
Load the sidebar toc from a shared JS file or iframe
- Loading branch information
Showing
9 changed files
with
272 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<!DOCTYPE HTML> | ||
<html lang="{{ language }}" class="{{ default_theme }}" dir="{{ text_direction }}"> | ||
<head> | ||
<!-- sidebar iframe generated using mdBook | ||
This is a frame, and not included directly in the page, to control the total size of the | ||
book. The TOC contains an entry for each page, so if each page includes a copy of the TOC, | ||
the total size of the page becomes O(n**2). | ||
The frame is only used as a fallback when JS is turned off. When it's on, the sidebar is | ||
instead added to the main page by `toc.js` instead. The JavaScript mode is better | ||
because, when running in a `file:///` URL, the iframed page would not be Same-Origin as | ||
the rest of the page, so the sidebar and the main page theme would fall out of sync. | ||
--> | ||
<meta charset="UTF-8"> | ||
<meta name="robots" content="noindex"> | ||
{{#if base_url}} | ||
<base href="{{ base_url }}"> | ||
{{/if}} | ||
<!-- Custom HTML head --> | ||
{{> head}} | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="theme-color" content="#ffffff"> | ||
<link rel="stylesheet" href="{{ path_to_root }}css/variables.css"> | ||
<link rel="stylesheet" href="{{ path_to_root }}css/general.css"> | ||
<link rel="stylesheet" href="{{ path_to_root }}css/chrome.css"> | ||
{{#if print_enable}} | ||
<link rel="stylesheet" href="{{ path_to_root }}css/print.css" media="print"> | ||
{{/if}} | ||
<!-- Fonts --> | ||
<link rel="stylesheet" href="{{ path_to_root }}FontAwesome/css/font-awesome.css"> | ||
{{#if copy_fonts}} | ||
<link rel="stylesheet" href="{{ path_to_root }}fonts/fonts.css"> | ||
{{/if}} | ||
<!-- Custom theme stylesheets --> | ||
{{#each additional_css}} | ||
<link rel="stylesheet" href="{{ ../path_to_root }}{{ this }}"> | ||
{{/each}} | ||
</head> | ||
<body class="sidebar-iframe-inner"> | ||
{{#toc}}{{/toc}} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Populate the sidebar | ||
// | ||
// This is a script, and not included directly in the page, to control the total size of the book. | ||
// The TOC contains an entry for each page, so if each page includes a copy of the TOC, | ||
// the total size of the page becomes O(n**2). | ||
var sidebarScrollbox = document.querySelector("#sidebar .sidebar-scrollbox"); | ||
sidebarScrollbox.innerHTML = '{{#toc}}{{/toc}}'; | ||
(function() { | ||
let current_page = document.location.href.toString(); | ||
if (current_page.endsWith("/")) { | ||
current_page += "index.html"; | ||
} | ||
var links = sidebarScrollbox.querySelectorAll("a"); | ||
var l = links.length; | ||
for (var i = 0; i < l; ++i) { | ||
var link = links[i]; | ||
var href = link.getAttribute("href"); | ||
if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) { | ||
link.href = path_to_root + href; | ||
} | ||
// The "index" page is supposed to alias the first chapter in the book. | ||
if (link.href === current_page || (i === 0 && path_to_root === "" && current_page.endsWith("/index.html"))) { | ||
link.classList.add("active"); | ||
var parent = link.parentElement; | ||
while (parent) { | ||
if (parent.tagName === "LI" && parent.previousElementSibling) { | ||
if (parent.previousElementSibling.classList.contains("chapter-item")) { | ||
parent.previousElementSibling.classList.add("expanded"); | ||
} | ||
} | ||
parent = parent.parentElement; | ||
} | ||
} | ||
} | ||
})(); | ||
|
||
// Track and set sidebar scroll position | ||
sidebarScrollbox.addEventListener('click', function(e) { | ||
if (e.target.tagName === 'A') { | ||
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop); | ||
} | ||
}, { passive: true }); | ||
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll'); | ||
sessionStorage.removeItem('sidebar-scroll'); | ||
if (sidebarScrollTop) { | ||
// preserve sidebar scroll position when navigating via links within sidebar | ||
sidebarScrollbox.scrollTop = sidebarScrollTop; | ||
} else { | ||
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons | ||
var activeSection = document.querySelector('#sidebar .active'); | ||
if (activeSection) { | ||
activeSection.scrollIntoView({ block: 'center' }); | ||
} | ||
} |
Oops, something went wrong.