Remove fancy markdown table formatting
parent
0b9977a4c4
commit
63b614670c
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue