Work on inline attributes

This commit is contained in:
Zef Hemel
2023-07-24 19:54:31 +02:00
parent a1c623b1f5
commit 2b494f263e
12 changed files with 211 additions and 16 deletions
+12 -5
View File
@@ -3,6 +3,7 @@ import type { IndexTreeEvent, QueryProviderEvent } from "$sb/app_event.ts";
import { index } from "$sb/silverbullet-syscall/mod.ts";
import { collectNodesOfType, ParseTree, renderToText } from "$sb/lib/tree.ts";
import { applyQuery, removeQueries } from "$sb/lib/query.ts";
import { extractAttributes } from "$sb/lib/attribute.ts";
export type Item = {
name: string;
@@ -11,7 +12,7 @@ export type Item = {
// Not stored in DB
page?: string;
pos?: number;
};
} & Record<string, any>;
export async function indexItems({ name, tree }: IndexTreeEvent) {
const items: { key: string; value: Item }[] = [];
@@ -30,6 +31,10 @@ export async function indexItems({ name, tree }: IndexTreeEvent) {
return;
}
const item: Item = {
name: "", // to be replaced
};
const textNodes: ParseTree[] = [];
let nested: string | undefined;
for (const child of n.children!.slice(1)) {
@@ -37,13 +42,15 @@ export async function indexItems({ name, tree }: IndexTreeEvent) {
nested = renderToText(child);
break;
}
// Extract attributes and remove from tree
const extractedAttributes = extractAttributes(child, true);
for (const [key, value] of Object.entries(extractedAttributes)) {
item[key] = value;
}
textNodes.push(child);
}
const itemText = textNodes.map(renderToText).join("").trim();
const item: Item = {
name: itemText,
};
item.name = textNodes.map(renderToText).join("").trim();
if (nested) {
item.nested = nested;
}
+10 -2
View File
@@ -25,6 +25,7 @@ import { applyQuery } from "$sb/lib/query.ts";
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
import { invokeFunction } from "$sb/silverbullet-syscall/system.ts";
import { isValidPageName } from "$sb/lib/page.ts";
import { extractAttributes } from "$sb/lib/attribute.ts";
// Key space:
// l:toPage:pos => {name: pageName, inDirective: true}
@@ -42,14 +43,21 @@ export async function indexLinks({ name, tree }: IndexTreeEvent) {
// [[Style Links]]
// console.log("Now indexing links for", name);
const pageMeta = await extractFrontmatter(tree);
if (Object.keys(pageMeta).length > 0) {
// console.log("Extracted page meta data", pageMeta);
const toplevelAttributes = extractAttributes(tree, false);
if (
Object.keys(pageMeta).length > 0 ||
Object.keys(toplevelAttributes).length > 0
) {
for (const [k, v] of Object.entries(toplevelAttributes)) {
pageMeta[k] = v;
}
// Don't index meta data starting with $
for (const key in pageMeta) {
if (key.startsWith("$")) {
delete pageMeta[key];
}
}
console.log("Extracted page meta data", pageMeta);
await index.set(name, "meta:", pageMeta);
}
+12
View File
@@ -11,6 +11,7 @@ type MarkdownRenderOptions = {
smartHardBreak?: true;
annotationPositions?: true;
attachmentUrlPrefix?: string;
preserveAttributes?: true;
// When defined, use to inline images as data: urls
translateUrls?: (url: string) => string;
};
@@ -345,6 +346,17 @@ function render(
const body = findNodeOfType(t, "DirectiveBody")!;
return posPreservingRender(body.children![0], options);
}
case "Attribute":
if (options.preserveAttributes) {
return {
name: "span",
attrs: {
class: "attribute",
},
body: renderToText(t),
};
}
return null;
// Text
case undefined:
return t.text!;
+8 -1
View File
@@ -23,6 +23,7 @@ import {
} from "$sb/lib/tree.ts";
import { applyQuery, removeQueries } from "$sb/lib/query.ts";
import { niceDate } from "$sb/lib/dates.ts";
import { extractAttributes } from "$sb/lib/attribute.ts";
export type Task = {
name: string;
@@ -33,7 +34,7 @@ export type Task = {
// Not saved in DB, just added when pulled out (from key)
pos?: number;
page?: string;
};
} & Record<string, any>;
function getDeadline(deadlineNode: ParseTree): string {
return deadlineNode.children![0].text!.replace(/📅\s*/, "");
@@ -67,6 +68,12 @@ export async function indexTasks({ name, tree }: IndexTreeEvent) {
}
});
// Extract attributes and remove from tree
const extractedAttributes = extractAttributes(n, true);
for (const [key, value] of Object.entries(extractedAttributes)) {
task[key] = value;
}
task.name = n.children!.slice(1).map(renderToText).join("").trim();
const taskIndex = n.parent!.children!.indexOf(n);