Fix chapter navigation links

This commit is contained in:
Carlos Polop
2026-07-05 16:55:32 +02:00
parent e1ed994783
commit 30f0e6f3e3
2 changed files with 88 additions and 14 deletions
+6 -14
View File
@@ -380,33 +380,25 @@
<nav class="nav-wrapper" aria-label="Page navigation"> <nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons --> <!-- Mobile navigation buttons -->
{{#if previous}} <a rel="prev" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left" hidden>
<a rel="prev" href="{{ path_to_root }}{{previous.path}}" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i> <i class="fa fa-angle-left"></i>
</a> </a>
{{/if}}
{{#if next}} <a rel="next prefetch" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right" hidden>
<a rel="next prefetch" href="{{ path_to_root }}{{next.path}}" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i> <i class="fa fa-angle-right"></i>
</a> </a>
{{/if}}
<div style="clear: both"></div> <div style="clear: both"></div>
</nav> </nav>
<nav class="nav-wide-wrapper" aria-label="Page navigation"> <nav class="nav-wide-wrapper" aria-label="Page navigation">
{{#if previous}} <a rel="prev" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left" hidden>
<a rel="prev" href="{{ path_to_root }}{{previous.path}}" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> <i class="fa fa-angle-left"></i><span class="chapter-nav-label" style="font-size: medium; align-self: center; margin-left: 10px;"></span>
<i class="fa fa-angle-left"></i><span style="font-size: medium; align-self: center; margin-left: 10px;">{{previous.name}}</span>
</a> </a>
{{/if}}
{{#if next}} <a rel="next prefetch" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right" hidden>
<a rel="next prefetch" href="{{ path_to_root }}{{next.path}}" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> <span class="chapter-nav-label" style="font-size: medium; align-self: center; margin-right: 10px;"></span><i class="fa fa-angle-right"></i>
<span style="font-size: medium; align-self: center; margin-right: 10px;">{{next.name}}</span><i class="fa fa-angle-right"></i>
</a> </a>
{{/if}}
</nav> </nav>
</div> </div>
+82
View File
@@ -42,6 +42,7 @@ class MDBookSidebarScrollbox extends HTMLElement {
} }
} }
} }
updateChapterNavigation(links);
// Track and set sidebar scroll position // Track and set sidebar scroll position
this.addEventListener('click', function(e) { this.addEventListener('click', function(e) {
if (e.target.tagName === 'A') { if (e.target.tagName === 'A') {
@@ -83,3 +84,84 @@ class MDBookSidebarScrollbox extends HTMLElement {
} }
} }
window.customElements.define("mdbook-sidebar-scrollbox", MDBookSidebarScrollbox); window.customElements.define("mdbook-sidebar-scrollbox", MDBookSidebarScrollbox);
function normalizeChapterUrl(url) {
try {
var normalized = new URL(url, document.location.href);
normalized.hash = "";
normalized.search = "";
if (normalized.pathname.endsWith("/")) {
normalized.pathname += "index.html";
}
return normalized.href;
} catch (_) {
return "";
}
}
function setChapterNavigationLink(selector, chapter) {
var link = document.querySelector(selector);
if (!link) {
return;
}
if (!chapter) {
link.hidden = true;
link.removeAttribute("href");
return;
}
link.href = chapter.href;
link.hidden = false;
var label = link.querySelector(".chapter-nav-label") || link.querySelector("span");
if (label) {
label.textContent = chapter.title;
}
}
function updateChapterNavigation(links) {
var current = normalizeChapterUrl(document.location.href);
var seen = Object.create(null);
var chapters = [];
links.forEach(function (link) {
var rawHref = link.getAttribute("href");
if (!rawHref || rawHref.startsWith("#") || link.classList.contains("external-link")) {
return;
}
var url;
try {
url = new URL(link.href, document.location.href);
} catch (_) {
return;
}
if (url.origin !== document.location.origin && url.protocol !== "file:") {
return;
}
var normalized = normalizeChapterUrl(url.href);
if (!normalized || seen[normalized]) {
return;
}
seen[normalized] = true;
chapters.push({
href: link.href,
key: normalized,
title: link.textContent.trim()
});
});
var index = chapters.findIndex(function (chapter) {
return chapter.key === current;
});
var previous = index > 0 ? chapters[index - 1] : null;
var next = index >= 0 && index < chapters.length - 1 ? chapters[index + 1] : null;
setChapterNavigationLink(".mobile-nav-chapters.previous", previous);
setChapterNavigationLink(".nav-chapters.previous", previous);
setChapterNavigationLink(".mobile-nav-chapters.next", next);
setChapterNavigationLink(".nav-chapters.next", next);
}