All files / src/lib site-search.ts

25% Statements 6/24
61.53% Branches 8/13
44.44% Functions 4/9
27.27% Lines 6/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                                                        1x     5x                                                                   3x 5x   3x 3x                                                    
import { BASE_PATH } from "@/lib/static-export";
import type { CatalogLanguage } from "@/lib/metadata-language";
 
/**
 * The site search (ADR-082): Pagefind, run in the browser over the index that
 * scripts/build-search-index.mjs writes to public/pagefind/<lang>/. Nothing a
 * reader types leaves the browser; the index is a set of static files, the
 * same on GitHub Pages and on Azure.
 */
 
/** One result: a page or a document, with up to three headings inside it. */
export interface SearchHit {
  url: string;
  title: string;
  kind: "page" | "doc";
  /** Pagefind's excerpt: text escaped by Pagefind, the match in <mark>. */
  excerpt: string;
  anchors: { url: string; title: string; excerpt: string }[];
}
 
/** What Pagefind's `result.data()` gives, as far as the dialog reads it. */
export interface PagefindData {
  url: string;
  excerpt: string;
  meta: Record<string, string | undefined>;
  sub_results?: { url: string; title: string; excerpt: string }[];
}
 
interface PagefindResults {
  results: { data(): Promise<PagefindData> }[];
}
 
interface Pagefind {
  options(o: Record<string, unknown>): Promise<void>;
  init(): Promise<void>;
  debouncedSearch(
    query: string,
    options?: Record<string, unknown>,
    ms?: number,
  ): Promise<PagefindResults | null>;
}
 
/** Where a language's index is, under the static export's base path. */
export function indexUrl(lang: CatalogLanguage): string {
  return `${BASE_PATH}/pagefind/${lang}/pagefind.js`;
}
 
const loaded = new Map<CatalogLanguage, Promise<Pagefind>>();
 
function load(lang: CatalogLanguage): Promise<Pagefind> {
  let p = loaded.get(lang);
  if (!p) {
    p = (async () => {
      // A file under public/, not a module of the bundle: the bundler must
      // leave the import to the browser.
      const pf = (await import(
        /* webpackIgnore: true */ /* turbopackIgnore: true */ indexUrl(lang)
      )) as Pagefind;
      // URLs as the index holds them ("/docs/ehds/glossary"); the router
      // adds the base path.
      // Pagefind matches the start of words, so "SPE" also finds "speaks"
      // and "spec". A higher termSimilarity ranks the exact word above the
      // longer ones (SPE puts the SPE document first); quoting the query
      // would drop the longer words but ranks by page order instead.
      await pf.options({
        baseUrl: "/",
        excerptLength: 20,
        ranking: { termSimilarity: 5 },
      });
      await pf.init();
      return pf;
    })();
    // A failed load (no index in this build) is tried again next time.
    p.catch(() => loaded.delete(lang));
    loaded.set(lang, p);
  }
  return p;
}
 
/** A Pagefind result as the dialog shows it. */
export function toHit(d: PagefindData): SearchHit {
  const anchors = (d.sub_results ?? [])
    .filter((s) => s.url.includes("#") && s.url !== d.url)
    .slice(0, 3)
    .map(({ url, title, excerpt }) => ({ url, title, excerpt }));
  return {
    url: d.url,
    title: d.meta.title ?? d.url,
    kind: d.meta.kind === "doc" ? "doc" : "page",
    excerpt: d.excerpt,
    anchors,
  };
}
 
/**
 * Search the index of one language. Resolves to null when a later keystroke
 * replaced this search before it ran.
 */
export async function searchSite(
  lang: CatalogLanguage,
  query: string,
  limit = 8,
): Promise<SearchHit[] | null> {
  const pf = await load(lang);
  const res = await pf.debouncedSearch(query, {}, 150);
  if (!res) return null;
  const data = await Promise.all(
    res.results.slice(0, limit).map((r) => r.data()),
  );
  return data.map(toHit);
}