2022-05-16 13:09:36 +00:00
|
|
|
import { fullTextIndex, fullTextSearch } from "@plugos/plugos-syscall/fulltext";
|
|
|
|
import { renderToText } from "@silverbulletmd/common/tree";
|
2022-05-17 09:53:17 +00:00
|
|
|
import { PageMeta } from "@silverbulletmd/common/types";
|
2022-05-17 13:54:55 +00:00
|
|
|
import { queryPrefix } from "@silverbulletmd/plugos-silverbullet-syscall";
|
2022-05-17 09:53:17 +00:00
|
|
|
import {
|
|
|
|
navigate,
|
|
|
|
prompt,
|
|
|
|
} from "@silverbulletmd/plugos-silverbullet-syscall/editor";
|
2022-05-16 13:09:36 +00:00
|
|
|
import { IndexTreeEvent } from "@silverbulletmd/web/app_event";
|
|
|
|
import { applyQuery, QueryProviderEvent } from "../query/engine";
|
|
|
|
import { removeQueries } from "../query/util";
|
|
|
|
|
|
|
|
export async function index(data: IndexTreeEvent) {
|
|
|
|
removeQueries(data.tree);
|
|
|
|
let cleanText = renderToText(data.tree);
|
|
|
|
await fullTextIndex(data.name, cleanText);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function queryProvider({
|
|
|
|
query,
|
|
|
|
}: QueryProviderEvent): Promise<any[]> {
|
|
|
|
let phraseFilter = query.filter.find((f) => f.prop === "phrase");
|
|
|
|
if (!phraseFilter) {
|
|
|
|
throw Error("No 'phrase' filter specified, this is mandatory");
|
|
|
|
}
|
|
|
|
let results = await fullTextSearch(phraseFilter.value, 100);
|
|
|
|
|
2022-05-17 06:32:33 +00:00
|
|
|
let allPageMap: Map<string, any> = new Map(
|
|
|
|
results.map((r: any) => [r.name, r])
|
|
|
|
);
|
2022-05-17 13:54:55 +00:00
|
|
|
for (let { page, value } of await queryPrefix("meta:")) {
|
2022-05-16 13:09:36 +00:00
|
|
|
let p = allPageMap.get(page);
|
|
|
|
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() {
|
|
|
|
let phrase = await prompt("Search for: ");
|
|
|
|
if (phrase) {
|
|
|
|
await navigate(`@search/${phrase}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function readPageSearch(
|
|
|
|
name: string
|
|
|
|
): Promise<{ text: string; meta: PageMeta }> {
|
|
|
|
let phrase = name.substring("@search/".length);
|
|
|
|
let results = await fullTextSearch(phrase, 100);
|
|
|
|
const text = `# Search results for "${phrase}"\n${results
|
|
|
|
.map((r: any) => `* [[${r.name}]] (score: ${r.rank})`)
|
|
|
|
.join("\n")}
|
|
|
|
`;
|
|
|
|
return {
|
|
|
|
text: text,
|
|
|
|
meta: {
|
|
|
|
name,
|
|
|
|
lastModified: 0,
|
|
|
|
perm: "ro",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPageMetaSearch(name: string): Promise<PageMeta> {
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
lastModified: 0,
|
|
|
|
perm: "ro",
|
|
|
|
};
|
|
|
|
}
|