pull/1219/merge
Zef Hemel 2025-02-09 17:57:59 +01:00
parent 654151437c
commit 5eb2405823
3 changed files with 14 additions and 29 deletions

View File

@ -119,7 +119,7 @@ export async function expandCodeWidgets(
result = result.markdown; result = result.markdown;
} }
const markdown = renderExpressionResult(result); const markdown = await renderExpressionResult(result);
return await parseMarkdown(markdown); return await parseMarkdown(markdown);
} }
}); });

View File

@ -1,9 +1,9 @@
import type { PageMeta } from "../../plug-api/types.ts"; import type { PageMeta } from "../../plug-api/types.ts";
import { space, system, template } from "@silverbulletmd/silverbullet/syscalls"; import { space, system, template } from "@silverbulletmd/silverbullet/syscalls";
import { cleanTemplate } from "./plug_api.ts"; import { cleanTemplate } from "./plug_api.ts";
import { LuaTable } from "$common/space_lua/runtime.ts"; import { LuaTable, luaToString } from "$common/space_lua/runtime.ts";
export function defaultJsonTransformer(v: any): string { export async function defaultJsonTransformer(v: any): Promise<string> {
if (v === undefined) { if (v === undefined) {
return ""; return "";
} }
@ -11,33 +11,18 @@ export function defaultJsonTransformer(v: any): string {
return v.replaceAll("\n", " ").replaceAll("|", "\\|"); return v.replaceAll("\n", " ").replaceAll("|", "\\|");
} }
if (Array.isArray(v)) { if (Array.isArray(v)) {
return v.map(defaultJsonTransformer).join(", "); return (await Promise.all(v.map(defaultJsonTransformer))).join(", ");
} else if (v && typeof v === "object") { } else if (v && typeof v === "object") {
return Object.entries(v).map(([k, v]: [string, any]) => return luaToString(v);
`${k}: ${defaultJsonTransformer(v)}`
).join(", ");
} }
return "" + v; return "" + v;
} }
export function jsonObjectToMDTable(
obj: Record<string, any>,
valueTransformer: (v: any) => string = defaultJsonTransformer,
): string {
const lines = [];
lines.push("| Key | Value |");
lines.push("| --- | --- |");
for (const [k, v] of Object.entries(obj)) {
lines.push(`| ${k} | ${valueTransformer(v)} |`);
}
return lines.join("\n");
}
// Nicely format an array of JSON objects as a Markdown table // Nicely format an array of JSON objects as a Markdown table
export function jsonToMDTable( export async function jsonToMDTable(
jsonArray: any[], jsonArray: any[],
valueTransformer: (v: any) => string = defaultJsonTransformer, valueTransformer: (v: any) => Promise<string> = defaultJsonTransformer,
): string { ): Promise<string> {
const headers = new Set<string>(); 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)) {
@ -66,7 +51,7 @@ export function jsonToMDTable(
for (const val of jsonArray) { for (const val of jsonArray) {
const el = []; const el = [];
for (const prop of headerList) { for (const prop of headerList) {
const s = valueTransformer(val[prop]); const s = await valueTransformer(val[prop]);
el.push(s); el.push(s);
} }
lines.push("|" + el.join("|") + "|"); lines.push("|" + el.join("|") + "|");
@ -93,7 +78,7 @@ export async function renderQueryTemplate(
}); });
} }
export function renderExpressionResult(result: any): string { export function renderExpressionResult(result: any): Promise<string> {
if (result instanceof LuaTable) { if (result instanceof LuaTable) {
result = result.toJS(); result = result.toJS();
} }
@ -109,15 +94,15 @@ export function renderExpressionResult(result: any): string {
JSON.stringify(result) JSON.stringify(result)
}`, }`,
); );
return JSON.stringify(result); return Promise.resolve(JSON.stringify(result));
} }
} else if (typeof result === "object" && result.constructor === Object) { } else if (typeof result === "object" && result.constructor === Object) {
// if result is a plain object, render as a markdown table // if result is a plain object, render as a markdown table
return jsonToMDTable([result]); return jsonToMDTable([result]);
} else if (Array.isArray(result)) { } else if (Array.isArray(result)) {
// Not-object array, let's render it as a markdown list // Not-object array, let's render it as a markdown list
return result.map((item) => `- ${item}`).join("\n"); return Promise.resolve(result.map((item) => `- ${item}`).join("\n"));
} else { } else {
return "" + result; return Promise.resolve("" + result);
} }
} }

View File

@ -99,7 +99,7 @@ export class LuaWidget extends WidgetType {
// If there is a markdown key, use it, otherwise render the objects as a markdown table // If there is a markdown key, use it, otherwise render the objects as a markdown table
let mdContent = widgetContent.markdown; let mdContent = widgetContent.markdown;
if (!mdContent) { if (!mdContent) {
mdContent = renderExpressionResult(widgetContent); mdContent = await renderExpressionResult(widgetContent);
} }
let mdTree = parse( let mdTree = parse(
extendedMarkdownLanguage, extendedMarkdownLanguage,