Skip to content

Commit 72fc92e

Browse files
authored
fix(navigation): align sidebar and breadcrumbs with active topic (#32)
* fix(navigation): reveal active sidebar topic * fix(navigation): use readable breadcrumb labels
1 parent 8169995 commit 72fc92e

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

build.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,26 @@ function extractSections(html) {
245245

246246
function buildBreadcrumb(pagePath) {
247247
const parts = pagePath.split("/");
248+
const topic = manifest.topics.find((candidate) =>
249+
candidate.pages.some((page) => page.path === pagePath),
250+
);
251+
const page = topic && topic.pages.find((candidate) => candidate.path === pagePath);
252+
const labels = parts.map((segment) =>
253+
segment
254+
.split("-")
255+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
256+
.join(" "),
257+
);
258+
259+
if (topic) labels[0] = topic.name;
260+
if (page) labels[labels.length - 1] = page.title;
261+
248262
const out = [];
249-
parts.forEach((seg, i) => {
250-
const last = i === parts.length - 1;
263+
labels.forEach((label, i) => {
264+
const last = i === labels.length - 1;
251265
// Parent crumbs + separators drop away on narrow screens; the current page always stays.
252266
if (i > 0) out.push(`<span class="crumb-hide">/</span>`);
253-
out.push(`<span class="${last ? "here" : "crumb-hide"}">${seg}</span>`);
267+
out.push(`<span class="${last ? "here" : "crumb-hide"}">${label}</span>`);
254268
});
255269
return out.join("");
256270
}

src/shell.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@
166166
var collapsedNavGroups = readCollapsedNavGroups();
167167
document.querySelectorAll(".nav-group-toggle").forEach(function (btn) {
168168
var key = btn.dataset.navKey;
169-
if (key && collapsedNavGroups.indexOf(key) !== -1) {
169+
var group = btn.closest(".nav-group");
170+
var containsActiveLink = group && group.querySelector("a.active");
171+
if (key && collapsedNavGroups.indexOf(key) !== -1 && !containsActiveLink) {
170172
btn.setAttribute("aria-expanded", "false");
171173
}
172174

@@ -196,6 +198,21 @@
196198
});
197199
});
198200

201+
function centerActiveSidebarLink() {
202+
if (!sidebar) return;
203+
var activeLink = sidebar.querySelector(".nav-group a.active");
204+
if (!activeLink) return;
205+
206+
var sidebarRect = sidebar.getBoundingClientRect();
207+
var activeRect = activeLink.getBoundingClientRect();
208+
var activeTop = activeRect.top - sidebarRect.top + sidebar.scrollTop;
209+
var targetTop = activeTop - (sidebar.clientHeight - activeRect.height) / 2;
210+
var maxScrollTop = Math.max(0, sidebar.scrollHeight - sidebar.clientHeight);
211+
sidebar.scrollTop = Math.min(Math.max(0, targetTop), maxScrollTop);
212+
}
213+
214+
if (sidebar) window.requestAnimationFrame(centerActiveSidebarLink);
215+
199216
var toTop = document.getElementById("toTop");
200217
if (toTop) {
201218
var onScroll = function () {

0 commit comments

Comments
 (0)