mirror of
https://github.com/DustinBrett/daedalOS.git
synced 2025-12-06 00:20:05 +01:00
Simpler search json w/paths stored once
This commit is contained in:
parent
93ec39fac4
commit
3e09365cf8
|
|
@ -37,6 +37,11 @@ const normalizePath = (path) =>
|
|||
const normalizeText = (text) =>
|
||||
text.replace(/\r?\n|\r/g, " ").replace(/<\/?[^>]+(>|$)/g, "");
|
||||
|
||||
const keyPathMap = {};
|
||||
|
||||
const keyPathMapper = (path) =>
|
||||
(keyPathMap[path] ||= Object.keys(keyPathMap).length);
|
||||
|
||||
const createSearchIndex = (path) => {
|
||||
const directoryContents = readdirSync(path);
|
||||
const normalizedPath = normalizePath(path);
|
||||
|
|
@ -46,7 +51,7 @@ const createSearchIndex = (path) => {
|
|||
|
||||
indexData.push({
|
||||
name,
|
||||
path: normalizedPath,
|
||||
path: keyPathMapper(normalizedPath),
|
||||
text: normalizeText(
|
||||
[
|
||||
name,
|
||||
|
|
@ -88,12 +93,14 @@ const createSearchIndex = (path) => {
|
|||
}
|
||||
}
|
||||
|
||||
const name = basename(keyPath, extname(keyPath));
|
||||
|
||||
indexData.push({
|
||||
name: basename(keyPath, extname(keyPath)),
|
||||
path: keyPath,
|
||||
name,
|
||||
path: keyPathMapper(keyPath),
|
||||
text: SEARCH_EXTENSIONS.index.includes(extname(entry).toLowerCase())
|
||||
? normalizeText(readFileSync(fullPath, "utf8"))
|
||||
: undefined,
|
||||
? `${name} ${normalizeText(readFileSync(fullPath, "utf8"))}`
|
||||
: name,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
@ -113,9 +120,13 @@ if (!existsSync(join(PUBLIC_PATH, ".index"))) {
|
|||
mkdirSync(join(PUBLIC_PATH, ".index"));
|
||||
}
|
||||
|
||||
const searchJson = searchIndex.toJSON();
|
||||
|
||||
searchJson.paths = Object.keys(keyPathMap);
|
||||
|
||||
writeFileSync(
|
||||
join(PUBLIC_PATH, ".index/search.lunr.json"),
|
||||
JSON.stringify(searchIndex.toJSON()),
|
||||
JSON.stringify(searchJson),
|
||||
{
|
||||
flag: "w",
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,11 @@ export const SEARCH_INPUT_PROPS = {
|
|||
export const SEARCH_LIBS = ["/System/lunr/lunr.min.js"];
|
||||
|
||||
let baseIndex = Object.create(null) as Index;
|
||||
let basePaths = [] as string[];
|
||||
|
||||
type ResponseIndex = Index & {
|
||||
paths: string[];
|
||||
};
|
||||
|
||||
const search = async (
|
||||
searchTerm: string,
|
||||
|
|
@ -36,13 +41,17 @@ const search = async (
|
|||
const response = await fetch(FILE_INDEX, HIGH_PRIORITY_REQUEST);
|
||||
|
||||
try {
|
||||
baseIndex = window.lunr?.Index.load(
|
||||
JSON.parse(await response.text()) as Index
|
||||
);
|
||||
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
|
||||
|
|
@ -62,7 +71,14 @@ const search = async (
|
|||
// Ignore search errors
|
||||
}
|
||||
|
||||
return results ?? [];
|
||||
if (results) {
|
||||
return results.map((result) => ({
|
||||
...result,
|
||||
ref: basePaths[result.ref as unknown as number],
|
||||
}));
|
||||
}
|
||||
|
||||
return [];
|
||||
};
|
||||
|
||||
interface IWritableFs extends Omit<IndexedDBFileSystem, "_cache"> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user