From f7b664aad3219f769b271391a58469d078e29772 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 13 Oct 2023 15:37:25 +0200 Subject: [PATCH] Fixes to paragraph indexing --- plugs/index/paragraph.ts | 33 +++++++++++++++++++++------------ plugs/index/tags.ts | 3 +++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/plugs/index/paragraph.ts b/plugs/index/paragraph.ts index 9619e68..ee2c28f 100644 --- a/plugs/index/paragraph.ts +++ b/plugs/index/paragraph.ts @@ -1,6 +1,12 @@ import type { IndexTreeEvent } from "$sb/app_event.ts"; import { indexObjects } from "./api.ts"; -import { renderToText, traverseTree, traverseTreeAsync } from "$sb/lib/tree.ts"; +import { + addParentPointers, + collectNodesOfType, + findParentMatching, + renderToText, + traverseTreeAsync, +} from "$sb/lib/tree.ts"; import { extractAttributes } from "$sb/lib/attribute.ts"; /** ParagraphObject An index object for the top level text nodes */ @@ -14,22 +20,23 @@ export type ParagraphObject = { export async function indexParagraphs({ name: page, tree }: IndexTreeEvent) { const objects: ParagraphObject[] = []; + addParentPointers(tree); await traverseTreeAsync(tree, async (p) => { - // only search directly under document - // Paragraph nodes also appear under block elements - if (p.type == "Document") return false; // continue traversal if p is Document - if (p.type != "Paragraph") return true; + if (p.type !== "Paragraph") { + return false; + } + if (findParentMatching(p, (n) => n.type === "ListItem")) { + // Not looking at paragraphs nested in a list + return false; + } + + // So we're looking at indexable a paragraph now const tags = new Set(["paragraph"]); // tag the paragraph with any hashtags inside it - traverseTree(p, (e) => { - if (e.type == "Hashtag") { - tags.add(e.children![0].text!.substring(1)); - return true; - } - - return false; + collectNodesOfType(p, "Hashtag").forEach((tagNode) => { + tags.add(tagNode.children![0].text!.substring(1)); }); const attrs = await extractAttributes(p, false); @@ -47,5 +54,7 @@ export async function indexParagraphs({ name: page, tree }: IndexTreeEvent) { return true; }); + // console.log("Paragraph objects", objects); + await indexObjects(page, objects); } diff --git a/plugs/index/tags.ts b/plugs/index/tags.ts index f4491d3..7ad853e 100644 --- a/plugs/index/tags.ts +++ b/plugs/index/tags.ts @@ -32,6 +32,9 @@ export async function indexTags({ name, tree }: IndexTreeEvent) { } else if (findParentMatching(h, (n) => n.type === "ListItem")) { // Or an item tags.add(`${tagName}:item`); + } else if (findParentMatching(h, (n) => n.type === "Paragraph")) { + // Still indexing this as a page tag + tags.add(`${tagName}:page`); } }); // console.log("Indexing these tags", tags);