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