Skip to content

Commit b553ee9

Browse files
committed
Implement our own fuzzy search
1 parent dbd7e52 commit b553ee9

File tree

4 files changed

+445
-80
lines changed

4 files changed

+445
-80
lines changed

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"bootstrap": "^5.2.2",
1313
"esbuild": "^0.25.0",
1414
"esbuild-plugin-vue3": "^0.4.2",
15-
"fuse.js": "^7.0.0",
1615
"htmlnano": "^2.1.1",
1716
"typescript": "^5.2.2",
1817
"vue": "^3.2.40"

src/App.ts

Lines changed: 5 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import Fuse, { type FuseResultMatch, type FuseSortFunctionArg, type IFuseOptions } from 'fuse.js';
2-
31
import { defineComponent, markRaw, ref } from 'vue';
42
import interfacesJson from '../api.json';
53
import ApiParameter from './ApiParameter.vue';
64
import type { ApiInterface, ApiMethod, ApiMethodParameter, ApiServices, SidebarGroupData } from './interfaces';
5+
import { ApiSearcher } from './search';
76

87
interface FuseSearchType {
98
interface: string;
@@ -81,7 +80,7 @@ export default defineComponent({
8180
accessTokenVisible: false,
8281
currentFilter: '',
8382
currentInterface: '',
84-
fuzzy: new Object() as Fuse<FuseSearchType>,
83+
search: markRaw(new ApiSearcher(interfaces)),
8584
interfaces,
8685
groupsMap,
8786
groupsData,
@@ -230,26 +229,6 @@ export default defineComponent({
230229
false,
231230
);
232231

233-
const fuseOptions: IFuseOptions<FuseSearchType> = {
234-
shouldSort: true,
235-
useExtendedSearch: true,
236-
includeMatches: true,
237-
minMatchCharLength: 3,
238-
threshold: 0.3,
239-
keys: [
240-
{
241-
name: 'interface',
242-
weight: 0.3,
243-
},
244-
{
245-
name: 'method',
246-
weight: 0.7,
247-
},
248-
],
249-
};
250-
const fuse = new Fuse<FuseSearchType>(flattenedMethods, fuseOptions);
251-
this.fuzzy = markRaw(fuse);
252-
253232
this.bindGlobalKeybind();
254233
},
255234
computed: {
@@ -285,26 +264,16 @@ export default defineComponent({
285264
return this.interfaces;
286265
}
287266

288-
const matches = this.fuzzy.search(this.currentFilter.replace('/', '|'));
289267
const matchedInterfaces: ApiServices = {};
268+
const hits = this.search.search(this.currentFilter);
290269

291-
for (const searchResult of matches) {
292-
const match = searchResult.item;
293-
270+
for (const match of hits) {
294271
if (!matchedInterfaces[match.interface]) {
295272
matchedInterfaces[match.interface] = {};
296273
}
297274

298-
let highlight: string | undefined;
299-
for (const m of searchResult.matches!) {
300-
if (m.key === 'method') {
301-
highlight = this.highlightMatches(m);
302-
break;
303-
}
304-
}
305-
306275
const method = this.interfaces[match.interface][match.method];
307-
method.highlight = highlight;
276+
method.highlight = match.highlight;
308277
matchedInterfaces[match.interface][match.method] = method;
309278
}
310279

@@ -660,38 +629,6 @@ export default defineComponent({
660629
this.currentFilter = (e.target as HTMLInputElement).value;
661630
});
662631
},
663-
highlightMatches(match: FuseResultMatch) {
664-
let lastIndex = 0;
665-
const result: string[] = [];
666-
const sortedMatches = match.indices.toSorted((a, b) => a[0] - b[0]);
667-
const value = match.value!;
668-
669-
for (let [start, end] of sortedMatches) {
670-
if (end <= lastIndex) {
671-
continue;
672-
}
673-
674-
if (start < lastIndex) {
675-
start = lastIndex;
676-
}
677-
678-
if (lastIndex < start) {
679-
result.push(value.slice(lastIndex, start));
680-
}
681-
682-
end++;
683-
684-
result.push('<b>', value.slice(start, end), '</b>');
685-
686-
lastIndex = end;
687-
}
688-
689-
if (lastIndex !== value.length) {
690-
result.push(value.slice(lastIndex));
691-
}
692-
693-
return result.join('');
694-
},
695632
bindGlobalKeybind() {
696633
document.addEventListener('keydown', (e: KeyboardEvent) => {
697634
if (e.ctrlKey || e.metaKey) {

0 commit comments

Comments
 (0)