Skip to content

sphinx_highlight.js removes text fragment from URL #13916

Description

@haampie

Describe the bug

All major browsers support highlighting with Text Fragments, which is the #:~:text=... bit in a URL:

https://www.sphinx-doc.org/en/master/usage/quickstart.html#:~:text=In%20Sphinx%20source

It highlights and scrolls to the first match on the page, which is extremely useful. It's used by Google Search to scroll to a particular part of the page, and by LLMs to link to a relevant bit of the page they got their answers from, which is super useful these days.

Annoyingly, Sphinx's sphinx_highlight.js always removes this text fragment from the URL, making sharing of the exact URL the user found impossible.

How to Reproduce

Go to

https://www.sphinx-doc.org/en/master/usage/quickstart.html#:~:text=In%20Sphinx%20source

Notice that the URL bar is immediately replaced with

https://www.sphinx-doc.org/en/master/usage/quickstart.html

instead of the original

https://www.sphinx-doc.org/en/master/usage/quickstart.html#:~:text=In%20Sphinx%20source

Presumably this is due to

window.history.replaceState({}, "", url);

which is run unconditionally from

if (typeof Search === "undefined") SphinxHighlight.highlightSearchWords();

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions