1
0
silverbullet/plugs/directive/util.ts

112 lines
3.0 KiB
TypeScript
Raw Normal View History

import Handlebars from "handlebars";
import * as YAML from "yaml";
import { space } from "$sb/silverbullet-syscall/mod.ts";
import { niceDate } from "$sb/lib/dates.ts";
2022-04-29 11:37:31 +00:00
const maxWidth = 70;
// Nicely format an array of JSON objects as a Markdown table
export function jsonToMDTable(
jsonArray: any[],
2022-10-15 17:02:56 +00:00
valueTransformer: (k: string, v: any) => string = (_k, v) => "" + v,
): 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-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
);
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));
}
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);
}
}
export async function renderTemplate(
renderTemplate: string,
data: any[],
): Promise<string> {
Handlebars.registerHelper("json", (v: any) => JSON.stringify(v));
Handlebars.registerHelper("niceDate", (ts: any) => niceDate(new Date(ts)));
Handlebars.registerHelper("prefixLines", (v: string, prefix: string) =>
v
.split("\n")
.map((l) => prefix + l)
.join("\n"));
Handlebars.registerHelper(
"substring",
(s: string, from: number, to: number, elipsis = "") =>
s.length > to - from ? s.substring(from, to) + elipsis : s,
);
Handlebars.registerHelper("yaml", (v: any, prefix: string) => {
if (typeof prefix === "string") {
let yaml = YAML.stringify(v)
.split("\n")
.join("\n" + prefix)
.trim();
if (Array.isArray(v)) {
return "\n" + prefix + yaml;
} else {
return yaml;
}
} else {
return YAML.stringify(v).trim();
}
});
let templateText = await space.readPage(renderTemplate);
templateText = `{{#each .}}\n${templateText}\n{{/each}}`;
const template = Handlebars.compile(templateText, { noEscape: true });
return template(data);
}