2023-10-03 12:16:33 +00:00
|
|
|
import { handlebars, space } from "$sb/syscalls.ts";
|
|
|
|
import { handlebarHelpers } from "../../common/syscalls/handlebar_helpers.ts";
|
|
|
|
import { PageMeta } from "$sb/types.ts";
|
2023-11-15 15:14:15 +00:00
|
|
|
import { cleanTemplate } from "../template/plug_api.ts";
|
2022-10-28 14:17:40 +00:00
|
|
|
|
2022-11-20 09:24:42 +00:00
|
|
|
export function defaultJsonTransformer(_k: string, v: any) {
|
|
|
|
if (v === undefined) {
|
|
|
|
return "";
|
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
if (typeof v === "string") {
|
|
|
|
return v.replaceAll("\n", " ").replaceAll("|", "\\|");
|
|
|
|
}
|
2022-11-20 09:24:42 +00:00
|
|
|
return "" + v;
|
|
|
|
}
|
|
|
|
|
2022-04-21 09:46:33 +00:00
|
|
|
// Nicely format an array of JSON objects as a Markdown table
|
|
|
|
export function jsonToMDTable(
|
|
|
|
jsonArray: any[],
|
2022-11-20 09:24:42 +00:00
|
|
|
valueTransformer: (k: string, v: any) => string = defaultJsonTransformer,
|
2022-04-21 09:46:33 +00:00
|
|
|
): string {
|
2023-10-29 11:09:12 +00:00
|
|
|
const headers = new Set<string>();
|
2022-10-15 17:02:56 +00:00
|
|
|
for (const entry of jsonArray) {
|
|
|
|
for (const k of Object.keys(entry)) {
|
2023-10-29 11:09:12 +00:00
|
|
|
headers.add(k);
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-29 11:37:31 +00:00
|
|
|
|
2023-10-29 11:09:12 +00:00
|
|
|
const headerList = [...headers];
|
2022-10-14 13:11:33 +00:00
|
|
|
const lines = [];
|
2022-04-29 11:37:31 +00:00
|
|
|
lines.push(
|
|
|
|
"|" +
|
|
|
|
headerList
|
|
|
|
.map(
|
2023-10-29 11:09:12 +00:00
|
|
|
(headerName) => headerName,
|
2022-04-29 11:37:31 +00:00
|
|
|
)
|
|
|
|
.join("|") +
|
2022-10-12 09:47:13 +00:00
|
|
|
"|",
|
2022-04-29 11:37:31 +00:00
|
|
|
);
|
|
|
|
lines.push(
|
|
|
|
"|" +
|
|
|
|
headerList
|
2023-10-29 11:09:12 +00:00
|
|
|
.map(() => "--")
|
2022-04-29 11:37:31 +00:00
|
|
|
.join("|") +
|
2022-10-12 09:47:13 +00:00
|
|
|
"|",
|
2022-04-29 11:37:31 +00:00
|
|
|
);
|
2022-04-21 09:46:33 +00:00
|
|
|
for (const val of jsonArray) {
|
2022-10-15 17:02:56 +00:00
|
|
|
const el = [];
|
|
|
|
for (const prop of headerList) {
|
|
|
|
const s = valueTransformer(prop, val[prop]);
|
2023-10-29 11:09:12 +00:00
|
|
|
el.push(s);
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
lines.push("|" + el.join("|") + "|");
|
|
|
|
}
|
|
|
|
return lines.join("\n");
|
|
|
|
}
|
2022-10-28 14:17:40 +00:00
|
|
|
|
2023-11-09 08:26:44 +00:00
|
|
|
export async function renderQueryTemplate(
|
2023-07-02 09:25:32 +00:00
|
|
|
pageMeta: PageMeta,
|
2023-11-09 08:26:44 +00:00
|
|
|
templatePage: string,
|
2022-10-28 14:17:40 +00:00
|
|
|
data: any[],
|
2023-10-30 13:15:12 +00:00
|
|
|
renderAll: boolean,
|
2022-10-28 14:17:40 +00:00
|
|
|
): Promise<string> {
|
2023-11-09 08:26:44 +00:00
|
|
|
let templateText = await space.readPage(templatePage);
|
|
|
|
templateText = await cleanTemplate(templateText);
|
2023-11-15 15:14:15 +00:00
|
|
|
|
2023-10-30 13:15:12 +00:00
|
|
|
if (!renderAll) {
|
|
|
|
templateText = `{{#each .}}\n${templateText}\n{{/each}}`;
|
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
return handlebars.renderTemplate(templateText, data, { page: pageMeta });
|
2023-05-23 18:53:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-02 09:25:32 +00:00
|
|
|
export function buildHandebarOptions(pageMeta: PageMeta) {
|
|
|
|
return {
|
2023-10-03 12:16:33 +00:00
|
|
|
helpers: handlebarHelpers(),
|
2023-07-02 09:25:32 +00:00
|
|
|
data: { page: pageMeta },
|
|
|
|
};
|
|
|
|
}
|