Fix navigation when .html omitted (#1374)

* Prefer exact match of pathname
* Update CHANGELOG.md
This commit is contained in:
Peter Mosses
2024-01-25 18:02:39 +01:00
committed by GitHub
parent e96bf6ae3d
commit 742281635f
2 changed files with 23 additions and 4 deletions

View File

@@ -487,11 +487,28 @@ jtd.setTheme = function(theme) {
// and not have the slash on GitHub Pages
function navLink() {
var href = document.location.pathname;
if (href.endsWith('/') && href != '/') {
href = href.slice(0, -1);
var pathname = document.location.pathname;
var navLink = document.getElementById('site-nav').querySelector('a[href="' + pathname + '"]');
if (navLink) {
return navLink;
}
return document.getElementById('site-nav').querySelector('a[href="' + href + '"], a[href="' + href + '/"]');
// The `permalink` setting may produce navigation links whose `href` ends with `/` or `.html`.
// To find these links when `/` is omitted from or added to pathname, or `.html` is omitted:
if (pathname.endsWith('/') && pathname != '/') {
pathname = pathname.slice(0, -1);
}
if (pathname != '/') {
navLink = document.getElementById('site-nav').querySelector('a[href="' + pathname + '"], a[href="' + pathname + '/"], a[href="' + pathname + '.html"]');
if (navLink) {
return navLink;
}
}
return null; // avoids `undefined`
}
// Scroll site-nav to ensure the link to the current page is visible