From 6d8149d747ae057cccecdf7fbfae1302bee69524 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sat, 6 Jul 2024 20:52:27 +0200 Subject: [PATCH] Fixes #845 --- plugs/index/item.ts | 12 +++++++++--- plugs/index/paragraph.ts | 10 ++++++---- plugs/tasks/task.ts | 7 ++++++- website/CHANGELOG.md | 1 + 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/plugs/index/item.ts b/plugs/index/item.ts index dfe00659..41680af4 100644 --- a/plugs/index/item.ts +++ b/plugs/index/item.ts @@ -14,8 +14,9 @@ import { extractFrontmatter } from "$sb/lib/frontmatter.ts"; export type ItemObject = ObjectValue< { - name: string; page: string; + name: string; + text: string; pos: number; } & Record >; @@ -27,7 +28,7 @@ export async function indexItems({ name, tree }: IndexTreeEvent) { const coll = collectNodesOfType(tree, "ListItem"); - for (let n of coll) { + for (const n of coll) { if (!n.children) { continue; } @@ -40,13 +41,16 @@ export async function indexItems({ name, tree }: IndexTreeEvent) { const item: ItemObject = { ref: `${name}@${n.from}`, tag: "item", - name: "", // to be replaced + name: "", + text: "", page: name, pos: n.from!, }; const textNodes: ParseTree[] = []; + const fullText = renderToText(n); + collectNodesOfType(n, "Hashtag").forEach((h) => { // Push tag to the list, removing the initial # tags.add(h.children![0].text!.substring(1)); @@ -69,6 +73,8 @@ export async function indexItems({ name, tree }: IndexTreeEvent) { } item.name = textNodes.map(renderToText).join("").trim(); + item.text = fullText; + if (tags.size > 0) { item.tags = [...tags]; } diff --git a/plugs/index/paragraph.ts b/plugs/index/paragraph.ts index 3ede671d..16eea3a0 100644 --- a/plugs/index/paragraph.ts +++ b/plugs/index/paragraph.ts @@ -14,9 +14,9 @@ import { extractFrontmatter } from "$sb/lib/frontmatter.ts"; /** ParagraphObject An index object for the top level text nodes */ export type ParagraphObject = ObjectValue< { - text: string; page: string; pos: number; + text: string; } & Record >; @@ -35,7 +35,9 @@ export async function indexParagraphs({ name: page, tree }: IndexTreeEvent) { return false; } - // So we're looking at indexable a paragraph now + const fullText = renderToText(p); + + // Collect tags and remove from the tree const tags = new Set(); collectNodesOfType(p, "Hashtag").forEach((tagNode) => { tags.add(tagNode.children![0].text!.substring(1)); @@ -54,9 +56,9 @@ export async function indexParagraphs({ name: page, tree }: IndexTreeEvent) { const pos = p.from!; const paragraph: ParagraphObject = { - ref: `${page}@${pos}`, - text, tag: "paragraph", + ref: `${page}@${pos}`, + text: fullText, page, pos, ...attrs, diff --git a/plugs/tasks/task.ts b/plugs/tasks/task.ts index 35c58993..16bb82be 100644 --- a/plugs/tasks/task.ts +++ b/plugs/tasks/task.ts @@ -28,6 +28,7 @@ export type TaskObject = ObjectValue< page: string; pos: number; name: string; + text: string; done: boolean; state: string; deadline?: string; @@ -70,6 +71,7 @@ export async function indexTasks({ name, tree }: IndexTreeEvent) { ref: `${name}@${n.from}`, tag: "task", name: "", + text: "", done: complete, page: name, pos: n.from!, @@ -78,6 +80,10 @@ export async function indexTasks({ name, tree }: IndexTreeEvent) { rewritePageRefs(n, name); + // The task text is everything after the task marker + task.text = n.children!.slice(1).map(renderToText).join("").trim(); + + // This finds the deadline and tags, and removes them from the tree replaceNodesMatching(n, (tree) => { if (tree.type === "DeadlineDate") { task.deadline = getDeadline(tree); @@ -96,7 +102,6 @@ export async function indexTasks({ name, tree }: IndexTreeEvent) { }); // Extract attributes and remove from tree - task.name = n.children!.slice(1).map(renderToText).join("").trim(); const extractedAttributes = await extractAttributes( ["task", ...task.tags || []], n, diff --git a/website/CHANGELOG.md b/website/CHANGELOG.md index 60ed7589..8a7ce3c6 100644 --- a/website/CHANGELOG.md +++ b/website/CHANGELOG.md @@ -16,6 +16,7 @@ _These features are not yet properly released, you need to use [the edge builds] * Hide `\` escapes in [[Live Preview]] (by [MrMugame](https://github.com/silverbulletmd/silverbullet/pull/901)) * Added Erlang [[Markdown/Syntax Highlighting]] * Dates (formatted as e.g. `2023-07-01` or `2023-07-01 23:33`) in [[Frontmatter]] are now converted into strings (rather than empty objects) +* `task` and `item` objects now have an additional `text` attribute that contains the full text of the item and/or task, with any [[Attributes]] and [[Tags]] intact (whereas they are removed from `name`) * Numerous other bug fixes (thanks MrMugame and onespaceman) ---