-
Notifications
You must be signed in to change notification settings - Fork 1.7k
toc.js: marking active page in toc when redirected to url without .html
suffix
#2570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
toc.js: marking active page in toc when redirected to url without .html
suffix
#2570
Conversation
I did not notice that Github pages redirects I personally prefer to direct people to the nice URLs (without the I tried this PR locally, both with the files generated by mdbook and the files after my post-processing. It seems that the highlighting works in all the cases. One thing I find odd in the PR is that this.innerHTML = '{{#toc}}{{/toc}}';
// Set the current, active page, and reveal it if it's hidden
let current_page = document.location.href.toString().split("#")[0];
if (current_page.endsWith("/")) {
current_page += "index.html";
}
var links = Array.prototype.slice.call(this.querySelectorAll("a")); came after all the function definitions and we would not have
but
|
My bad for not making that clear, gh pages behaves like what you said (serves html without the extension and without redirect).
Yep that's intended, this code should require no change to continue working if the
I will update that |
81a07a5
to
568b064
Compare
Addressed the comments, and I also found a bug where in the test book, the prefix chapter gets incorrectly highlighted for the root path because it's the first chapter, while the root path opens Introduction instead. In this case the first chapter is not aliased to because an |
@ehuss are you able to review this? |
☔ The latest upstream changes (possibly #2681) made this pull request unmergeable. Please resolve the merge conflicts. |
Issue
When serving the book through services that redirects
/foo/bar.html
->/foo/bar
, the page in TOC isn't marked as active because the toc uses an exact match with thehref
on thea
tag.This is a common behavior for services such as GH pages, and tools like
npx serve
(which actually redirects to paths without.html
and serves the html anyway. in this case the active page never gets marked)Fix
Refactored the check for if a link is current page, and loosened the condition so the
.html
suffix is optional:/foo
and/foo.html
will match/foo.html
link/foo/
,/foo/index
,/foo/index.html
will match/foo/index.html
linkIf the active page is still not found, it will try to map
/foo
to/foo/index.html
, which is the redirect behavior of some services/toolsTesting
Verified each link in the TOC now gets highlighted when switching to the page