All files / apps/developer-portal/app/utils search.ts

96.42% Statements 27/28
92% Branches 23/25
90.9% Functions 10/11
100% Lines 25/25

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                                  2x             2x 2x   2x 26x 26x         2x         33x             33x 153x   396x 396x         33x   33x 29x   29x   25x 69x 53x 25x 29x   29x                       33x                 396x               131x      
import type { CommandPaletteGroup, CommandPaletteItem } from "@nuxt/ui";
import type { AppEntry } from "~/types/AppEntry";
import type { AppGroup } from "~/types/AppGroup";
import type { Translate } from "~/types/Translate";
 
/** One heading of one doc, as `queryCollectionSearchSections` splits it. */
export interface SearchSection {
    /** The doc's collection path, plus `#<anchor>` for a heading inside it. */
    id: string;
    title: string;
    /** The headings above this one, the doc's title first. */
    titles: string[];
    level: number;
    content: string;
}
 
/** Docs that a feature page renders, keyed by that page's `nav` key, so a removed layer falls back to the wiki. */
const RECORDS: Record<string, RegExp> = {
    decisions: /^\/docs\/decisions\/(\d{4}-[^/]+)$/,
    audits: /^\/docs\/audits\/(\d{4}-[^/]+)$/,
    benchmarks: /^\/docs\/benchmarks\/((?!readme$|template$)[^/]+)$/,
};
 
/** A page whose name or app holds every word typed, in any case; an empty search keeps them all. */
export const matchPages = (term: string, items: CommandPaletteItem[]): CommandPaletteItem[] => {
    const words = term.toLowerCase().split(/\s+/).filter(Boolean);
 
    return items.filter((item) => {
        const text = `${item.label ?? ""} ${item.suffix ?? ""}`.toLowerCase();
        return words.every((word) => text.includes(word));
    });
};
 
/** Every page in the rail, then each doc under the page that renders it, the wiki taking the rest. */
export const searchGroups = (
    apps: (AppGroup & { entries: AppEntry[] })[],
    sections: SearchSection[],
    t: Translate,
): CommandPaletteGroup<CommandPaletteItem>[] => {
    const WIKI = {
        id: "docs",
        label: t("app.search.wiki"),
        icon: "i-lucide-file-text",
        to: "/wiki",
    };
 
    const owners = apps
        .flatMap((app) => app.entries)
        .flatMap((entry) => {
            const pattern = RECORDS[entry.key];
            return pattern
                ? [{ id: entry.key, label: entry.label, icon: entry.icon, to: entry.to, pattern }]
                : [];
        });
 
    const docs = new Map<string, CommandPaletteItem[]>();
 
    for (const section of sections) {
        const [path = "", anchor] = section.id.split("#");
 
        if (anchor !== undefined && section.level === 1) continue;
 
        const owner = owners
            .map((candidate) => ({ ...candidate, record: candidate.pattern.exec(path)?.[1] }))
            .find(({ record }) => record !== undefined);
        const id = owner?.id ?? WIKI.id;
        const to = owner ? `${owner.to}/${owner.record}` : `${WIKI.to}${path}`;
 
        docs.set(id, [
            ...(docs.get(id) ?? []),
            {
                label: section.title,
                prefix: section.titles.length > 0 ? `${section.titles.join(" › ")} ›` : undefined,
                suffix: section.content,
                to: anchor === undefined ? to : `${to}#${anchor}`,
                icon: owner?.icon ?? WIKI.icon,
            },
        ]);
    }
 
    return [
        {
            id: "pages",
            label: t("app.search.pages"),
            // The palette keeps its 12 best matches across every group, and docs that also match in
            // their text outrank a page matching by name alone, so pages are matched apart.
            ignoreFilter: true,
            postFilter: matchPages,
            items: apps.flatMap((app) =>
                app.entries.map((entry) => ({
                    label: entry.label,
                    suffix: app.label,
                    to: entry.to,
                    icon: entry.icon,
                })),
            ),
        },
        ...[...owners, WIKI].map(({ id, label }) => ({ id, label, items: docs.get(id) ?? [] })),
    ];
};