loading
Note:
Why it isn't just location.hash Everything after :~: is a fragment directive, which the browser strips off the URL while navigating - it is gone from location.hash and from location.href before page script ever sees it, and document.fragmentDirective deliberately exposes nothing. So the directive of the page you are on cannot be read back: Parse reads a URL you still hold, before you navigate to it. And because the directive is only acted on during a navigation, Navigate is what makes the browser scroll.
Warning:
Chromium and Safari Firefox does not implement text fragments; there the link simply loads the page without scrolling, which is a fine fallback. Style the highlight with the ::target-text pseudo-element.

Support check

IsSupported

Reports document.fragmentDirective. A false doesn't make your links unusable - they just load the page without scrolling - so this is a 'will the highlight happen' check, not a gate on generating them.

C#
@inject Bit.Butil.TextFragment textFragment

var supported = await textFragment.IsSupported();
Live sample
support check output
Results will appear here when you interact with the samples.

API reference

Member
Signature
Description
IsSupported
ValueTask<bool> IsSupported()
True when the runtime exposes document.fragmentDirective.
Build
ValueTask<string> Build(params TextFragmentDirective[] directives)
Encodes directives into the fragment string, :~:text=…, with the escaping the spec requires.
BuildUrl
ValueTask<string> BuildUrl(string url, params TextFragmentDirective[] directives)
Appends directives to a URL, replacing any it already carries and keeping an ordinary #anchor in front of them.
Parse
ValueTask<TextFragmentDirective[]> Parse(string url)
Reads the directives out of a URL. Empty when it carries none. The current URL never carries one - the browser strips it off while navigating.
FromSelection
ValueTask<TextFragmentDirective?> FromSelection()
Turns the user's current selection into a directive. Null when nothing is selected.
Navigate
ValueTask Navigate(string url, bool replace = false)
Navigates to a URL so the browser acts on its text directive.
An unhandled error has occurred. Reload 🗙