2023-10-03 12:16:33 +00:00
|
|
|
import type { WidgetContent } from "$sb/app_event.ts";
|
2023-10-31 09:33:38 +00:00
|
|
|
import { events, language, space, system } from "$sb/syscalls.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { parseTreeToAST } from "$sb/lib/tree.ts";
|
|
|
|
import { astToKvQuery } from "$sb/lib/parse-query.ts";
|
2023-11-09 08:26:44 +00:00
|
|
|
import { jsonToMDTable, renderQueryTemplate } from "../directive/util.ts";
|
2023-11-06 08:14:16 +00:00
|
|
|
import { loadPageObject, replaceTemplateVars } from "../template/template.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
|
2023-10-31 09:33:38 +00:00
|
|
|
export async function widget(
|
|
|
|
bodyText: string,
|
|
|
|
pageName: string,
|
|
|
|
): Promise<WidgetContent> {
|
2023-11-06 08:14:16 +00:00
|
|
|
const pageObject = await loadPageObject(pageName);
|
2023-10-03 12:16:33 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const queryAST = parseTreeToAST(
|
2023-10-06 12:40:09 +00:00
|
|
|
await language.parseLanguage(
|
|
|
|
"query",
|
2023-11-06 08:14:16 +00:00
|
|
|
await replaceTemplateVars(bodyText, pageObject),
|
2023-10-03 12:16:33 +00:00
|
|
|
),
|
|
|
|
);
|
2023-10-06 12:40:09 +00:00
|
|
|
const parsedQuery = astToKvQuery(queryAST[1]);
|
2023-10-03 12:16:33 +00:00
|
|
|
|
|
|
|
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,
|
2023-11-06 08:14:16 +00:00
|
|
|
{ query: parsedQuery, pageName: pageObject.name },
|
2023-10-03 12:16:33 +00:00
|
|
|
30 * 1000,
|
|
|
|
);
|
|
|
|
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}'`,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const allResults = results.flat();
|
|
|
|
if (allResults.length === 0) {
|
|
|
|
resultMarkdown = "No results";
|
|
|
|
} else {
|
|
|
|
if (parsedQuery.render) {
|
|
|
|
// Configured a custom rendering template, let's use it!
|
2023-11-09 08:26:44 +00:00
|
|
|
const rendered = await renderQueryTemplate(
|
2023-11-06 08:14:16 +00:00
|
|
|
pageObject,
|
2023-10-03 12:16:33 +00:00
|
|
|
parsedQuery.render,
|
|
|
|
allResults,
|
2023-10-30 13:15:12 +00:00
|
|
|
parsedQuery.renderAll!,
|
2023-10-03 12:16:33 +00:00
|
|
|
);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 09:02:50 +00:00
|
|
|
return system.invokeFunction(
|
|
|
|
"markdown.markdownContentWidget",
|
|
|
|
resultMarkdown,
|
2023-11-03 11:01:33 +00:00
|
|
|
pageName,
|
2023-10-29 09:02:50 +00:00
|
|
|
);
|
2023-10-03 12:16:33 +00:00
|
|
|
} catch (e: any) {
|
2023-10-29 09:02:50 +00:00
|
|
|
return system.invokeFunction(
|
|
|
|
"markdown.markdownContentWidget",
|
|
|
|
`**Error:** ${e.message}`,
|
|
|
|
);
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
|
|
|
}
|