2023-12-22 12:59:16 +00:00
|
|
|
import { determineTags } from "$sb/lib/cheap_yaml.ts";
|
2024-01-02 13:47:02 +00:00
|
|
|
import { handlebarHelpers } from "../../common/syscalls/handlebar_helpers.ts";
|
|
|
|
import { PageMeta } from "$sb/types.ts";
|
|
|
|
import { handlebars, space } from "$sb/syscalls.ts";
|
|
|
|
import { cleanTemplate } from "./plug_api.ts";
|
2023-12-22 12:59:16 +00:00
|
|
|
|
2023-11-09 08:26:44 +00:00
|
|
|
const frontMatterRegex = /^---\n(([^\n]|\n)*?)---\n/;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quick and dirty way to check if a page is a template or not
|
|
|
|
* @param pageText
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function isTemplate(pageText: string): boolean {
|
|
|
|
const frontmatter = frontMatterRegex.exec(pageText);
|
|
|
|
// Poor man's YAML frontmatter parsing
|
|
|
|
if (frontmatter) {
|
|
|
|
pageText = pageText.slice(frontmatter[0].length);
|
|
|
|
const frontmatterText = frontmatter[1];
|
2023-12-22 12:59:16 +00:00
|
|
|
const tags = determineTags(frontmatterText);
|
|
|
|
if (tags.includes("template")) {
|
|
|
|
return true;
|
2023-11-09 08:26:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Or if the page text starts with a #template tag
|
|
|
|
if (/^\s*#template(\W|$)/.test(pageText)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-02 13:47:02 +00:00
|
|
|
|
|
|
|
export function buildHandebarOptions(pageMeta: PageMeta) {
|
|
|
|
return {
|
|
|
|
helpers: handlebarHelpers(),
|
|
|
|
data: { page: pageMeta },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-04 19:08:12 +00:00
|
|
|
export function defaultJsonTransformer(v: any): string {
|
2024-01-02 13:47:02 +00:00
|
|
|
if (v === undefined) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
if (typeof v === "string") {
|
|
|
|
return v.replaceAll("\n", " ").replaceAll("|", "\\|");
|
|
|
|
}
|
2024-01-04 19:08:12 +00:00
|
|
|
if (Array.isArray(v)) {
|
|
|
|
return v.map(defaultJsonTransformer).join(", ");
|
|
|
|
} else if (typeof v === "object") {
|
|
|
|
return Object.entries(v).map(([k, v]: [string, any]) =>
|
|
|
|
`${k}: ${defaultJsonTransformer(v)}`
|
|
|
|
).join(", ");
|
|
|
|
}
|
2024-01-02 13:47:02 +00:00
|
|
|
return "" + v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nicely format an array of JSON objects as a Markdown table
|
|
|
|
export function jsonToMDTable(
|
|
|
|
jsonArray: any[],
|
2024-01-04 19:08:12 +00:00
|
|
|
valueTransformer: (v: any) => string = defaultJsonTransformer,
|
2024-01-02 13:47:02 +00:00
|
|
|
): string {
|
|
|
|
const headers = new Set<string>();
|
|
|
|
for (const entry of jsonArray) {
|
|
|
|
for (const k of Object.keys(entry)) {
|
|
|
|
headers.add(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const headerList = [...headers];
|
|
|
|
const lines = [];
|
|
|
|
lines.push(
|
|
|
|
"|" +
|
|
|
|
headerList
|
|
|
|
.map(
|
|
|
|
(headerName) => headerName,
|
|
|
|
)
|
|
|
|
.join("|") +
|
|
|
|
"|",
|
|
|
|
);
|
|
|
|
lines.push(
|
|
|
|
"|" +
|
|
|
|
headerList
|
|
|
|
.map(() => "--")
|
|
|
|
.join("|") +
|
|
|
|
"|",
|
|
|
|
);
|
|
|
|
for (const val of jsonArray) {
|
|
|
|
const el = [];
|
|
|
|
for (const prop of headerList) {
|
2024-01-04 19:08:12 +00:00
|
|
|
const s = valueTransformer(val[prop]);
|
2024-01-02 13:47:02 +00:00
|
|
|
el.push(s);
|
|
|
|
}
|
|
|
|
lines.push("|" + el.join("|") + "|");
|
|
|
|
}
|
|
|
|
return lines.join("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function renderQueryTemplate(
|
|
|
|
pageMeta: PageMeta,
|
|
|
|
templatePage: string,
|
|
|
|
data: any[],
|
|
|
|
renderAll: boolean,
|
|
|
|
): Promise<string> {
|
|
|
|
let templateText = await space.readPage(templatePage);
|
|
|
|
templateText = await cleanTemplate(templateText);
|
|
|
|
|
|
|
|
if (!renderAll) {
|
|
|
|
templateText = `{{#each .}}\n${templateText}\n{{/each}}`;
|
|
|
|
}
|
|
|
|
return handlebars.renderTemplate(templateText, data, { page: pageMeta });
|
|
|
|
}
|