-
Notifications
You must be signed in to change notification settings - Fork 82
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
Fix: Fix URL Anchor Hash Inconsistency When Switching Languages #160
base: main
Are you sure you want to change the base?
Conversation
* Still few incorrect anchors are exist.
* As there is no value about english headers, they are generated automatically * Mismatch with automatic-generated english anchors and other languages anchors
@clicelee is attempting to deploy a commit to the Toss Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @clicelee, thanks for the PR! I really appreciate you finding and fixing this issue. Sorry for the late comment.
I have a couple of thoughts:
- This solution requires adding every possible header text to the titleMap. As our documentation grows and changes, managing that could become a hassle. What do you think about that?
- Also, is it really necessary to have anchors work across different languages? The current version keeps the anchor static for a single language, and while the new approach fixes the inconsistency, it relies on a fixed titleMap, which might bring its own challenges.
I'd love to hear your thoughts on these trade-offs. Thanks again for your work on this!
📝 Key Changes
This PR ensures that anchor hash values remain consistent across different languages. Previously, switching languages would not update the hash, causing incorrect navigation. By implementing a slugify function and using a titleMap based on English headers, it now generates unified anchor values automatically, preventing unwanted page jumps.
Changed file:
.vitepress/config.mts
Problem
In the Frontend Fundamentals, when navigating to a specific section, the section title is appended as a hash value in the URL.
For example:
✅ Korean URL:
https://frontend-fundamentals.com/code/examples/submit-button.html#👃-코드-냄새-맡아보기
However, when switching to another language, the hash value does not change, which results in incorrect navigation.
❌ Incorrect behavior when switching languages:
https://frontend-fundamentals.com/ja/code/examples/submit-button.html#👃-코드-냄새-맡아보기
Since the hash remains the same across all languages, the browser does not navigate to the correct section and instead moves to the top of the page.
Solution Written in VitePress
According to VitePress documentation, we can unify the anchor hash values across different languages by explicitly specifying the anchor name in the markdown headers:
This ensures that all languages use the same
#code-example
hash, leading to consistent navigation:✅
...com/en/#code-example
✅
...com/ko/#code-example
However manually defining
{#anchor-name}
for every header in a multilingual documentation is tedious and error-prone, I automated the process by using a slugify function.Solution using slugify and titleMap
titleMap
follows English headers as the default).titleMap
.Code Example 1: LoginStartPage
andCode Example
), I explicitly define the mapping intitleMap
.Implementation Summary
titleMap
.💡 Additional Notes
titleMap
to maintain consistency.🖼️ Before and After Comparison
Before
When switching to another language, the hash value does not change, which results in incorrect navigation.
Since the hash remains the same across all languages, the browser does not navigate to the correct section and instead moves to the top of the page.
BEFORE.mov
After
When switching between languages, users can stay on the correct section instead of being sent to the top of the page.
AFTER.mov