1
0
silverbullet/plugs/search/search.ts

98 lines
2.4 KiB
TypeScript
Raw Normal View History

import { IndexTreeEvent, QueryProviderEvent } from "$sb/app_event.ts";
2022-10-14 13:11:33 +00:00
import { renderToText } from "$sb/lib/tree.ts";
import {
applyQuery,
evalQueryExpression,
liftAttributeFilter,
} from "$sb/lib/query.ts";
2023-10-03 13:24:07 +00:00
import { editor } from "$sb/syscalls.ts";
import { FileMeta } from "$sb/types.ts";
2023-08-29 19:17:29 +00:00
import { PromiseQueue } from "$sb/lib/async.ts";
2023-10-03 13:24:07 +00:00
import { ftsIndexPage, ftsSearch } from "./engine.ts";
2022-05-16 13:09:36 +00:00
const searchPrefix = "🔍 ";
2023-08-29 19:17:29 +00:00
// Search indexing is prone to concurrency issues, so we queue all write operations
const promiseQueue = new PromiseQueue();
export function indexPage({ name, tree }: IndexTreeEvent) {
const text = renderToText(tree);
2023-08-29 19:17:29 +00:00
return promiseQueue.runInQueue(async () => {
// console.log("Now FTS indexing", name);
2023-10-03 13:24:07 +00:00
// await engine.deleteDocument(name);
await ftsIndexPage(name, text);
2023-08-29 19:17:29 +00:00
});
2022-06-28 12:14:15 +00:00
}
2022-05-16 13:09:36 +00:00
export async function queryProvider({
query,
}: QueryProviderEvent): Promise<any[]> {
const phraseFilter = liftAttributeFilter(query.filter, "phrase");
2022-05-16 13:09:36 +00:00
if (!phraseFilter) {
throw Error("No 'phrase' filter specified, this is mandatory");
}
const phrase = evalQueryExpression(phraseFilter, {});
// console.log("Phrase", phrase);
2023-10-03 13:24:07 +00:00
let results: any[] = await ftsSearch(phrase);
// Patch the object to a format that users expect (translate id to name)
for (const r of results) {
r.name = r.id;
delete r.id;
}
2022-05-16 13:09:36 +00:00
results = applyQuery(query, results);
return results;
}
2022-05-17 09:53:17 +00:00
export async function searchCommand() {
2022-10-19 07:52:29 +00:00
const phrase = await editor.prompt("Search for: ");
2022-05-17 09:53:17 +00:00
if (phrase) {
2022-10-14 13:11:33 +00:00
await editor.navigate(`${searchPrefix}${phrase}`);
2022-05-17 09:53:17 +00:00
}
}
2022-10-19 07:52:29 +00:00
export async function readFileSearch(
2022-10-12 09:47:13 +00:00
name: string,
): Promise<{ data: Uint8Array; meta: FileMeta }> {
2022-10-19 07:52:29 +00:00
const phrase = name.substring(
searchPrefix.length,
name.length - ".md".length,
);
2023-10-03 13:24:07 +00:00
const results = await ftsSearch(phrase);
2022-10-12 09:47:13 +00:00
const text = `# Search results for "${phrase}"\n${
results
.map((r) => `* [[${r.id}]] (score ${r.score})`)
.join("\n")
2022-10-12 09:47:13 +00:00
}
`;
2022-10-19 07:52:29 +00:00
2022-05-17 09:53:17 +00:00
return {
data: new TextEncoder().encode(text),
2022-05-17 09:53:17 +00:00
meta: {
name,
2022-10-19 07:52:29 +00:00
contentType: "text/markdown",
size: text.length,
2022-05-17 09:53:17 +00:00
lastModified: 0,
perm: "ro",
},
};
}
export function writeFileSearch(
name: string,
): FileMeta {
// Never actually writing this
return getFileMetaSearch(name);
}
2022-10-19 07:52:29 +00:00
export function getFileMetaSearch(name: string): FileMeta {
2022-05-17 09:53:17 +00:00
return {
name,
2022-10-19 07:52:29 +00:00
contentType: "text/markdown",
size: -1,
2022-05-17 09:53:17 +00:00
lastModified: 0,
perm: "ro",
};
}