2023-12-29 17:59:42 +00:00
|
|
|
import { markdown, space,, YAML } from "$sb/syscalls.ts";
|
2023-11-06 08:14:16 +00:00
|
|
|
import { loadPageObject, replaceTemplateVars } from "../template/template.ts";
|
2023-12-29 17:59:42 +00:00
|
|
|
import { CodeWidgetContent, PageMeta } from "$sb/types.ts";
|
2023-11-15 13:56:34 +00:00
|
|
|
import { renderTemplate } from "../template/plug_api.ts";
|
2023-11-15 15:14:15 +00:00
|
|
|
import { renderToText } from "$sb/lib/tree.ts";
|
2023-11-16 12:55:02 +00:00
|
|
|
import { rewritePageRefs, rewritePageRefsInString } from "$sb/lib/resolve.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
|
|
|
|
type TemplateConfig = {
|
|
|
|
// Pull the template from a page
|
|
|
|
page?: string;
|
|
|
|
// Or use a string directly
|
|
|
|
template?: string;
|
|
|
|
// Optional argument to pass
|
|
|
|
value?: any;
|
|
|
|
// If true, don't render the template, just use it as-is
|
|
|
|
raw?: boolean;
|
|
|
|
};
|
|
|
|
|
2023-11-02 11:51:09 +00:00
|
|
|
export async function widget(
|
|
|
|
bodyText: string,
|
|
|
|
pageName: string,
|
2023-12-29 17:59:42 +00:00
|
|
|
): Promise<CodeWidgetContent> {
|
2023-11-06 08:14:16 +00:00
|
|
|
const pageMeta: PageMeta = await loadPageObject(pageName);
|
2023-10-03 12:16:33 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const config: TemplateConfig = await YAML.parse(bodyText);
|
|
|
|
let templateText = config.template || "";
|
2023-10-03 16:09:03 +00:00
|
|
|
let templatePage = config.page;
|
|
|
|
if (templatePage) {
|
2023-11-16 12:55:02 +00:00
|
|
|
// Rewrite federation page references
|
|
|
|
templatePage = rewritePageRefsInString(templatePage, pageName);
|
2023-10-03 16:09:03 +00:00
|
|
|
if (templatePage.startsWith("[[")) {
|
|
|
|
templatePage = templatePage.slice(2, -2);
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
2023-11-19 11:18:16 +00:00
|
|
|
if (!templatePage) {
|
|
|
|
throw new Error("No template page specified");
|
|
|
|
}
|
2023-10-03 16:09:03 +00:00
|
|
|
templateText = await space.readPage(templatePage);
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 12:40:09 +00:00
|
|
|
const value = config.value
|
|
|
|
? JSON.parse(
|
|
|
|
await replaceTemplateVars(JSON.stringify(config.value), pageMeta),
|
|
|
|
)
|
|
|
|
: undefined;
|
|
|
|
|
2023-11-15 13:56:34 +00:00
|
|
|
let { text: rendered } = config.raw
|
|
|
|
? { text: templateText }
|
|
|
|
: await renderTemplate(
|
|
|
|
templateText,
|
|
|
|
pageMeta,
|
|
|
|
value,
|
|
|
|
);
|
2023-10-03 16:09:03 +00:00
|
|
|
|
|
|
|
if (templatePage) {
|
2023-10-29 09:02:50 +00:00
|
|
|
const parsedMarkdown = await markdown.parseMarkdown(rendered);
|
2023-10-03 16:09:03 +00:00
|
|
|
rewritePageRefs(parsedMarkdown, templatePage);
|
2023-10-29 09:02:50 +00:00
|
|
|
rendered = renderToText(parsedMarkdown);
|
2023-10-03 16:09:03 +00:00
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
|
2023-12-29 17:59:42 +00:00
|
|
|
return {
|
|
|
|
markdown: rendered,
|
|
|
|
buttons: [{
|
|
|
|
description: "Edit",
|
|
|
|
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-edit"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path></svg>`,
|
|
|
|
invokeFunction: "query.editButton",
|
|
|
|
}, {
|
|
|
|
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: "query.refreshAllWidgets",
|
|
|
|
}],
|
|
|
|
};
|
2023-10-03 12:16:33 +00:00
|
|
|
} catch (e: any) {
|
2023-12-27 12:46:45 +00:00
|
|
|
return {
|
|
|
|
markdown: `**Error:** ${e.message}`,
|
|
|
|
};
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
|
|
|
}
|