import { getCurrentPage, reloadPage, save, } from "$sb/silverbullet-syscall/editor.ts"; import Handlebars from "handlebars"; import { readPage, writePage } from "$sb/silverbullet-syscall/space.ts"; import { invokeFunction } from "$sb/silverbullet-syscall/system.ts"; import { renderQuery } from "./engine.ts"; import { parseQuery } from "./parser.ts"; import { replaceTemplateVars } from "../core/template.ts"; import { jsonToMDTable, queryRegex } from "./util.ts"; import { dispatch } from "$sb/plugos-syscall/event.ts"; import { replaceAsync } from "../lib/util.ts"; import { parseMarkdown } from "$sb/silverbullet-syscall/markdown.ts"; import { nodeAtPos, renderToText } from "../../common/tree.ts"; import { extractMeta } from "./data.ts"; export async function updateMaterializedQueriesCommand() { const currentPage = await getCurrentPage(); await save(); if ( await invokeFunction( "server", "updateMaterializedQueriesOnPage", currentPage, ) ) { console.log("Going reload the page"); await reloadPage(); } } export const templateInstRegex = /()(.+?)()/gs; function updateTemplateInstantiations( text: string, pageName: string, ): Promise { return replaceAsync( text, templateInstRegex, async (fullMatch, startInst, type, template, args, body, endInst) => { args = args.trim(); let parsedArgs = {}; if (args) { try { parsedArgs = JSON.parse(args); } catch (e) { console.error("Failed to parse template instantiation args", args); return fullMatch; } } let templateText = ""; if (template.startsWith("http://") || template.startsWith("https://")) { try { let req = await fetch(template); templateText = await req.text(); } catch (e: any) { templateText = `ERROR: ${e.message}`; } } else { templateText = (await readPage(template)).text; } let newBody = templateText; // if it's a template injection (not a literal "include") if (type === "use" || type === "use-verbose") { let tree = await parseMarkdown(templateText); extractMeta(tree, ["$disableDirectives"]); templateText = renderToText(tree); let templateFn = Handlebars.compile( replaceTemplateVars(templateText, pageName), { noEscape: true }, ); newBody = templateFn(parsedArgs); } return `${startInst}\n${newBody.trim()}\n${endInst}`; }, ); } function cleanTemplateInstantiations(text: string): Promise { return replaceAsync( text, templateInstRegex, ( fullMatch, startInst, type, template, args, body, endInst, ): Promise => { if (type === "use") { body = body.replaceAll( queryRegex, ( fullMatch: string, startQuery: string, query: string, body: string, ) => { return body.trim(); }, ); } return Promise.resolve(`${startInst}${body}${endInst}`); }, ); } // Called from client, running on server export async function updateMaterializedQueriesOnPage( pageName: string, ): Promise { // console.log("Updating queries"); let text = ""; try { text = (await readPage(pageName)).text; } catch { console.warn( "Could not read page", pageName, "perhaps it doesn't yet exist", ); return false; } let newText = await updateTemplateInstantiations(text, pageName); let tree = await parseMarkdown(newText); let metaData = extractMeta(tree, ["$disableDirectives"]); if (metaData.$disableDirectives) { console.log("Directives disabled, skipping"); return false; } newText = renderToText(tree); newText = await replaceAsync( newText, queryRegex, async (fullMatch, startQuery, query, body, endQuery, index) => { let currentNode = nodeAtPos(tree, index + 1); if (currentNode?.type !== "CommentBlock") { // If not a comment block, it's likely a code block, ignore return fullMatch; } let parsedQuery = parseQuery(replaceTemplateVars(query, pageName)); // console.log("Parsed query", parsedQuery); // Let's dispatch an event and see what happens let results = await dispatch( `query:${parsedQuery.table}`, { query: parsedQuery, pageName: pageName }, 10 * 1000, ); if (results.length === 0) { return `${startQuery}\n${endQuery}`; } else if (results.length === 1) { if (parsedQuery.render) { let rendered = await renderQuery(parsedQuery, results[0]); return `${startQuery}\n${rendered.trim()}\n${endQuery}`; } else { return `${startQuery}\n${jsonToMDTable(results[0])}\n${endQuery}`; } } else { console.error("Too many query results", results); return fullMatch; } }, ); newText = await cleanTemplateInstantiations(newText); if (text !== newText) { await writePage(pageName, newText); return true; } return false; }