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