Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .chronus/changes/astro-utils-search-highlights.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
changeKind: feature
packages:
- "@typespec/astro-utils"
---

Highlight search terms on destination pages using Pagefind's built-in highlighting. Search links preserve heading anchors, and scoped searches highlight the terms without the selector prefix.

```text
api: isHttpFile
-> https://typespec.io/docs/libraries/http/reference/typekits/?highlight=ishttpfile#ishttpfile
```
9 changes: 9 additions & 0 deletions packages/astro-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ The component uses the site's base path, Fluent theme tokens, and Starlight sear
(with English defaults on non-Starlight pages). It supports Up/Down result navigation and
Enter activation, and avoids favoring short generated API pages over longer documentation.

### Highlighting result pages

Search result links include a `highlight` query parameter. On the destination page,
the component loads Pagefind's built-in highlighting script to mark matching text
in the indexed content. Heading links retain their anchors, and scoped searches
highlight the search terms without the `api:` or `all:` selector.

Pages without a highlight query and development mode do not load the highlighting script.

### Search scopes

Use `<Search scopes />` for prefix-based search:
Expand Down
5 changes: 5 additions & 0 deletions packages/astro-utils/src/components/search.astro
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const devWarning =
window.PagefindComponents.configureInstance("default", {
baseUrl,
bundlePath,
highlightParam: "highlight",
ranking: {
// Avoid favoring short generated API pages over guides and decorator references.
pageLength: 0,
Expand Down Expand Up @@ -266,6 +267,10 @@ const devWarning =
}
const template = document.querySelector(".pagefind-search template");
template.replaceWith(template.content.cloneNode(true));
if (new URLSearchParams(window.location.search).get("highlight")) {
await import(`${bundlePath}pagefind-highlight.js`);
new window.PagefindHighlight({ highlightParam: "highlight" });
}
</script>
</>
) : (
Expand Down
Loading