1
0
silverbullet/plugs/core/item.ts
2022-04-05 17:02:17 +02:00

50 lines
1.3 KiB
TypeScript

import { IndexEvent } from "../../webapp/app_event";
import { whiteOutQueries } from "./materialized_queries";
import { batchSet } from "plugos-silverbullet-syscall/index";
import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
import { collectNodesMatching, MarkdownTree, renderMarkdown } from "../lib/tree";
type Item = {
item: string;
nested?: string;
};
export async function indexItems({ name, text }: IndexEvent) {
let items: { key: string; value: Item }[] = [];
text = whiteOutQueries(text);
console.log("Indexing items", name);
let mdTree = await parseMarkdown(text);
let coll = collectNodesMatching(mdTree, (n) => n.type === "ListItem");
coll.forEach((n) => {
if (!n.children) {
return;
}
let textNodes: MarkdownTree[] = [];
let nested: string | undefined;
for (let child of n.children!.slice(1)) {
if (child.type === "OrderedList" || child.type === "BulletList") {
nested = renderMarkdown(child);
break;
}
textNodes.push(child);
}
let item = textNodes.map(renderMarkdown).join("").trim();
let value: Item = {
item,
};
if (nested) {
value.nested = nested;
}
items.push({
key: `it:${n.from}`,
value,
});
});
console.log("Found", items.length, "item(s)");
await batchSet(name, items);
}