Skip to content

fix: enhance sidebar navigation link selection for decoded URLs#2708

Merged
sy-records merged 2 commits intodocsifyjs:developfrom
sy-records:fix/2706
Apr 2, 2026
Merged

fix: enhance sidebar navigation link selection for decoded URLs#2708
sy-records merged 2 commits intodocsifyjs:developfrom
sy-records:fix/2706

Conversation

@sy-records
Copy link
Copy Markdown
Member

Summary

Related issue, if any:

Close #2706

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed

Does this PR introduce a breaking change?

  • Yes
  • No

Tested in the following browsers:

  • Chrome
  • Firefox
  • Safari
  • Edge

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 31, 2026

@sy-records is attempting to deploy a commit to the Docsify Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 31, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docsify-preview Ready Ready Preview, Comment Mar 31, 2026 10:52am

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes sidebar navigation link selection so the active sidebar item (and its generated TOC expansion) can be found even when the current route URL is percent-encoded but the sidebar link href is decoded (e.g., non‑Latin filenames like Chinese), addressing #2706.

Changes:

  • Decode the computed active route URL and attempt to match the sidebar link by either encoded or decoded href.
  • Update the sidebar active element lookup selector to support both forms.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +317 to +322
const activeElmHref = this.router.toURL(this.route.path);
const decodedHref = decodeURIComponent(activeElmHref);
const activeEl = /** @type {HTMLElement | null} */ (
dom.find(`.sidebar-nav a[href="${activeElmHref}"]`)
dom.find(
`.sidebar-nav a[href="${activeElmHref}"]${activeElmHref !== decodedHref ? `, .sidebar-nav a[href="${decodedHref}"]` : ''}`,
)
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

decodeURIComponent(activeElmHref) can throw on malformed percent-encoding (e.g. a user navigates to a hash containing a raw %), which would break sidebar rendering here. Also, using the decoded value directly inside a CSS attribute selector can make querySelector throw if decoding introduces selector-breaking characters like " or \. Consider a safe decode (try/catch + fallback to original) and/or avoid interpolating unescaped URL strings into a selector (e.g., iterate anchors and compare getAttribute('href'), or escape via CSS.escape with a fallback).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

@paulhibbitts paulhibbitts left a comment

Choose a reason for hiding this comment

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

Thanks @sy-records! Created the following codesandbox to test, both with current rc-4 (confirmed issue) and PR preview:
https://codesandbox.io/p/sandbox/2jjczx

With the example content as described in original issue, this fix looks to be working.

Image

Great catch @a1ess!

@sy-records sy-records merged commit 72c0d63 into docsifyjs:develop Apr 2, 2026
8 checks passed
@sy-records sy-records deleted the fix/2706 branch April 2, 2026 02:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v5 does not generate TOC in sidebar if file name is in Chinese

3 participants