Remove fancy markdown table formatting

pull/555/head
Zef Hemel 2023-10-29 12:09:12 +01:00
parent 0b9977a4c4
commit 63b614670c
1 changed files with 6 additions and 33 deletions

View File

@ -2,8 +2,6 @@ import { handlebars, space } from "$sb/syscalls.ts";
import { handlebarHelpers } from "../../common/syscalls/handlebar_helpers.ts";
import { PageMeta } from "$sb/types.ts";
const maxWidth = 70;
export function defaultJsonTransformer(_k: string, v: any) {
if (v === undefined) {
return "";
@ -19,33 +17,20 @@ export function jsonToMDTable(
jsonArray: any[],
valueTransformer: (k: string, v: any) => string = defaultJsonTransformer,
): string {
const fieldWidths = new Map<string, number>();
const headers = new Set<string>();
for (const entry of jsonArray) {
for (const k of Object.keys(entry)) {
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);
headers.add(k);
}
}
let fullWidth = 0;
for (const v of fieldWidths.values()) {
fullWidth += v + 1;
}
const headerList = [...fieldWidths.keys()];
const headerList = [...headers];
const lines = [];
lines.push(
"|" +
headerList
.map(
(headerName) =>
headerName +
charPad(" ", fieldWidths.get(headerName)! - headerName.length),
(headerName) => headerName,
)
.join("|") +
"|",
@ -53,7 +38,7 @@ export function jsonToMDTable(
lines.push(
"|" +
headerList
.map((title) => charPad("-", fieldWidths.get(title)!))
.map(() => "--")
.join("|") +
"|",
);
@ -61,23 +46,11 @@ export function jsonToMDTable(
const el = [];
for (const prop of headerList) {
const s = valueTransformer(prop, val[prop]);
el.push(s + charPad(" ", fieldWidths.get(prop)! - s.length));
el.push(s);
}
lines.push("|" + el.join("|") + "|");
}
return lines.join("\n");
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(