Scroll to text fragment
Several times I noticed that some links contain an URL fragment that highlights text on the linked page, specifically this: #:~:text=. I didn't look into it at all, I just though it's some JavaScript library on the target page that makes this possible.
Recently I came across a link that uses this syntax and finally looked it up. I was pleasantly surprised to learn that it's a standard, albeit newer feature supported in all major browsers. The spec proposal is here.
The simplest form is:
#:~:text=textToMatch
The match must be provided exactly, with spaces and punctuation, but seems to be case insensitive. Only the first match is highlighted.
Spaces and punctuation should ideally be URL encoded. When testing and typing the match into the address bar, spaces seem to get converted to %20 by the browsers. But I think in the href HTML attribute it's best to avoid using spaces and encode them to %20 to be sure.
The spec says that dash - (%2D), ampersand & (%26) and comma , (%2C) must be encoded, otherwise the matching doesn't work. For a passage I wanted to highlight I needed to include a left parenthesis. Because I'm writing in markdown I had to encode ( (%28) as well. In the browser address bar it wasn't necessary. Therefore I'd make the general recommendation to encode all punctuation.
The full syntax of the fragment is like this:
:~:text=[prefix-,]textStart[,textEnd][,-suffix]
context |-------match-----| context
We can provide a starting and ending snippet for the match, which is useful for longer passages that we cannot put into the fragment in full. With a prefix and/or suffix we can optionally provide additional context. Prefix is a snippet is directly preceding the match, and suffix is a snippet directly after it. Neither will be highlighted. They provide the surrounding context, as the name suggests.
There must be a dash in both – the prefix must end, the suffix must start with a dash – it disambiguates them from textStart and textEnd snippets. That is the reason why the dash character must be URL encoded.