2022-10-28 14:17:40 +00:00
|
|
|
import Handlebars from "handlebars";
|
|
|
|
|
|
|
|
import { space } from "$sb/silverbullet-syscall/mod.ts";
|
|
|
|
import { niceDate } from "$sb/lib/dates.ts";
|
2023-07-02 09:25:32 +00:00
|
|
|
import { PageMeta } from "../../web/types.ts";
|
2022-10-28 14:17:40 +00:00
|
|
|
|
2022-04-29 11:37:31 +00:00
|
|
|
const maxWidth = 70;
|
2022-11-20 09:24:42 +00:00
|
|
|
|
|
|
|
export function defaultJsonTransformer(_k: string, v: any) {
|
|
|
|
if (v === undefined) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
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 {
|
2022-10-14 13:11:33 +00:00
|
|
|
const fieldWidths = new Map<string, number>();
|
2022-10-15 17:02:56 +00:00
|
|
|
for (const entry of jsonArray) {
|
|
|
|
for (const k of Object.keys(entry)) {
|
2022-04-29 11:37:31 +00:00
|
|
|
let fieldWidth = fieldWidths.get(k);
|
|
|
|
if (!fieldWidth) {
|
|
|
|
fieldWidth = valueTransformer(k, entry[k]).length;
|
|
|
|
} else {
|
|
|
|
fieldWidth = Math.max(valueTransformer(k, entry[k]).length, fieldWidth);
|
|
|
|
}
|
|
|
|
fieldWidths.set(k, fieldWidth);
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-29 11:37:31 +00:00
|
|
|
|
|
|
|
let fullWidth = 0;
|
2022-10-15 17:02:56 +00:00
|
|
|
for (const v of fieldWidths.values()) {
|
2022-04-29 11:37:31 +00:00
|
|
|
fullWidth += v + 1;
|
|
|
|
}
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const headerList = [...fieldWidths.keys()];
|
|
|
|
const lines = [];
|
2022-04-29 11:37:31 +00:00
|
|
|
lines.push(
|
|
|
|
"|" +
|
|
|
|
headerList
|
|
|
|
.map(
|
|
|
|
(headerName) =>
|
|
|
|
headerName +
|
2022-10-12 09:47:13 +00:00
|
|
|
charPad(" ", fieldWidths.get(headerName)! - headerName.length),
|
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
|
|
|
|
.map((title) => charPad("-", fieldWidths.get(title)!))
|
|
|
|
.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]);
|
2022-04-29 11:37:31 +00:00
|
|
|
el.push(s + charPad(" ", fieldWidths.get(prop)! - s.length));
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
lines.push("|" + el.join("|") + "|");
|
|
|
|
}
|
|
|
|
return lines.join("\n");
|
2022-04-29 11:37:31 +00:00
|
|
|
|
|
|
|
function charPad(ch: string, length: number) {
|
|
|
|
if (fullWidth > maxWidth && ch === "") {
|
|
|
|
return "";
|
|
|
|
} else if (fullWidth > maxWidth && ch === "-") {
|
|
|
|
return "--";
|
|
|
|
}
|
|
|
|
if (length < 1) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return new Array(length + 1).join(ch);
|
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
2022-10-28 14:17:40 +00:00
|
|
|
|
|
|
|
export async function renderTemplate(
|
2023-07-02 09:25:32 +00:00
|
|
|
pageMeta: PageMeta,
|
2022-10-28 14:17:40 +00:00
|
|
|
renderTemplate: string,
|
|
|
|
data: any[],
|
|
|
|
): Promise<string> {
|
2023-05-23 18:53:53 +00:00
|
|
|
let templateText = await space.readPage(renderTemplate);
|
|
|
|
templateText = `{{#each .}}\n${templateText}\n{{/each}}`;
|
|
|
|
const template = Handlebars.compile(templateText, { noEscape: true });
|
2023-07-02 09:25:32 +00:00
|
|
|
return template(data, buildHandebarOptions(pageMeta));
|
2023-05-23 18:53:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-02 09:25:32 +00:00
|
|
|
export function buildHandebarOptions(pageMeta: PageMeta) {
|
|
|
|
return {
|
|
|
|
helpers: handlebarHelpers(pageMeta.name),
|
|
|
|
data: { page: pageMeta },
|
|
|
|
};
|
|
|
|
}
|
2022-10-28 14:17:40 +00:00
|
|
|
|
2023-07-02 09:25:32 +00:00
|
|
|
export function handlebarHelpers(pageName: string) {
|
|
|
|
return {
|
|
|
|
json: (v: any) => JSON.stringify(v),
|
|
|
|
niceDate: (ts: any) => niceDate(new Date(ts)),
|
|
|
|
escapeRegexp: (ts: any) => {
|
|
|
|
return ts.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
|
|
},
|
|
|
|
prefixLines: (v: string, prefix: string) =>
|
|
|
|
v.split("\n").map((l) => prefix + l).join("\n"),
|
|
|
|
substring: (s: string, from: number, to: number, elipsis = "") =>
|
2022-10-28 14:17:40 +00:00
|
|
|
s.length > to - from ? s.substring(from, to) + elipsis : s,
|
2023-07-02 09:25:32 +00:00
|
|
|
|
|
|
|
today: () => niceDate(new Date()),
|
|
|
|
tomorrow: () => {
|
|
|
|
const tomorrow = new Date();
|
|
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
return niceDate(tomorrow);
|
|
|
|
},
|
|
|
|
yesterday: () => {
|
|
|
|
const yesterday = new Date();
|
|
|
|
yesterday.setDate(yesterday.getDate() - 1);
|
|
|
|
return niceDate(yesterday);
|
|
|
|
},
|
|
|
|
lastWeek: () => {
|
|
|
|
const lastWeek = new Date();
|
|
|
|
lastWeek.setDate(lastWeek.getDate() - 7);
|
|
|
|
return niceDate(lastWeek);
|
|
|
|
},
|
|
|
|
nextWeek: () => {
|
|
|
|
const nextWeek = new Date();
|
|
|
|
nextWeek.setDate(nextWeek.getDate() + 7);
|
|
|
|
return niceDate(nextWeek);
|
|
|
|
},
|
|
|
|
};
|
2022-10-28 14:17:40 +00:00
|
|
|
}
|