1
0
silverbullet/plugs/core/search.ts

83 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-10-14 13:11:33 +00:00
import { fulltext } from "$sb/plugos-syscall/mod.ts";
import { renderToText } from "$sb/lib/tree.ts";
import type { PageMeta } from "../../common/types.ts";
import { editor, index } from "$sb/silverbullet-syscall/mod.ts";
import { IndexTreeEvent, QueryProviderEvent } from "$sb/app_event.ts";
import { applyQuery, removeQueries } from "$sb/lib/query.ts";
2022-05-16 13:09:36 +00:00
const searchPrefix = "🔍 ";
2022-10-14 13:11:33 +00:00
export async function pageIndex(data: IndexTreeEvent) {
2022-05-16 13:09:36 +00:00
removeQueries(data.tree);
2022-10-14 13:11:33 +00:00
const cleanText = renderToText(data.tree);
await fulltext.fullTextIndex(data.name, cleanText);
2022-05-16 13:09:36 +00:00
}
2022-10-14 13:11:33 +00:00
export async function pageUnindex(pageName: string) {
await fulltext.fullTextDelete(pageName);
2022-06-28 12:14:15 +00:00
}
2022-05-16 13:09:36 +00:00
export async function queryProvider({
query,
}: QueryProviderEvent): Promise<any[]> {
2022-10-14 13:11:33 +00:00
const phraseFilter = query.filter.find((f) => f.prop === "phrase");
2022-05-16 13:09:36 +00:00
if (!phraseFilter) {
throw Error("No 'phrase' filter specified, this is mandatory");
}
2022-10-14 13:11:33 +00:00
let results = await fulltext.fullTextSearch(phraseFilter.value, 100);
2022-05-16 13:09:36 +00:00
2022-10-14 13:11:33 +00:00
const allPageMap: Map<string, any> = new Map(
2022-10-12 09:47:13 +00:00
results.map((r: any) => [r.name, r]),
2022-05-17 06:32:33 +00:00
);
2022-10-14 13:11:33 +00:00
for (const { page, value } of await index.queryPrefix("meta:")) {
const p = allPageMap.get(page);
2022-05-16 13:09:36 +00:00
if (p) {
for (let [k, v] of Object.entries(value)) {
p[k] = v;
}
}
}
// Remove the "phrase" filter
query.filter.splice(query.filter.indexOf(phraseFilter), 1);
results = applyQuery(query, results);
return results;
}
2022-05-17 09:53:17 +00:00
export async function searchCommand() {
2022-10-14 13:11:33 +00:00
const phrase = await 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
}
}
export async function readPageSearch(
2022-10-12 09:47:13 +00:00
name: string,
2022-05-17 09:53:17 +00:00
): Promise<{ text: string; meta: PageMeta }> {
2022-10-14 13:11:33 +00:00
const phrase = name.substring(searchPrefix.length);
const results = await fulltext.fullTextSearch(phrase, 100);
2022-10-12 09:47:13 +00:00
const text = `# Search results for "${phrase}"\n${
results
.map((r: any) => `* [[${r.name}]] (score: ${r.rank})`)
.join("\n")
}
2022-05-17 09:53:17 +00:00
`;
return {
text: text,
meta: {
name,
lastModified: 0,
perm: "ro",
},
};
}
2022-10-14 13:11:33 +00:00
export function getPageMetaSearch(name: string): PageMeta {
2022-05-17 09:53:17 +00:00
return {
name,
lastModified: 0,
perm: "ro",
};
}