Skip to content

Fix current page detection for clean URLs#2993

Closed
qaqland wants to merge 1 commit into
rust-lang:masterfrom
qaqland:current-page-active
Closed

Fix current page detection for clean URLs#2993
qaqland wants to merge 1 commit into
rust-lang:masterfrom
qaqland:current-page-active

Conversation

@qaqland
Copy link
Copy Markdown
Contributor

@qaqland qaqland commented Dec 24, 2025

Previously, the sidebar JavaScript logic for detecting the "active" page relied on URL and assumed it would end with .html. When using clean URLs without this suffix, this logic would fail to correctly highlight the current page in the sidebar.

This change moves the current page identification to server-side rendering by adding a data-current-link attribute to the HTML root element.

Fixes #2962

@rustbot rustbot added the S-waiting-on-review Status: waiting on a review label Dec 24, 2025
@qaqland
Copy link
Copy Markdown
Contributor Author

qaqland commented Dec 24, 2025

Ref: #2570 and #2415

@qaqland qaqland force-pushed the current-page-active branch 2 times, most recently from bee3fca to 1404b89 Compare December 24, 2025 16:37
Previously, the sidebar JavaScript logic for detecting the "active" page
relied on URL and assumed it would end with .html. When using clean URLs
without this suffix, this logic would fail to correctly highlight the
current page in the sidebar.

This change moves the current page identification to server-side
rendering by adding a data-current-link attribute to the HTML root
element.

Fixes rust-lang#2962
@qaqland qaqland force-pushed the current-page-active branch from 1404b89 to 4d0d4e5 Compare January 4, 2026 08:40
@qaqland
Copy link
Copy Markdown
Contributor Author

qaqland commented Jan 13, 2026

@ehuss , could you please review this when you have a moment?

@jhult
Copy link
Copy Markdown
Contributor

jhult commented Mar 8, 2026

Related PR: #3038

@jonathanpallant
Copy link
Copy Markdown

I tested this and it works great.

See https://f74d75b4.ferrous-systems-rust-exercises.pages.dev/latest/book/nrf52/usb-exercise.

if (current_page.endsWith('/')) {
current_page += 'index.html';
}
const current_link = document.documentElement.dataset.currentLink;
Copy link
Copy Markdown
Contributor

@ehuss ehuss May 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this would be a breaking change if the user has a custom index.hbs. I try to be a little cautious about making those kinds of changes.

View changes since the review

// Set a dummy path to ensure other paths (e.g. in the TOC) are generated correctly
data_404.insert("path".to_owned(), json!("404.md"));
data_404.insert("content".to_owned(), json!(html_content_404));
data_404.insert("current_link".to_owned(), json!("404.html"));
Copy link
Copy Markdown
Contributor

@ehuss ehuss May 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing to change here. Just noting that this isn't technically correct because the output filename can be customized. However, since there isn't anything in the sidebar for it to match, I don't think it matters.

View changes since the review

ctx.data.insert("title".to_owned(), json!(title));
ctx.data
.insert("path_to_root".to_owned(), json!(fs::path_to_root(path)));
let current_link = path.with_extension("html").to_url_path();
Copy link
Copy Markdown
Contributor

@ehuss ehuss May 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm noticing that current_link isn't being set in the print page.

I don't think it matters too much because there is no sidebar navigation for the print page.

Offhand I don't think it is required, but I do have a little unease about having a missing variable.

View changes since the review

@ehuss
Copy link
Copy Markdown
Contributor

ehuss commented May 19, 2026

Thanks for the PR! I've decided to go with a simpler solution in #3028.

I appreciate that this PR has a somewhat cleaner or more principled way of communicating the correct page to the TOC. However, I'm a little concerned with the additional complexity this adds. Hopefully the other change provides the functionality that you need. And if not, let me know.

There may be future extensions like #3047 which would provide first-class support for clean urls, which would hopefully deal with redirects by avoiding them.

@ehuss ehuss closed this May 19, 2026
@rustbot rustbot removed the S-waiting-on-review Status: waiting on a review label May 19, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 19, 2026

☔ The latest upstream changes (possibly #3028) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot rustbot added the S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. label May 19, 2026
@qaqland
Copy link
Copy Markdown
Contributor Author

qaqland commented May 20, 2026

Thank you for your review; happy to see that the problem can be resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Section Headers not Displaying if '.html' Stripped from URL

5 participants