Skip to content

Commit e91b6bd

Browse files
committed
docs(sidebar): Support path-aware active detection for sub-project links
why: Links with pathnames (e.g. /pytest-plugin/) never matched because the script only checked hostname. Sub-project links on the same domain need path prefix matching to highlight correctly. what: - Parse each link as a URL and check for a pathname - Links with a path: match origin + path prefix - Links without a path: match hostname only (unchanged behavior)
1 parent 5a4f8c7 commit e91b6bd

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

docs/_templates/sidebar/projects.html

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,23 @@
6262
<script type="text/javascript">
6363
(() => {
6464
const sidebar = document.getElementById("sidebar-projects");
65-
sidebar.querySelectorAll(`a[href*="${window.location.hostname}"]`)
66-
.forEach((link) => {
67-
if (!link.classList.contains("active")) {
68-
const d = document.createElement('span');
69-
d.textContent = link.textContent;
70-
d.classList.add("active");
71-
link.parentNode.replaceChild(d, link);
72-
}
73-
});
65+
const loc = window.location;
66+
sidebar.querySelectorAll("a[href]").forEach((link) => {
67+
const url = new URL(link.href, loc.origin);
68+
const sameHost = url.hostname === loc.hostname;
69+
const hasPath = url.pathname.replace(/\/+$/, "").length > 0;
70+
// Links with a pathname: match origin + path prefix (e.g. /pytest-plugin/)
71+
// Links without: match hostname only (e.g. https://libtmux.git-pull.com)
72+
const isActive = hasPath
73+
? sameHost && loc.pathname.startsWith(url.pathname.replace(/\/+$/, ""))
74+
: sameHost;
75+
if (isActive && !link.classList.contains("active")) {
76+
const d = document.createElement('span');
77+
d.textContent = link.textContent;
78+
d.classList.add("active");
79+
link.parentNode.replaceChild(d, link);
80+
}
81+
});
7482
sidebar.classList.add('ready');
7583
})()
7684
</script>

0 commit comments

Comments
 (0)