2022-10-14 13:11:33 +00:00
|
|
|
import { editor } from "$sb/silverbullet-syscall/mod.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
2022-06-08 13:58:43 +00:00
|
|
|
import Handlebars from "handlebars";
|
2022-04-01 15:07:08 +00:00
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
import { markdown, space } from "$sb/silverbullet-syscall/mod.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { invokeFunction } from "$sb/silverbullet-syscall/system.ts";
|
|
|
|
import { renderQuery } from "./engine.ts";
|
|
|
|
import { parseQuery } from "./parser.ts";
|
|
|
|
import { replaceTemplateVars } from "../core/template.ts";
|
2022-10-14 13:11:33 +00:00
|
|
|
import { jsonToMDTable } from "./util.ts";
|
|
|
|
import { queryRegex } from "$sb/lib/query.ts";
|
|
|
|
import { events } from "$sb/plugos-syscall/mod.ts";
|
|
|
|
import { replaceAsync } from "$sb/lib/util.ts";
|
|
|
|
import { nodeAtPos, renderToText } from "$sb/lib/tree.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { extractMeta } from "./data.ts";
|
2022-03-29 15:02:28 +00:00
|
|
|
|
|
|
|
export async function updateMaterializedQueriesCommand() {
|
2022-10-14 13:11:33 +00:00
|
|
|
const currentPage = await editor.getCurrentPage();
|
|
|
|
await editor.save();
|
2022-05-17 13:54:55 +00:00
|
|
|
if (
|
|
|
|
await invokeFunction(
|
|
|
|
"server",
|
|
|
|
"updateMaterializedQueriesOnPage",
|
2022-10-10 12:50:21 +00:00
|
|
|
currentPage,
|
2022-05-17 13:54:55 +00:00
|
|
|
)
|
|
|
|
) {
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.reloadPage();
|
2022-05-17 13:54:55 +00:00
|
|
|
}
|
2022-04-20 08:56:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 13:58:43 +00:00
|
|
|
export const templateInstRegex =
|
2022-08-10 07:18:11 +00:00
|
|
|
/(<!--\s*#(use|use-verbose|include)\s+\[\[([^\]]+)\]\](.*?)-->)(.+?)(<!--\s*\/\2\s*-->)/gs;
|
2022-06-08 13:58:43 +00:00
|
|
|
|
2022-10-10 16:19:08 +00:00
|
|
|
function updateTemplateInstantiations(
|
2022-06-08 13:58:43 +00:00
|
|
|
text: string,
|
2022-10-10 12:50:21 +00:00
|
|
|
pageName: string,
|
2022-06-08 13:58:43 +00:00
|
|
|
): Promise<string> {
|
|
|
|
return replaceAsync(
|
|
|
|
text,
|
|
|
|
templateInstRegex,
|
2022-10-14 13:11:33 +00:00
|
|
|
async (fullMatch, startInst, type, template, args, _body, endInst) => {
|
2022-06-08 13:58:43 +00:00
|
|
|
args = args.trim();
|
|
|
|
let parsedArgs = {};
|
|
|
|
if (args) {
|
|
|
|
try {
|
|
|
|
parsedArgs = JSON.parse(args);
|
2022-10-14 13:11:33 +00:00
|
|
|
} catch {
|
2022-06-08 13:58:43 +00:00
|
|
|
console.error("Failed to parse template instantiation args", args);
|
|
|
|
return fullMatch;
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 14:51:46 +00:00
|
|
|
let templateText = "";
|
|
|
|
if (template.startsWith("http://") || template.startsWith("https://")) {
|
|
|
|
try {
|
2022-10-14 13:11:33 +00:00
|
|
|
const req = await fetch(template);
|
2022-07-25 14:51:46 +00:00
|
|
|
templateText = await req.text();
|
|
|
|
} catch (e: any) {
|
|
|
|
templateText = `ERROR: ${e.message}`;
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-14 13:11:33 +00:00
|
|
|
templateText = await space.readPage(template);
|
2022-07-25 14:51:46 +00:00
|
|
|
}
|
|
|
|
let newBody = templateText;
|
2022-08-09 13:37:47 +00:00
|
|
|
// if it's a template injection (not a literal "include")
|
2022-08-10 07:18:11 +00:00
|
|
|
if (type === "use" || type === "use-verbose") {
|
2022-10-14 13:11:33 +00:00
|
|
|
const tree = await markdown.parseMarkdown(templateText);
|
2022-08-09 13:37:47 +00:00
|
|
|
extractMeta(tree, ["$disableDirectives"]);
|
|
|
|
templateText = renderToText(tree);
|
2022-10-14 13:11:33 +00:00
|
|
|
const templateFn = Handlebars.compile(
|
2022-07-25 14:51:46 +00:00
|
|
|
replaceTemplateVars(templateText, pageName),
|
2022-10-10 12:50:21 +00:00
|
|
|
{ noEscape: true },
|
2022-07-25 14:51:46 +00:00
|
|
|
);
|
|
|
|
newBody = templateFn(parsedArgs);
|
|
|
|
}
|
2022-06-08 13:58:43 +00:00
|
|
|
return `${startInst}\n${newBody.trim()}\n${endInst}`;
|
2022-10-10 12:50:21 +00:00
|
|
|
},
|
2022-06-08 13:58:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-10 16:19:08 +00:00
|
|
|
function cleanTemplateInstantiations(text: string): Promise<string> {
|
2022-08-09 13:37:47 +00:00
|
|
|
return replaceAsync(
|
|
|
|
text,
|
|
|
|
templateInstRegex,
|
2022-10-10 16:19:08 +00:00
|
|
|
(
|
2022-10-14 13:11:33 +00:00
|
|
|
_fullMatch,
|
2022-10-10 16:19:08 +00:00
|
|
|
startInst,
|
|
|
|
type,
|
2022-10-14 13:11:33 +00:00
|
|
|
_template,
|
|
|
|
_args,
|
2022-10-10 16:19:08 +00:00
|
|
|
body,
|
|
|
|
endInst,
|
|
|
|
): Promise<string> => {
|
2022-08-10 07:18:11 +00:00
|
|
|
if (type === "use") {
|
2022-08-09 13:37:47 +00:00
|
|
|
body = body.replaceAll(
|
|
|
|
queryRegex,
|
|
|
|
(
|
2022-10-14 13:11:33 +00:00
|
|
|
_fullMatch: string,
|
|
|
|
_startQuery: string,
|
|
|
|
_query: string,
|
2022-10-10 12:50:21 +00:00
|
|
|
body: string,
|
2022-08-09 13:37:47 +00:00
|
|
|
) => {
|
|
|
|
return body.trim();
|
2022-10-10 12:50:21 +00:00
|
|
|
},
|
2022-08-09 13:37:47 +00:00
|
|
|
);
|
|
|
|
}
|
2022-10-10 16:19:08 +00:00
|
|
|
return Promise.resolve(`${startInst}${body}${endInst}`);
|
2022-10-10 12:50:21 +00:00
|
|
|
},
|
2022-08-09 13:37:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-29 15:02:28 +00:00
|
|
|
// Called from client, running on server
|
2022-05-17 13:54:55 +00:00
|
|
|
export async function updateMaterializedQueriesOnPage(
|
2022-10-10 12:50:21 +00:00
|
|
|
pageName: string,
|
2022-05-17 13:54:55 +00:00
|
|
|
): Promise<boolean> {
|
2022-10-10 16:19:08 +00:00
|
|
|
// console.log("Updating queries");
|
2022-07-04 13:07:27 +00:00
|
|
|
let text = "";
|
|
|
|
try {
|
2022-10-14 13:11:33 +00:00
|
|
|
text = await space.readPage(pageName);
|
2022-07-04 13:07:27 +00:00
|
|
|
} catch {
|
|
|
|
console.warn(
|
|
|
|
"Could not read page",
|
|
|
|
pageName,
|
2022-10-10 12:50:21 +00:00
|
|
|
"perhaps it doesn't yet exist",
|
2022-07-04 13:07:27 +00:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-08 13:58:43 +00:00
|
|
|
let newText = await updateTemplateInstantiations(text, pageName);
|
2022-10-14 13:11:33 +00:00
|
|
|
const tree = await markdown.parseMarkdown(newText);
|
|
|
|
const metaData = extractMeta(tree, ["$disableDirectives"]);
|
2022-08-09 13:37:47 +00:00
|
|
|
if (metaData.$disableDirectives) {
|
|
|
|
console.log("Directives disabled, skipping");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
newText = renderToText(tree);
|
2022-07-25 14:51:46 +00:00
|
|
|
|
2022-06-08 13:58:43 +00:00
|
|
|
newText = await replaceAsync(
|
|
|
|
newText,
|
2022-04-12 11:33:07 +00:00
|
|
|
queryRegex,
|
2022-10-14 13:11:33 +00:00
|
|
|
async (fullMatch, startQuery, query, _body, endQuery, index) => {
|
|
|
|
const currentNode = nodeAtPos(tree, index + 1);
|
2022-07-25 14:51:46 +00:00
|
|
|
if (currentNode?.type !== "CommentBlock") {
|
|
|
|
// If not a comment block, it's likely a code block, ignore
|
|
|
|
return fullMatch;
|
|
|
|
}
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const parsedQuery = parseQuery(replaceTemplateVars(query, pageName));
|
2022-04-06 13:39:20 +00:00
|
|
|
|
2022-08-10 07:18:11 +00:00
|
|
|
// console.log("Parsed query", parsedQuery);
|
2022-04-19 14:54:47 +00:00
|
|
|
// Let's dispatch an event and see what happens
|
2022-10-14 13:11:33 +00:00
|
|
|
const results = await events.dispatchEvent(
|
2022-04-19 14:54:47 +00:00
|
|
|
`query:${parsedQuery.table}`,
|
|
|
|
{ query: parsedQuery, pageName: pageName },
|
2022-10-10 12:50:21 +00:00
|
|
|
10 * 1000,
|
2022-04-19 14:54:47 +00:00
|
|
|
);
|
|
|
|
if (results.length === 0) {
|
|
|
|
return `${startQuery}\n${endQuery}`;
|
|
|
|
} else if (results.length === 1) {
|
2022-04-28 09:55:38 +00:00
|
|
|
if (parsedQuery.render) {
|
2022-10-14 13:11:33 +00:00
|
|
|
const rendered = await renderQuery(parsedQuery, results[0]);
|
2022-04-28 09:55:38 +00:00
|
|
|
return `${startQuery}\n${rendered.trim()}\n${endQuery}`;
|
|
|
|
} else {
|
|
|
|
return `${startQuery}\n${jsonToMDTable(results[0])}\n${endQuery}`;
|
|
|
|
}
|
2022-04-19 14:54:47 +00:00
|
|
|
} else {
|
|
|
|
console.error("Too many query results", results);
|
|
|
|
return fullMatch;
|
2022-04-12 11:33:07 +00:00
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
},
|
2022-04-12 11:33:07 +00:00
|
|
|
);
|
2022-08-09 13:37:47 +00:00
|
|
|
newText = await cleanTemplateInstantiations(newText);
|
2022-05-17 13:54:55 +00:00
|
|
|
if (text !== newText) {
|
2022-10-14 13:11:33 +00:00
|
|
|
await space.writePage(pageName, newText);
|
2022-05-17 13:54:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2022-03-29 15:02:28 +00:00
|
|
|
}
|