返回 CodeWhale
search-utils.ts
根目录 / web / lib / search-utils.ts
1 /**
2 * search-utils.ts — shared keyword-search utilities for docs and FAQ.
3 *
4 * Pure functions extracted from the client components so they can be unit-tested
5 * without a DOM. Used by DocsSearch and FaqSearch.
6 */
7
8 import type { DocTopic } from "./docs-map";
9
10 const CATEGORY_LABELS: Record<string, { en: string; zh: string }> = {
11 "getting-started": { en: "Getting started", zh: "入门" },
12 "core-concepts": { en: "Core concepts", zh: "核心概念" },
13 reference: { en: "Reference", zh: "参考" },
14 extending: { en: "Extending", zh: "扩展" },
15 operations: { en: "Operations & community", zh: "运维与社区" },
16 };
17
18 /**
19 * Build a lowercase haystack string for a DocTopic, searching across both
20 * locales, source files, category name, and id/slug.
21 */
22 export function docTopicHaystack(t: DocTopic): string {
23 const sources = Array.isArray(t.repoSource) ? t.repoSource : [t.repoSource];
24 const parts = [
25 t.id,
26 t.slug,
27 t.label.en,
28 t.label.zh,
29 t.description.en,
30 t.description.zh,
31 ...sources,
32 t.category,
33 CATEGORY_LABELS[t.category]?.en ?? "",
34 CATEGORY_LABELS[t.category]?.zh ?? "",
35 ];
36 if (t.id === "fleet") parts.push("pod");
37 return normalizeQuery(parts.join(" "));
38 }
39
40 /**
41 * Filter DocTopics by keyword query. Returns indices into the input array.
42 * Empty/whitespace query returns all indices.
43 */
44 export function filterDocTopics(topics: DocTopic[], query: string): number[] {
45 const q = normalizeQuery(query);
46 if (!q) return topics.map((_, i) => i);
47 return topics
48 .map((t, i) => ({ i, hay: docTopicHaystack(t) }))
49 .filter(({ hay }) => hay.includes(q))
50 .map(({ i }) => i);
51 }
52
53 /**
54 * Normalize a query for matching.
55 */
56 export function normalizeQuery(query: string): string {
57 return query.trim().toLowerCase().replace(/\s+/g, " ");
58 }
59
60 /**
61 * Check whether a query matches a haystack (case-insensitive substring).
62 */
63 export function matches(haystack: string, query: string): boolean {
64 const q = normalizeQuery(query);
65 if (!q) return true;
66 return normalizeQuery(haystack).includes(q);
67 }
68
69 /** The three pieces a highlighted match splits a string into. */
70 export interface HighlightSpan {
71 before: string;
72 match: string;
73 after: string;
74 }
75
76 /**
77 * Locate `query` inside `text`, case-insensitively, in `text`'s own indices.
78 *
79 * The obvious form — `text.toLowerCase().indexOf(q)`, then slicing `text`
80 * with that index — assumes lowercasing preserves length. It does not:
81 * `"İ".toLowerCase()` is two code units, so every index after a dotted
82 * capital I in the haystack is off by one and the highlight lands on the
83 * wrong characters. Turkish is a routed locale, so this is reachable the
84 * moment localized copy enters the search haystack.
85 *
86 * A position map keeps the three returned pieces exactly reassembling `text`.
87 * Returns null when there is no match (including an empty query).
88 */
89 export function highlightSpan(text: string, query: string): HighlightSpan | null {
90 const q = normalizeQuery(query);
91 if (!q) return null;
92
93 let folded = "";
94 const sourceStart: number[] = [];
95 const sourceEnd: number[] = [];
96 for (let i = 0; i < text.length; ) {
97 if (/\s/.test(text[i])) {
98 const start = i;
99 while (i < text.length && /\s/.test(text[i])) i += 1;
100 folded += " ";
101 sourceStart.push(start);
102 sourceEnd.push(i);
103 continue;
104 }
105 const runStart = i;
106 while (i < text.length && !/\s/.test(text[i])) {
107 const char = String.fromCodePoint(text.codePointAt(i)!);
108 i += char.length;
109 }
110 const run = text.slice(runStart, i);
111 const lowered = run.toLowerCase();
112 folded += lowered;
113 const runStarts: number[] = [];
114 const runEnds: number[] = [];
115 for (let j = runStart; j < i; ) {
116 const char = String.fromCodePoint(text.codePointAt(j)!);
117 const next = j + char.length;
118 const charLower = char.toLowerCase();
119 for (let k = 0; k < charLower.length; k++) {
120 runStarts.push(j);
121 runEnds.push(next);
122 }
123 j = next;
124 }
125 if (runStarts.length === lowered.length) {
126 for (let k = 0; k < runStarts.length; k++) {
127 sourceStart.push(runStarts[k]);
128 sourceEnd.push(runEnds[k]);
129 }
130 } else {
131 for (let k = 0; k < lowered.length; k++) {
132 sourceStart.push(runStart);
133 sourceEnd.push(i);
134 }
135 }
136 }
137
138 const idx = folded.indexOf(q);
139 if (idx === -1) return null;
140
141 const start = sourceStart[idx];
142 const end = sourceEnd[idx + q.length - 1];
143
144 return {
145 before: text.slice(0, start),
146 match: text.slice(start, end),
147 after: text.slice(end),
148 };
149 }
150
150 lines TYPESCRIPT