Rebuilt frontmatter templates as template widgets
This commit is contained in:
+44
-40
@@ -4,7 +4,12 @@ import { findNodeOfType, traverseTreeAsync } from "$sb/lib/tree.ts";
|
||||
import { parseQuery } from "$sb/lib/parse-query.ts";
|
||||
import { loadPageObject, replaceTemplateVars } from "../template/template.ts";
|
||||
import { cleanPageRef, resolvePath } from "$sb/lib/resolve.ts";
|
||||
import { CodeWidgetContent, LintDiagnostic } from "$sb/types.ts";
|
||||
import {
|
||||
CodeWidgetContent,
|
||||
LintDiagnostic,
|
||||
PageMeta,
|
||||
Query,
|
||||
} from "$sb/types.ts";
|
||||
import { jsonToMDTable, renderQueryTemplate } from "../template/util.ts";
|
||||
|
||||
export async function widget(
|
||||
@@ -12,53 +17,33 @@ export async function widget(
|
||||
pageName: string,
|
||||
): Promise<CodeWidgetContent> {
|
||||
const pageObject = await loadPageObject(pageName);
|
||||
|
||||
try {
|
||||
let resultMarkdown = "";
|
||||
const parsedQuery = await parseQuery(
|
||||
await replaceTemplateVars(bodyText, pageObject),
|
||||
);
|
||||
|
||||
if (!parsedQuery.limit) {
|
||||
parsedQuery.limit = ["number", 1000];
|
||||
}
|
||||
|
||||
const eventName = `query:${parsedQuery.querySource}`;
|
||||
|
||||
let resultMarkdown = "";
|
||||
|
||||
// console.log("Parsed query", parsedQuery);
|
||||
// Let's dispatch an event and see what happens
|
||||
const results = await events.dispatchEvent(
|
||||
eventName,
|
||||
{ query: parsedQuery, pageName: pageObject.name },
|
||||
30 * 1000,
|
||||
const results = await performQuery(
|
||||
parsedQuery,
|
||||
pageObject,
|
||||
);
|
||||
if (results.length === 0) {
|
||||
// This means there was no handler for the event which means it's unsupported
|
||||
return {
|
||||
html:
|
||||
`**Error:** Unsupported query source '${parsedQuery.querySource}'`,
|
||||
};
|
||||
if (results.length === 0 && !parsedQuery.renderAll) {
|
||||
resultMarkdown = "No results";
|
||||
} else {
|
||||
const allResults = results.flat();
|
||||
if (allResults.length === 0) {
|
||||
resultMarkdown = "No results";
|
||||
if (parsedQuery.render) {
|
||||
// Configured a custom rendering template, let's use it!
|
||||
const templatePage = resolvePath(pageName, parsedQuery.render);
|
||||
const rendered = await renderQueryTemplate(
|
||||
pageObject,
|
||||
templatePage,
|
||||
results,
|
||||
parsedQuery.renderAll!,
|
||||
);
|
||||
resultMarkdown = rendered.trim();
|
||||
} else {
|
||||
if (parsedQuery.render) {
|
||||
// Configured a custom rendering template, let's use it!
|
||||
const templatePage = resolvePath(pageName, parsedQuery.render);
|
||||
const rendered = await renderQueryTemplate(
|
||||
pageObject,
|
||||
templatePage,
|
||||
allResults,
|
||||
parsedQuery.renderAll!,
|
||||
);
|
||||
resultMarkdown = rendered.trim();
|
||||
} else {
|
||||
// TODO: At this point it's a bit pointless to first render a markdown table, and then convert that to HTML
|
||||
// We should just render the HTML table directly
|
||||
resultMarkdown = jsonToMDTable(allResults);
|
||||
}
|
||||
// TODO: At this point it's a bit pointless to first render a markdown table, and then convert that to HTML
|
||||
// We should just render the HTML table directly
|
||||
resultMarkdown = jsonToMDTable(results);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,6 +69,25 @@ export async function widget(
|
||||
}
|
||||
}
|
||||
|
||||
export async function performQuery(parsedQuery: Query, pageObject: PageMeta) {
|
||||
if (!parsedQuery.limit) {
|
||||
parsedQuery.limit = ["number", 1000];
|
||||
}
|
||||
|
||||
const eventName = `query:${parsedQuery.querySource}`;
|
||||
// console.log("Parsed query", parsedQuery);
|
||||
// Let's dispatch an event and see what happens
|
||||
const results = await events.dispatchEvent(
|
||||
eventName,
|
||||
{ query: parsedQuery, pageName: pageObject.name },
|
||||
30 * 1000,
|
||||
);
|
||||
if (results.length === 0) {
|
||||
throw new Error(`Unsupported query source '${parsedQuery.querySource}'`);
|
||||
}
|
||||
return results.flat();
|
||||
}
|
||||
|
||||
export async function lintQuery(
|
||||
{ name, tree }: LintEvent,
|
||||
): Promise<LintDiagnostic[]> {
|
||||
|
||||
+20
-5
@@ -4,14 +4,20 @@ import { CodeWidgetContent, PageMeta } from "$sb/types.ts";
|
||||
import { renderTemplate } from "../template/plug_api.ts";
|
||||
import { renderToText } from "$sb/lib/tree.ts";
|
||||
import { rewritePageRefs, rewritePageRefsInString } from "$sb/lib/resolve.ts";
|
||||
import { performQuery } from "./query.ts";
|
||||
import { parseQuery } from "$sb/lib/parse-query.ts";
|
||||
|
||||
type TemplateConfig = {
|
||||
// Pull the template from a page
|
||||
page?: string;
|
||||
// Or use a string directly
|
||||
template?: string;
|
||||
// Optional argument to pass
|
||||
// To feed data into the template you can either use a concrete value
|
||||
value?: any;
|
||||
|
||||
// Or a query
|
||||
query?: string;
|
||||
|
||||
// If true, don't render the template, just use it as-is
|
||||
raw?: boolean;
|
||||
};
|
||||
@@ -38,11 +44,20 @@ export async function widget(
|
||||
templateText = await space.readPage(templatePage);
|
||||
}
|
||||
|
||||
const value = config.value
|
||||
? JSON.parse(
|
||||
let value: any;
|
||||
|
||||
if (config.value) {
|
||||
value = JSON.parse(
|
||||
await replaceTemplateVars(JSON.stringify(config.value), pageMeta),
|
||||
)
|
||||
: undefined;
|
||||
);
|
||||
}
|
||||
|
||||
if (config.query) {
|
||||
const parsedQuery = await parseQuery(
|
||||
await replaceTemplateVars(config.query, pageMeta),
|
||||
);
|
||||
value = await performQuery(parsedQuery, pageMeta);
|
||||
}
|
||||
|
||||
let { text: rendered } = config.raw
|
||||
? { text: templateText }
|
||||
|
||||
Reference in New Issue
Block a user