2023-10-03 20:16:33 +08:00
|
|
|
import type { IndexTreeEvent } from "$sb/app_event.ts";
|
2022-04-01 23:07:08 +08:00
|
|
|
|
2022-10-14 21:11:33 +08:00
|
|
|
import { collectNodesOfType, ParseTree, renderToText } from "$sb/lib/tree.ts";
|
2023-10-03 20:16:33 +08:00
|
|
|
import { removeQueries } from "$sb/lib/query.ts";
|
2023-07-25 01:54:31 +08:00
|
|
|
import { extractAttributes } from "$sb/lib/attribute.ts";
|
2023-07-31 01:31:04 +08:00
|
|
|
import { rewritePageRefs } from "$sb/lib/resolve.ts";
|
2023-10-03 20:16:33 +08:00
|
|
|
import { ObjectValue } from "$sb/types.ts";
|
|
|
|
import { indexObjects } from "./api.ts";
|
2022-03-29 23:02:28 +08:00
|
|
|
|
2023-10-03 20:16:33 +08:00
|
|
|
export type ItemObject = ObjectValue<
|
|
|
|
{
|
|
|
|
name: string;
|
|
|
|
page: string;
|
|
|
|
pos: number;
|
|
|
|
} & Record<string, any>
|
|
|
|
>;
|
2022-03-29 23:02:28 +08:00
|
|
|
|
2022-04-20 16:56:43 +08:00
|
|
|
export async function indexItems({ name, tree }: IndexTreeEvent) {
|
2023-10-03 20:16:33 +08:00
|
|
|
const items: ObjectValue<ItemObject>[] = [];
|
2022-04-20 16:56:43 +08:00
|
|
|
removeQueries(tree);
|
2022-03-29 23:02:28 +08:00
|
|
|
|
2022-10-21 16:00:43 +08:00
|
|
|
// console.log("Indexing items", name);
|
2022-04-04 17:51:41 +08:00
|
|
|
|
2022-10-14 21:11:33 +08:00
|
|
|
const coll = collectNodesOfType(tree, "ListItem");
|
2022-04-04 17:51:41 +08:00
|
|
|
|
2023-07-26 23:12:56 +08:00
|
|
|
for (const n of coll) {
|
2022-04-04 17:51:41 +08:00
|
|
|
if (!n.children) {
|
2023-07-26 23:12:56 +08:00
|
|
|
continue;
|
2022-04-04 17:51:41 +08:00
|
|
|
}
|
2022-07-08 15:50:26 +08:00
|
|
|
if (collectNodesOfType(n, "Task").length > 0) {
|
|
|
|
// This is a task item, skip it
|
2023-07-26 23:12:56 +08:00
|
|
|
continue;
|
2022-07-08 15:50:26 +08:00
|
|
|
}
|
|
|
|
|
2023-10-13 22:33:37 +08:00
|
|
|
const tags = new Set<string>(["item"]);
|
|
|
|
|
2023-10-03 20:16:33 +08:00
|
|
|
const item: ItemObject = {
|
|
|
|
ref: `${name}@${n.from}`,
|
|
|
|
tags: [],
|
2023-07-25 01:54:31 +08:00
|
|
|
name: "", // to be replaced
|
2023-10-03 20:16:33 +08:00
|
|
|
page: name,
|
|
|
|
pos: n.from!,
|
2023-07-25 01:54:31 +08:00
|
|
|
};
|
|
|
|
|
2022-10-14 21:11:33 +08:00
|
|
|
const textNodes: ParseTree[] = [];
|
2023-10-03 20:16:33 +08:00
|
|
|
|
|
|
|
collectNodesOfType(n, "Hashtag").forEach((h) => {
|
|
|
|
// Push tag to the list, removing the initial #
|
2023-10-13 22:33:37 +08:00
|
|
|
tags.add(h.children![0].text!.substring(1));
|
2023-10-03 20:16:33 +08:00
|
|
|
});
|
|
|
|
|
2022-10-14 21:11:33 +08:00
|
|
|
for (const child of n.children!.slice(1)) {
|
2023-07-31 01:31:04 +08:00
|
|
|
rewritePageRefs(child, name);
|
2022-04-04 17:51:41 +08:00
|
|
|
if (child.type === "OrderedList" || child.type === "BulletList") {
|
|
|
|
break;
|
|
|
|
}
|
2023-07-25 01:54:31 +08:00
|
|
|
// Extract attributes and remove from tree
|
2023-07-26 23:12:56 +08:00
|
|
|
const extractedAttributes = await extractAttributes(child, true);
|
2023-08-02 03:35:19 +08:00
|
|
|
|
2023-07-25 01:54:31 +08:00
|
|
|
for (const [key, value] of Object.entries(extractedAttributes)) {
|
|
|
|
item[key] = value;
|
|
|
|
}
|
2022-04-04 17:51:41 +08:00
|
|
|
textNodes.push(child);
|
|
|
|
}
|
2022-07-08 15:50:26 +08:00
|
|
|
|
2023-07-25 01:54:31 +08:00
|
|
|
item.name = textNodes.map(renderToText).join("").trim();
|
2023-10-13 22:33:37 +08:00
|
|
|
item.tags = [...tags.values()];
|
2022-07-08 15:50:26 +08:00
|
|
|
|
2023-10-13 22:33:37 +08:00
|
|
|
items.push(item);
|
2023-07-26 23:12:56 +08:00
|
|
|
}
|
2023-07-31 01:31:04 +08:00
|
|
|
// console.log("Found", items, "item(s)");
|
2023-10-03 20:16:33 +08:00
|
|
|
await indexObjects(name, items);
|
2022-04-19 22:54:47 +08:00
|
|
|
}
|