From 32937f88e318def44fde5621c88876110ae348c6 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 8 Jun 2022 15:58:43 +0200 Subject: [PATCH] Template instatiation, tweak of query syntax --- .DS_Store | Bin 10244 -> 10244 bytes packages/plugos/bin/plugos-bundle.ts | 2 +- packages/plugs/query/command.ts | 2 +- packages/plugs/query/materialized_queries.ts | 42 ++++++++++++++++--- packages/plugs/query/util.ts | 4 +- packages/web/components/panel.tsx | 2 - 6 files changed, 41 insertions(+), 11 deletions(-) diff --git a/.DS_Store b/.DS_Store index 8432836ba509e1a9512d105b8ee5c186cf0785a7..c68faf69c7e235410f1ba74cf6369b864af33008 100644 GIT binary patch delta 1073 zcmeH_-%Aux9L49XR(IfncQSXL z(2`4@7wI&UW+zX2eTg=qbHfrng2z=iqH5TP4Dfjmb7%N+G_7T@OPm=L)+oy-WIltr zJ=CvlQrQgY&?6SFkLmWNo|aZ@X*uP;TAro1d$z^XS@lJWKEGVCp@5(P_QCU`gBMlT;@WO{2$e8m{9OZsQK_;XWSX37+B^p5ra%@BtsOfY11X zulRuh{vUbeeoa~=(_H4?ic3yY*&E0H^LTQCq{E?KoBx0eJcOOr*asokAHDX}8C6pGlp*;nrS7JGZK3Eu% zXhtQR^SFSExP;4?zzy6?0lJ$4^Z<`Ag=sv-3|?UtukmL3rLUUee?_ZTRIpk_gydIF ImMMzz7dG1rlK=n! delta 1120 zcmeH_-Afcv7{=!r`in=H!J|5-q7&1}3OvrkQA|`H3<*NB89JZ0*dd39&^| z)MZ;;1w|1EeUjKs^a)X2bYn}GL08^ZRFFb-W_P>kBKiZmnZx@&Gtc{;-!s!~({1+_ z17OLez{5oDYC?L2#cfxN2p;R45SAP2F`Z#mH6q zBJwVdAHi(9rk1e*Z}fiCaa)gYTdws~&QD*XaF8`vckAjJHnM zJVxiU+qTI5AoZmc&uot++-NK-9!QH+*yF-AUAb4gJmOArG4EjFjq*TK#ys94h4)y%XMDj|e8W$o`qhA%s~T#lT9*8{W&p-)>smh&zr+1A z^iGMNPih`Rx-!M`*A*B=WP>L0)8(16glyI%c)B7}292$n%$lm{l9U@2x(t-&WXhni zQ`aamMHQ*mWwdlziW$&{yv8p#3G?@=gF6E8(m7x-~Xuw_xr5Pq&ZX`IFxoRxr*5|Ib!Dz4!=Zr~2?;yz|EhsSt=XLvsQh}Dw- Y{U4YY!aoMK@o#}?T5;{=?TVuO0-_lozW@LL diff --git a/packages/plugos/bin/plugos-bundle.ts b/packages/plugos/bin/plugos-bundle.ts index f97899cb..0d6572dc 100755 --- a/packages/plugos/bin/plugos-bundle.ts +++ b/packages/plugos/bin/plugos-bundle.ts @@ -42,7 +42,7 @@ async function bundle( jsFunctionName, debug, allModulesToExclude, - true + false ); delete def.path; } diff --git a/packages/plugs/query/command.ts b/packages/plugs/query/command.ts index ed6ce200..51b24e1e 100644 --- a/packages/plugs/query/command.ts +++ b/packages/plugs/query/command.ts @@ -6,6 +6,6 @@ import { export async function insertQuery() { let cursorPos = await getCursor(); - await insertAtCursor(`\n\n`); + await insertAtCursor(`\n\n`); await moveCursor(cursorPos + 12); } diff --git a/packages/plugs/query/materialized_queries.ts b/packages/plugs/query/materialized_queries.ts index 9be8a32c..fce944db 100644 --- a/packages/plugs/query/materialized_queries.ts +++ b/packages/plugs/query/materialized_queries.ts @@ -1,10 +1,9 @@ import { - flashNotification, getCurrentPage, - getText, reloadPage, save, } from "@silverbulletmd/plugos-silverbullet-syscall/editor"; +import Handlebars from "handlebars"; import { readPage, @@ -13,7 +12,7 @@ import { import { invokeFunction } from "@silverbulletmd/plugos-silverbullet-syscall/system"; import { parseQuery, renderQuery } from "./engine"; import { replaceTemplateVars } from "../core/template"; -import { jsonToMDTable, queryRegex, removeQueries } from "./util"; +import { jsonToMDTable, queryRegex } from "./util"; import { dispatch } from "@plugos/plugos-syscall/event"; import { replaceAsync } from "../lib/util"; @@ -32,14 +31,47 @@ export async function updateMaterializedQueriesCommand() { } } +export const templateInstRegex = + /()(.+?)()/gs; + +async function updateTemplateInstantiations( + text: string, + pageName: string +): Promise { + return replaceAsync( + text, + templateInstRegex, + async (fullMatch, startInst, 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 { text: templateText } = await readPage(template); + let templateFn = Handlebars.compile( + replaceTemplateVars(templateText, pageName), + { noEscape: true } + ); + let newBody = templateFn(parsedArgs); + return `${startInst}\n${newBody.trim()}\n${endInst}`; + } + ); +} + // Called from client, running on server export async function updateMaterializedQueriesOnPage( pageName: string ): Promise { let { text } = await readPage(pageName); - let newText = await replaceAsync( - text, + let newText = await updateTemplateInstantiations(text, pageName); + newText = await replaceAsync( + newText, queryRegex, async (fullMatch, startQuery, query, body, endQuery) => { let parsedQuery = parseQuery(replaceTemplateVars(query, pageName)); diff --git a/packages/plugs/query/util.ts b/packages/plugs/query/util.ts index 35a35aee..f4b480ed 100644 --- a/packages/plugs/query/util.ts +++ b/packages/plugs/query/util.ts @@ -6,11 +6,11 @@ import { } from "@silverbulletmd/common/tree"; export const queryRegex = - /()(.+?)()/gs; + /()(.+?)()/gs; export const queryStartRegex = //s; -export const queryEndRegex = //s; +export const queryEndRegex = //s; // export function whiteOutQueries(text: string): string { // return text.replaceAll(queryRegex, (match) => diff --git a/packages/web/components/panel.tsx b/packages/web/components/panel.tsx index acd29811..b30aed16 100644 --- a/packages/web/components/panel.tsx +++ b/packages/web/components/panel.tsx @@ -1,8 +1,6 @@ import { useEffect, useRef } from "react"; // @ts-ignore import iframeHtml from "bundle-text:./panel.html"; -import { System } from "@plugos/plugos/system"; -import { SilverBulletHooks } from "@silverbulletmd/common/manifest"; import { Editor } from "../editor"; export function Panel({