2023-10-12 18:30:47 +00:00
|
|
|
import type { IndexTreeEvent } from "$sb/app_event.ts";
|
|
|
|
import { indexObjects } from "./api.ts";
|
2023-10-13 13:37:25 +00:00
|
|
|
import {
|
|
|
|
addParentPointers,
|
|
|
|
collectNodesOfType,
|
|
|
|
findParentMatching,
|
|
|
|
renderToText,
|
|
|
|
traverseTreeAsync,
|
|
|
|
} from "$sb/lib/tree.ts";
|
2023-10-12 18:30:47 +00:00
|
|
|
import { extractAttributes } from "$sb/lib/attribute.ts";
|
2023-10-13 14:33:37 +00:00
|
|
|
import { ObjectValue } from "$sb/types.ts";
|
2024-01-11 12:20:50 +00:00
|
|
|
import a from "https://esm.sh/v135/node_process.js";
|
|
|
|
import { updateITags } from "$sb/lib/tags.ts";
|
|
|
|
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
|
2023-10-12 18:30:47 +00:00
|
|
|
|
|
|
|
/** ParagraphObject An index object for the top level text nodes */
|
2023-10-13 14:33:37 +00:00
|
|
|
export type ParagraphObject = ObjectValue<
|
|
|
|
{
|
|
|
|
text: string;
|
|
|
|
page: string;
|
|
|
|
pos: number;
|
|
|
|
} & Record<string, any>
|
|
|
|
>;
|
2023-10-12 18:30:47 +00:00
|
|
|
|
|
|
|
export async function indexParagraphs({ name: page, tree }: IndexTreeEvent) {
|
|
|
|
const objects: ParagraphObject[] = [];
|
2024-01-11 12:20:50 +00:00
|
|
|
|
|
|
|
const frontmatter = await extractFrontmatter(tree);
|
2023-10-12 18:30:47 +00:00
|
|
|
|
|
|
|
await traverseTreeAsync(tree, async (p) => {
|
2023-10-13 13:37:25 +00:00
|
|
|
if (p.type !== "Paragraph") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (findParentMatching(p, (n) => n.type === "ListItem")) {
|
|
|
|
// Not looking at paragraphs nested in a list
|
|
|
|
return false;
|
|
|
|
}
|
2023-10-12 18:30:47 +00:00
|
|
|
|
2024-01-11 12:20:50 +00:00
|
|
|
const attrs = await extractAttributes(p, true);
|
|
|
|
const tags = new Set<string>();
|
|
|
|
const text = renderToText(p);
|
|
|
|
|
2023-10-13 13:37:25 +00:00
|
|
|
// So we're looking at indexable a paragraph now
|
2024-01-11 12:20:50 +00:00
|
|
|
collectNodesOfType(p, "Hashtag").forEach((tagNode) => {
|
|
|
|
tags.add(tagNode.children![0].text!.substring(1));
|
|
|
|
// Hacky way to remove the hashtag
|
|
|
|
tagNode.children = [];
|
|
|
|
});
|
|
|
|
|
|
|
|
const textWithoutTags = renderToText(p);
|
2023-10-13 15:32:31 +00:00
|
|
|
|
2024-01-11 12:20:50 +00:00
|
|
|
if (!textWithoutTags.trim()) {
|
|
|
|
// Empty paragraph, just tags and attributes maybe
|
|
|
|
return true;
|
2023-10-13 15:32:31 +00:00
|
|
|
}
|
2023-10-12 18:30:47 +00:00
|
|
|
|
|
|
|
const pos = p.from!;
|
2024-01-11 12:20:50 +00:00
|
|
|
const paragraph: ParagraphObject = {
|
2023-10-12 18:30:47 +00:00
|
|
|
ref: `${page}@${pos}`,
|
2024-01-11 12:20:50 +00:00
|
|
|
text,
|
|
|
|
tag: "paragraph",
|
2023-10-12 18:30:47 +00:00
|
|
|
page,
|
|
|
|
pos,
|
|
|
|
...attrs,
|
2024-01-11 12:20:50 +00:00
|
|
|
};
|
|
|
|
if (tags.size > 0) {
|
|
|
|
paragraph.tags = [...tags];
|
|
|
|
paragraph.itags = [...tags];
|
|
|
|
}
|
|
|
|
|
|
|
|
updateITags(paragraph, frontmatter);
|
|
|
|
objects.push(paragraph);
|
2023-10-12 18:30:47 +00:00
|
|
|
|
|
|
|
// stop on every element except document, including paragraphs
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2023-10-13 13:37:25 +00:00
|
|
|
// console.log("Paragraph objects", objects);
|
|
|
|
|
2023-10-12 18:30:47 +00:00
|
|
|
await indexObjects<ParagraphObject>(page, objects);
|
|
|
|
}
|