forked from DustinBrett/daedalOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.ts
More file actions
180 lines (154 loc) · 4.98 KB
/
search.ts
File metadata and controls
180 lines (154 loc) · 4.98 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { basename, extname } from "path";
import { useEffect, useState } from "react";
import { type Index } from "lunr";
import type OverlayFS from "browserfs/dist/node/backend/OverlayFS";
import type IndexedDBFileSystem from "browserfs/dist/node/backend/IndexedDB";
import { useFileSystem } from "contexts/fileSystem";
import { type RootFileSystem } from "contexts/fileSystem/useAsyncFs";
import SEARCH_EXTENSIONS from "scripts/searchExtensions.json";
import {
DISBALE_AUTO_INPUT_FEATURES,
HIGH_PRIORITY_REQUEST,
} from "utils/constants";
import { getExtension, loadFiles } from "utils/functions";
export const FILE_INDEX = "/.index/search.lunr.json";
export const SEARCH_LIB = "/System/lunr/lunr.min.js";
export const SEARCH_INPUT_PROPS = {
"aria-label": "Search",
enterKeyHint: "search",
inputMode: "search",
name: "search",
type: "search",
...DISBALE_AUTO_INPUT_FEATURES,
} as React.DetailedHTMLProps<
React.InputHTMLAttributes<HTMLInputElement>,
HTMLInputElement
>;
let baseIndex = Object.create(null) as Index;
let basePaths = [] as string[];
type ResponseIndex = Index & {
paths: string[];
};
const search = async (
searchTerm: string,
index?: Index
): Promise<Index.Result[]> => {
if (!window.lunr) await loadFiles([SEARCH_LIB]);
if (!index && !baseIndex?.search) {
const response = await fetch(FILE_INDEX, HIGH_PRIORITY_REQUEST);
try {
const { paths, ...responseIndex } = JSON.parse(
await response.text()
) as ResponseIndex;
baseIndex = window.lunr?.Index.load(responseIndex);
basePaths = paths;
} catch {
// Failed to parse text data to JSON
}
}
const searchIndex = index ?? baseIndex;
let results: Index.Result[] = [];
const normalizedSearchTerm = searchTerm
.trim()
.replace(/\./g, " ")
.replace(/\*~\^-\+/g, "");
try {
results = searchIndex.search?.(normalizedSearchTerm);
if (results?.length === 0) {
results = searchIndex.search?.(
`${normalizedSearchTerm.split(" ").join("* ")}*`
);
}
} catch {
// Ignore search errors
}
if (results) {
return results.map((result) => ({
...result,
ref:
(Object.prototype.hasOwnProperty.call(basePaths, result.ref)
? (basePaths[result.ref as keyof typeof basePaths] as string)
: result.ref) || "",
}));
}
return [];
};
interface IWritableFs extends Omit<IndexedDBFileSystem, "_cache"> {
_cache: {
map: Map<string, unknown>;
};
}
const buildDynamicIndex = async (
readFile: (path: string) => Promise<Buffer>,
rootFs?: RootFileSystem
): Promise<Index> => {
const overlayFs = rootFs?._getFs("/")?.fs as OverlayFS;
const overlayedFileSystems = overlayFs?.getOverlayedFileSystems();
const writable = overlayedFileSystems?.writable as IWritableFs;
const writableFiles =
(typeof writable?._cache?.map?.keys === "function" && [
...writable._cache.map.keys(),
]) ||
Object.keys(
(writable?._cache?.map as unknown as Record<string, unknown>) || {}
) ||
[];
const filesToIndex = writableFiles.filter((path) => {
const ext = getExtension(path);
return Boolean(ext) && !SEARCH_EXTENSIONS.ignore.includes(ext);
});
const indexedFiles = await Promise.all(
filesToIndex.map(async (path) => {
const name = basename(path, extname(path));
return {
name,
path,
text: SEARCH_EXTENSIONS.index.includes(getExtension(path))
? `${name} ${(await readFile(path)).toString()}`
: name,
};
})
);
const dynamicIndex = window.lunr?.(function buildIndex() {
this.ref("path");
this.field("name");
this.field("text");
indexedFiles.forEach((doc) => this.add(doc));
});
return window.lunr?.Index.load(dynamicIndex.toJSON());
};
export const fullSearch = async (
searchTerm: string,
readFile: (path: string) => Promise<Buffer>,
rootFs?: RootFileSystem
): Promise<Index.Result[]> => {
const baseResult = await search(searchTerm);
const dynamicIndex = await buildDynamicIndex(readFile, rootFs);
const dynamicResult = await search(searchTerm, dynamicIndex);
return [...baseResult, ...dynamicResult].sort((a, b) => b.score - a.score);
};
export const useSearch = (searchTerm: string): Index.Result[] => {
const [results, setResults] = useState([] as Index.Result[]);
const { readFile, rootFs } = useFileSystem();
useEffect(() => {
const updateResults = async (): Promise<void> => {
if (searchTerm.length > 0) {
if (!window.lunr) await loadFiles([SEARCH_LIB]);
search(searchTerm).then(setResults);
buildDynamicIndex(readFile, rootFs).then((dynamicIndex) =>
search(searchTerm, dynamicIndex).then((searchResults) =>
setResults((currentResults) =>
[...currentResults, ...searchResults].sort(
(a, b) => b.score - a.score
)
)
)
);
} else {
setResults([]);
}
};
updateResults();
}, [readFile, rootFs, searchTerm]);
return results;
};