Cleanup
parent
5eb2405823
commit
149a2ebea1
|
@ -3,25 +3,23 @@ import { space, system, template } from "@silverbulletmd/silverbullet/syscalls";
|
||||||
import { cleanTemplate } from "./plug_api.ts";
|
import { cleanTemplate } from "./plug_api.ts";
|
||||||
import { LuaTable, luaToString } from "$common/space_lua/runtime.ts";
|
import { LuaTable, luaToString } from "$common/space_lua/runtime.ts";
|
||||||
|
|
||||||
export async function defaultJsonTransformer(v: any): Promise<string> {
|
export function defaultTransformer(v: any): Promise<string> {
|
||||||
if (v === undefined) {
|
if (v === undefined) {
|
||||||
return "";
|
return Promise.resolve("");
|
||||||
}
|
}
|
||||||
if (typeof v === "string") {
|
if (typeof v === "string") {
|
||||||
return v.replaceAll("\n", " ").replaceAll("|", "\\|");
|
return Promise.resolve(v.replaceAll("\n", " ").replaceAll("|", "\\|"));
|
||||||
}
|
}
|
||||||
if (Array.isArray(v)) {
|
if (v && typeof v === "object") {
|
||||||
return (await Promise.all(v.map(defaultJsonTransformer))).join(", ");
|
return Promise.resolve(luaToString(v));
|
||||||
} else if (v && typeof v === "object") {
|
|
||||||
return luaToString(v);
|
|
||||||
}
|
}
|
||||||
return "" + v;
|
return Promise.resolve("" + v);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nicely format an array of JSON objects as a Markdown table
|
// Nicely format an array of JSON objects as a Markdown table
|
||||||
export async function jsonToMDTable(
|
export async function jsonToMDTable(
|
||||||
jsonArray: any[],
|
jsonArray: any[],
|
||||||
valueTransformer: (v: any) => Promise<string> = defaultJsonTransformer,
|
valueTransformer: (v: any) => Promise<string> = defaultTransformer,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const headers = new Set<string>();
|
const headers = new Set<string>();
|
||||||
for (const entry of jsonArray) {
|
for (const entry of jsonArray) {
|
||||||
|
|
Loading…
Reference in New Issue