From 63b614670cce5bdd07f528112be14ca21dfe7cec Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sun, 29 Oct 2023 12:09:12 +0100 Subject: [PATCH] Remove fancy markdown table formatting --- plugs/directive/util.ts | 39 ++++++--------------------------------- 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/plugs/directive/util.ts b/plugs/directive/util.ts index 1a772f12..b8540252 100644 --- a/plugs/directive/util.ts +++ b/plugs/directive/util.ts @@ -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(); + const headers = new Set(); 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(