Awesome frontmatter (#617)

Live Frontmatter Templates
This commit is contained in:
Zef Hemel
2024-01-04 20:08:12 +01:00
committed by GitHub
parent 9040993232
commit 91027af5fe
53 changed files with 646 additions and 342 deletions
+7 -1
View File
@@ -1,11 +1,17 @@
import { ObjectValue } from "$sb/types.ts";
export type TemplateFrontmatter = {
trigger?: string; // slash command name
displayName?: string;
type?: "page";
// Frontmatter can be encoded as an object (in which case we'll serialize it) or as a string
frontmatter?: Record<string, any> | string;
// Specific for slash templates
trigger?: string;
// Specific for frontmatter templates
where?: string; // expression (SB query style)
priority?: number; // When multiple templates match, the one with the highest priority is used
};
export type TemplateObject = ObjectValue<TemplateFrontmatter>;
+10 -3
View File
@@ -36,20 +36,27 @@ export function buildHandebarOptions(pageMeta: PageMeta) {
};
}
export function defaultJsonTransformer(_k: string, v: any) {
export function defaultJsonTransformer(v: any): string {
if (v === undefined) {
return "";
}
if (typeof v === "string") {
return v.replaceAll("\n", " ").replaceAll("|", "\\|");
}
if (Array.isArray(v)) {
return v.map(defaultJsonTransformer).join(", ");
} else if (typeof v === "object") {
return Object.entries(v).map(([k, v]: [string, any]) =>
`${k}: ${defaultJsonTransformer(v)}`
).join(", ");
}
return "" + v;
}
// Nicely format an array of JSON objects as a Markdown table
export function jsonToMDTable(
jsonArray: any[],
valueTransformer: (k: string, v: any) => string = defaultJsonTransformer,
valueTransformer: (v: any) => string = defaultJsonTransformer,
): string {
const headers = new Set<string>();
for (const entry of jsonArray) {
@@ -79,7 +86,7 @@ export function jsonToMDTable(
for (const val of jsonArray) {
const el = [];
for (const prop of headerList) {
const s = valueTransformer(prop, val[prop]);
const s = valueTransformer(val[prop]);
el.push(s);
}
lines.push("|" + el.join("|") + "|");