Skip to content

Update URL hash based on heading in view #514

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

Closed
wants to merge 1 commit into from

Conversation

chrissy-dev
Copy link

This is something I noticed while reading through the README on the website — the URL didn’t update as I scrolled, so using the back button wouldn’t take me to the last section I was reading.

This PR adds a small script that watches which heading is visible on screen and updates the #anchor in the browser's URL. That way, you can scroll around, and the browser history reflects where you are.

What changed

  • Uses IntersectionObserver to detect which heading is currently in view
  • Updates the URL hash (#heading-name) without reloading the page
  • Works with the heading IDs created by marked-gfm-heading-id, even when they start with - (which is fine in modern browsers)

Let me know if this feels too extra — just thought it was a nice little UX improvement.

@offa
Copy link
Owner

offa commented Apr 9, 2025

This sounds like a good idea! I just have to figure out how to actually try it 😅.

@offa
Copy link
Owner

offa commented Apr 12, 2025

Looks like the only easy way to try it is testing it locally 🤔.

Now, I'm not sure if I understand this feature correctly. Let's say i click on Calendar in the ToC, jump to it's section and the url is updated. Where should the back button bring me?

In my case I got back to Toc, but with some section in the url I didn't visit.

@chrissy-dev
Copy link
Author

Hey! Just wanted to say thanks for taking the time to look at this.

I’m going to go ahead and close the PR — turns out this was a bit of a false alarm. I recently moved over to Android from iOS and was going through the (super helpful) list on the site. At the time, I noticed that using the back/forward buttons didn’t take me to the section I was previously reading, so I thought updating the anchors on scroll would help.

But I’ve just gone back to do a quick comparison video… and now everything’s working exactly as I’d expect 😅 God knows what was going on the first time — maybe a glitch or browser quirk on my end.

Apologies for the noise, and thanks again!

@offa
Copy link
Owner

offa commented Apr 16, 2025

No problem, still thank you for your contribution! 😃

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

Successfully merging this pull request may close these issues.

2 participants