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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 4 deletions

View File

@ -17,6 +17,7 @@ This website is built from the `HEAD` of the `main` branch of the theme reposito
Code changes to `main` that are *not* in the latest release:
- Fixed: incorrect navigation when `.html` omitted from URL by [@pdmosses] in [#1374]
- Fixed: incorrect positioning of clickable area for navigation links on Safari by [@mattxwang] in [#1403]
Docs changes made since the latest release:
@ -29,6 +30,7 @@ Docs changes made since the latest release:
[@mitchnemirov]: https://github.com/mitchnemirov
[#1374]: https://github.com/just-the-docs/just-the-docs/pull/1374
[#1390]: https://github.com/just-the-docs/just-the-docs/pull/1390
[#1403]: https://github.com/just-the-docs/just-the-docs/pull/1403

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