1
0

Fixes to paragraph indexing

This commit is contained in:
Zef Hemel 2023-10-13 15:37:25 +02:00
parent 50105fd044
commit f7b664aad3
2 changed files with 24 additions and 12 deletions

View File

@ -1,6 +1,12 @@
import type { IndexTreeEvent } from "$sb/app_event.ts"; import type { IndexTreeEvent } from "$sb/app_event.ts";
import { indexObjects } from "./api.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"; import { extractAttributes } from "$sb/lib/attribute.ts";
/** ParagraphObject An index object for the top level text nodes */ /** 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) { export async function indexParagraphs({ name: page, tree }: IndexTreeEvent) {
const objects: ParagraphObject[] = []; const objects: ParagraphObject[] = [];
addParentPointers(tree);
await traverseTreeAsync(tree, async (p) => { await traverseTreeAsync(tree, async (p) => {
// only search directly under document if (p.type !== "Paragraph") {
// Paragraph nodes also appear under block elements return false;
if (p.type == "Document") return false; // continue traversal if p is Document }
if (p.type != "Paragraph") return true;
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<string>(["paragraph"]); const tags = new Set<string>(["paragraph"]);
// tag the paragraph with any hashtags inside it // tag the paragraph with any hashtags inside it
traverseTree(p, (e) => { collectNodesOfType(p, "Hashtag").forEach((tagNode) => {
if (e.type == "Hashtag") { tags.add(tagNode.children![0].text!.substring(1));
tags.add(e.children![0].text!.substring(1));
return true;
}
return false;
}); });
const attrs = await extractAttributes(p, false); const attrs = await extractAttributes(p, false);
@ -47,5 +54,7 @@ export async function indexParagraphs({ name: page, tree }: IndexTreeEvent) {
return true; return true;
}); });
// console.log("Paragraph objects", objects);
await indexObjects<ParagraphObject>(page, objects); await indexObjects<ParagraphObject>(page, objects);
} }

View File

@ -32,6 +32,9 @@ export async function indexTags({ name, tree }: IndexTreeEvent) {
} else if (findParentMatching(h, (n) => n.type === "ListItem")) { } else if (findParentMatching(h, (n) => n.type === "ListItem")) {
// Or an item // Or an item
tags.add(`${tagName}: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); // console.log("Indexing these tags", tags);