2024-01-08 16:08:26 +00:00
|
|
|
import {
|
|
|
|
codeWidget,
|
|
|
|
editor,
|
|
|
|
language,
|
|
|
|
markdown,
|
|
|
|
space,
|
|
|
|
} from "$sb/silverbullet-syscall/mod.ts";
|
|
|
|
import { parseTreeToAST, renderToText } from "$sb/lib/tree.ts";
|
2024-01-04 19:08:12 +00:00
|
|
|
import { CodeWidgetContent } from "$sb/types.ts";
|
2024-01-08 16:08:26 +00:00
|
|
|
import { loadPageObject } from "../template/template.ts";
|
2024-01-04 19:08:12 +00:00
|
|
|
import { queryObjects } from "./api.ts";
|
|
|
|
import { TemplateObject } from "../template/types.ts";
|
|
|
|
import { expressionToKvQueryExpression } from "$sb/lib/parse-query.ts";
|
|
|
|
import { evalQueryExpression } from "$sb/lib/query.ts";
|
2024-01-08 16:08:26 +00:00
|
|
|
import { renderTemplate } from "../template/plug_api.ts";
|
|
|
|
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
|
|
|
|
import { rewritePageRefs } from "$sb/lib/resolve.ts";
|
2024-01-04 19:08:12 +00:00
|
|
|
|
2024-01-08 16:08:26 +00:00
|
|
|
export async function refreshWidgets() {
|
|
|
|
await codeWidget.refreshAll();
|
|
|
|
}
|
2024-01-04 19:08:12 +00:00
|
|
|
|
2024-01-08 16:08:26 +00:00
|
|
|
export async function renderTemplateWidgets(side: "top" | "bottom"): Promise<
|
2024-01-04 19:08:12 +00:00
|
|
|
CodeWidgetContent | null
|
|
|
|
> {
|
|
|
|
const text = await editor.getText();
|
|
|
|
const pageMeta = await loadPageObject(await editor.getCurrentPage());
|
|
|
|
const parsedMd = await markdown.parseMarkdown(text);
|
|
|
|
const frontmatter = await extractFrontmatter(parsedMd);
|
|
|
|
|
|
|
|
const allFrontMatterTemplates = await queryObjects<TemplateObject>(
|
|
|
|
"template",
|
|
|
|
{
|
2024-01-11 13:06:24 +00:00
|
|
|
// where type = "widget:X" and enabled != false
|
|
|
|
filter: ["and", ["=", ["attr", "type"], ["string", `widget:${side}`]], [
|
|
|
|
"!=",
|
|
|
|
["attr", "enabled"],
|
|
|
|
["boolean", false],
|
|
|
|
]],
|
2024-01-04 19:08:12 +00:00
|
|
|
orderBy: [{ expr: ["attr", "priority"], desc: false }],
|
|
|
|
},
|
|
|
|
);
|
2024-01-08 16:08:26 +00:00
|
|
|
const templateBits: string[] = [];
|
2024-01-04 19:08:12 +00:00
|
|
|
// Strategy: walk through all matching templates, evaluate the 'where' expression, and pick the first one that matches
|
|
|
|
for (const template of allFrontMatterTemplates) {
|
2024-01-09 08:37:06 +00:00
|
|
|
if (!template.where) {
|
|
|
|
console.warn(
|
|
|
|
"Skipping template",
|
|
|
|
template.ref,
|
|
|
|
"because it has no 'where' expression",
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-04 19:08:12 +00:00
|
|
|
const exprAST = parseTreeToAST(
|
|
|
|
await language.parseLanguage("expression", template.where!),
|
|
|
|
);
|
|
|
|
const parsedExpression = expressionToKvQueryExpression(exprAST[1]);
|
|
|
|
if (evalQueryExpression(parsedExpression, pageMeta)) {
|
|
|
|
// Match! We're happy
|
2024-01-08 16:08:26 +00:00
|
|
|
const templateText = await space.readPage(template.ref);
|
|
|
|
let renderedTemplate = (await renderTemplate(
|
|
|
|
templateText,
|
|
|
|
pageMeta,
|
|
|
|
frontmatter,
|
|
|
|
)).text;
|
|
|
|
|
|
|
|
const parsedMarkdown = await markdown.parseMarkdown(renderedTemplate);
|
|
|
|
rewritePageRefs(parsedMarkdown, template.ref);
|
|
|
|
renderedTemplate = renderToText(parsedMarkdown);
|
|
|
|
|
2024-01-09 08:37:06 +00:00
|
|
|
templateBits.push(renderedTemplate.trim());
|
2024-01-04 19:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-09 08:37:06 +00:00
|
|
|
const summaryText = templateBits.join("\n");
|
2024-01-04 19:08:12 +00:00
|
|
|
// console.log("Rendered", summaryText);
|
|
|
|
return {
|
2024-01-08 16:08:26 +00:00
|
|
|
markdown: summaryText,
|
2024-01-04 19:08:12 +00:00
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
description: "Reload",
|
|
|
|
svg:
|
|
|
|
`<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-refresh-cw"><polyline points="23 4 23 10 17 10"></polyline><polyline points="1 20 1 14 7 14"></polyline><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"></path></svg>`,
|
|
|
|
invokeFunction: "index.refreshWidgets",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|