2022-04-21 11:57:45 +00:00
|
|
|
import type { IndexTreeEvent } from "@silverbulletmd/web/app_event";
|
2022-04-01 15:07:08 +00:00
|
|
|
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
batchSet,
|
|
|
|
scanPrefixGlobal,
|
2022-04-25 09:24:13 +00:00
|
|
|
} from "@silverbulletmd/plugos-silverbullet-syscall/index";
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
collectNodesOfType,
|
|
|
|
ParseTree,
|
|
|
|
renderToText,
|
|
|
|
} from "@silverbulletmd/common/tree";
|
2022-04-20 08:56:43 +00:00
|
|
|
import { removeQueries } from "../query/util";
|
2022-04-19 14:54:47 +00:00
|
|
|
import { applyQuery, QueryProviderEvent } from "../query/engine";
|
2022-03-29 15:02:28 +00:00
|
|
|
|
2022-04-12 11:33:07 +00:00
|
|
|
export type Item = {
|
|
|
|
name: string;
|
2022-04-04 09:51:41 +00:00
|
|
|
nested?: string;
|
2022-04-12 11:33:07 +00:00
|
|
|
// Not stored in DB
|
|
|
|
page?: string;
|
|
|
|
pos?: number;
|
2022-03-29 15:02:28 +00:00
|
|
|
};
|
|
|
|
|
2022-04-20 08:56:43 +00:00
|
|
|
export async function indexItems({ name, tree }: IndexTreeEvent) {
|
2022-03-29 15:02:28 +00:00
|
|
|
let items: { key: string; value: Item }[] = [];
|
2022-04-20 08:56:43 +00:00
|
|
|
removeQueries(tree);
|
2022-03-29 15:02:28 +00:00
|
|
|
|
2022-04-04 09:51:41 +00:00
|
|
|
console.log("Indexing items", name);
|
|
|
|
|
2022-04-20 08:56:43 +00:00
|
|
|
let coll = collectNodesOfType(tree, "ListItem");
|
2022-04-04 09:51:41 +00:00
|
|
|
|
|
|
|
coll.forEach((n) => {
|
|
|
|
if (!n.children) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-11 18:34:09 +00:00
|
|
|
let textNodes: ParseTree[] = [];
|
2022-04-04 09:51:41 +00:00
|
|
|
let nested: string | undefined;
|
|
|
|
for (let child of n.children!.slice(1)) {
|
|
|
|
if (child.type === "OrderedList" || child.type === "BulletList") {
|
2022-04-11 18:34:09 +00:00
|
|
|
nested = renderToText(child);
|
2022-04-04 09:51:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
textNodes.push(child);
|
|
|
|
}
|
2022-04-11 18:34:09 +00:00
|
|
|
let item = textNodes.map(renderToText).join("").trim();
|
2022-03-29 15:02:28 +00:00
|
|
|
let value: Item = {
|
2022-04-12 11:33:07 +00:00
|
|
|
name: item,
|
2022-03-29 15:02:28 +00:00
|
|
|
};
|
2022-04-04 09:51:41 +00:00
|
|
|
if (nested) {
|
|
|
|
value.nested = nested;
|
2022-03-29 15:02:28 +00:00
|
|
|
}
|
|
|
|
items.push({
|
2022-04-04 09:51:41 +00:00
|
|
|
key: `it:${n.from}`,
|
2022-03-29 15:02:28 +00:00
|
|
|
value,
|
|
|
|
});
|
2022-04-04 09:51:41 +00:00
|
|
|
});
|
2022-03-29 15:02:28 +00:00
|
|
|
console.log("Found", items.length, "item(s)");
|
2022-04-01 15:07:08 +00:00
|
|
|
await batchSet(name, items);
|
2022-03-29 15:02:28 +00:00
|
|
|
}
|
2022-04-19 14:54:47 +00:00
|
|
|
|
|
|
|
export async function queryProvider({
|
|
|
|
query,
|
|
|
|
}: QueryProviderEvent): Promise<string> {
|
|
|
|
let allItems: Item[] = [];
|
|
|
|
for (let { key, page, value } of await scanPrefixGlobal("it:")) {
|
|
|
|
let [, pos] = key.split(":");
|
|
|
|
allItems.push({
|
|
|
|
...value,
|
|
|
|
page: page,
|
|
|
|
pos: +pos,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let markdownItems = applyQuery(query, allItems).map(
|
|
|
|
(item) =>
|
|
|
|
`* [[${item.page}@${item.pos}]] ${item.name}` +
|
|
|
|
(item.nested ? "\n " + item.nested : "")
|
|
|
|
);
|
|
|
|
return markdownItems.join("\n");
|
|
|
|
}
|