2022-04-04 09:51:41 +00:00
|
|
|
import {IndexEvent} from "../../webapp/app_event";
|
|
|
|
import {whiteOutQueries} from "./materialized_queries";
|
2022-04-01 15:07:08 +00:00
|
|
|
|
2022-04-04 09:51:41 +00:00
|
|
|
import {batchSet} from "plugos-silverbullet-syscall/index";
|
|
|
|
import {parseMarkdown} from "plugos-silverbullet-syscall/markdown";
|
2022-04-04 13:25:07 +00:00
|
|
|
import {collectNodesMatching, MarkdownTree, renderMarkdown,} from "../lib/tree";
|
2022-03-29 15:02:28 +00:00
|
|
|
|
|
|
|
type Item = {
|
|
|
|
item: string;
|
2022-04-04 09:51:41 +00:00
|
|
|
nested?: string;
|
2022-03-29 15:02:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export async function indexItems({ name, text }: IndexEvent) {
|
|
|
|
let items: { key: string; value: Item }[] = [];
|
|
|
|
text = whiteOutQueries(text);
|
|
|
|
|
2022-04-04 09:51:41 +00:00
|
|
|
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") {
|
2022-04-04 13:25:07 +00:00
|
|
|
nested = renderMarkdown(child);
|
2022-04-04 09:51:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
textNodes.push(child);
|
|
|
|
}
|
2022-04-04 13:25:07 +00:00
|
|
|
let item = textNodes.map(renderMarkdown).join("").trim();
|
2022-03-29 15:02:28 +00:00
|
|
|
let value: Item = {
|
|
|
|
item,
|
|
|
|
};
|
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
|
|
|
}
|