-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathsearch.ts
More file actions
148 lines (121 loc) · 3.79 KB
/
search.ts
File metadata and controls
148 lines (121 loc) · 3.79 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// thanks @joyofcodedev for this :)
import { Index, type Id } from "flexsearch";
export type SearchContent = {
title: string;
content: string;
description: string;
href: string;
};
export type SearchResult = SearchContent & {
snippet?: string;
highlights?: string[];
category?: string;
};
let titleIndex: Index;
let contentIndex: Index;
let content: SearchContent[] = [];
export function createContentIndex(data: SearchContent[]) {
titleIndex = new Index({
tokenize: "forward",
resolution: 9,
});
contentIndex = new Index({
tokenize: "forward",
resolution: 5,
});
data.forEach((item, i) => {
titleIndex.add(i, item.title);
contentIndex.add(i, `${item.content} ${item.description}`);
});
content = data;
}
function getContentSnippet(content: string, query: string, maxLength = 150): string {
const words = query.toLowerCase().split(/\s+/);
const contentLower = content.toLowerCase();
let bestIndex = -1;
for (const word of words) {
const index = contentLower.indexOf(word);
if (index !== -1 && (bestIndex === -1 || index < bestIndex)) {
bestIndex = index;
}
}
if (bestIndex === -1) {
return content.slice(0, maxLength) + (content.length > maxLength ? "..." : "");
}
const start = Math.max(0, bestIndex - Math.floor(maxLength / 2));
const end = Math.min(content.length, start + maxLength);
const snippet = content.slice(start, end);
return (start > 0 ? "..." : "") + snippet + (end < content.length ? "..." : "");
}
function highlightMatches(text: string, query: string): string {
const words = query
.toLowerCase()
.split(/\s+/)
.filter((w) => w.length > 1);
let highlighted = text;
for (const word of words) {
const regex = new RegExp(`(${word})`, "gi");
highlighted = highlighted.replace(regex, "<mark>$1</mark>");
}
return highlighted;
}
function categorizeResult(href: string): string {
if (href.includes("/components/")) return "Components";
if (href.includes("/utilities/")) return "Utilities";
if (href.includes("/type-helpers/")) return "Type Helpers";
return "Guides";
}
function fuzzyMatch(text: string, query: string): boolean {
const textLower = text.toLowerCase();
const queryLower = query.toLowerCase();
if (textLower.includes(queryLower)) return true;
let queryIndex = 0;
for (let i = 0; i < textLower.length && queryIndex < queryLower.length; i++) {
if (textLower[i] === queryLower[queryIndex]) {
queryIndex++;
}
}
return queryIndex === queryLower.length;
}
export function searchContentIndex(query: string): SearchResult[] {
if (!query.trim()) return [];
const titleResults = titleIndex.search(query, { limit: 20, suggest: true });
const contentResults = contentIndex.search(query, { limit: 20, suggest: true });
const resultMap = new Map<Id, { score: number; source: string }>();
for (const id of titleResults) {
resultMap.set(id, { score: 10, source: "title" });
}
for (const id of contentResults) {
const existing = resultMap.get(id);
if (existing) {
existing.score += 5;
} else {
resultMap.set(id, { score: 5, source: "content" });
}
}
if (resultMap.size === 0) {
content.forEach((item, idx) => {
if (fuzzyMatch(item.title, query)) {
resultMap.set(idx, { score: 8, source: "fuzzy-title" });
} else if (fuzzyMatch(item.content, query) || fuzzyMatch(item.description, query)) {
resultMap.set(idx, { score: 3, source: "fuzzy-content" });
}
});
}
const sortedResults = Array.from(resultMap.entries())
.sort(([, a], [, b]) => b.score - a.score)
.slice(0, 10);
return sortedResults.map(([idx]) => {
const item = content[idx as number];
const snippet = getContentSnippet(item.content, query);
return {
...item,
snippet: highlightMatches(snippet, query),
highlights: query
.toLowerCase()
.split(/\s+/)
.filter((w) => w.length > 1),
category: categorizeResult(item.href),
};
});
}