2022-10-28 14:17:40 +00:00
|
|
|
import { queryRegex } from "$sb/lib/query.ts";
|
2022-12-14 19:04:20 +00:00
|
|
|
import { ParseTree, renderToText } from "$sb/lib/tree.ts";
|
2022-10-28 14:17:40 +00:00
|
|
|
import { replaceAsync } from "$sb/lib/util.ts";
|
|
|
|
import { markdown, space } from "$sb/silverbullet-syscall/mod.ts";
|
|
|
|
import Handlebars from "handlebars";
|
|
|
|
|
|
|
|
import { replaceTemplateVars } from "../core/template.ts";
|
2022-11-24 11:04:00 +00:00
|
|
|
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
|
2022-12-14 19:04:20 +00:00
|
|
|
import { directiveRegex } from "./directives.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import { updateDirectives } from "./command.ts";
|
|
|
|
import { registerHandlebarsHelpers } from "./util.ts";
|
2022-10-28 14:17:40 +00:00
|
|
|
|
|
|
|
const templateRegex = /\[\[([^\]]+)\]\]\s*(.*)\s*/;
|
|
|
|
|
|
|
|
export async function templateDirectiveRenderer(
|
|
|
|
directive: string,
|
|
|
|
pageName: string,
|
2022-12-14 19:04:20 +00:00
|
|
|
arg: string | ParseTree,
|
2022-10-28 14:17:40 +00:00
|
|
|
): Promise<string> {
|
2022-12-14 19:04:20 +00:00
|
|
|
if (typeof arg !== "string") {
|
|
|
|
throw new Error("Template directives must be a string");
|
|
|
|
}
|
2022-10-28 14:17:40 +00:00
|
|
|
const match = arg.match(templateRegex);
|
|
|
|
if (!match) {
|
|
|
|
throw new Error(`Invalid template directive: ${arg}`);
|
|
|
|
}
|
|
|
|
const template = match[1];
|
|
|
|
const args = match[2];
|
|
|
|
let parsedArgs = {};
|
|
|
|
if (args) {
|
|
|
|
try {
|
|
|
|
parsedArgs = JSON.parse(args);
|
|
|
|
} catch {
|
|
|
|
throw new Error(`Failed to parse template instantiation args: ${arg}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let templateText = "";
|
|
|
|
if (template.startsWith("http://") || template.startsWith("https://")) {
|
|
|
|
try {
|
|
|
|
const req = await fetch(template);
|
|
|
|
templateText = await req.text();
|
|
|
|
} catch (e: any) {
|
|
|
|
templateText = `ERROR: ${e.message}`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
templateText = await space.readPage(template);
|
|
|
|
}
|
|
|
|
let newBody = templateText;
|
|
|
|
// if it's a template injection (not a literal "include")
|
2022-12-14 19:04:20 +00:00
|
|
|
if (directive === "use") {
|
2022-10-28 14:17:40 +00:00
|
|
|
const tree = await markdown.parseMarkdown(templateText);
|
2023-05-23 18:53:53 +00:00
|
|
|
await extractFrontmatter(tree, ["$disableDirectives"]);
|
2022-10-28 14:17:40 +00:00
|
|
|
templateText = renderToText(tree);
|
2023-05-23 18:53:53 +00:00
|
|
|
registerHandlebarsHelpers();
|
2022-10-28 14:17:40 +00:00
|
|
|
const templateFn = Handlebars.compile(
|
|
|
|
replaceTemplateVars(templateText, pageName),
|
|
|
|
{ noEscape: true },
|
|
|
|
);
|
2023-05-23 18:53:53 +00:00
|
|
|
if (typeof parsedArgs !== "string") {
|
|
|
|
(parsedArgs as any).page = pageName;
|
|
|
|
}
|
2022-10-28 14:17:40 +00:00
|
|
|
newBody = templateFn(parsedArgs);
|
|
|
|
|
|
|
|
// Recursively render directives
|
2023-05-23 18:53:53 +00:00
|
|
|
newBody = await updateDirectives(pageName, newBody);
|
2022-10-28 14:17:40 +00:00
|
|
|
}
|
|
|
|
return newBody.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cleanTemplateInstantiations(text: string): Promise<string> {
|
|
|
|
return replaceAsync(
|
|
|
|
text,
|
|
|
|
directiveRegex,
|
|
|
|
(
|
|
|
|
_fullMatch,
|
|
|
|
startInst,
|
|
|
|
type,
|
|
|
|
_args,
|
|
|
|
body,
|
|
|
|
endInst,
|
|
|
|
): Promise<string> => {
|
|
|
|
if (type === "use") {
|
|
|
|
body = body.replaceAll(
|
|
|
|
queryRegex,
|
|
|
|
(
|
|
|
|
_fullMatch: string,
|
|
|
|
_startQuery: string,
|
|
|
|
_query: string,
|
|
|
|
body: string,
|
|
|
|
) => {
|
|
|
|
return body.trim();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Promise.resolve(`${startInst}${body}${endInst}`);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|