Template instatiation, tweak of query syntax

pull/3/head
Zef Hemel 2022-06-08 15:58:43 +02:00
parent 7429a96e61
commit 32937f88e3
6 changed files with 41 additions and 11 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -42,7 +42,7 @@ async function bundle(
jsFunctionName, jsFunctionName,
debug, debug,
allModulesToExclude, allModulesToExclude,
true false
); );
delete def.path; delete def.path;
} }

View File

@ -6,6 +6,6 @@ import {
export async function insertQuery() { export async function insertQuery() {
let cursorPos = await getCursor(); let cursorPos = await getCursor();
await insertAtCursor(`<!-- #query -->\n\n<!-- #end -->`); await insertAtCursor(`<!-- #query -->\n\n<!-- /query -->`);
await moveCursor(cursorPos + 12); await moveCursor(cursorPos + 12);
} }

View File

@ -1,10 +1,9 @@
import { import {
flashNotification,
getCurrentPage, getCurrentPage,
getText,
reloadPage, reloadPage,
save, save,
} from "@silverbulletmd/plugos-silverbullet-syscall/editor"; } from "@silverbulletmd/plugos-silverbullet-syscall/editor";
import Handlebars from "handlebars";
import { import {
readPage, readPage,
@ -13,7 +12,7 @@ import {
import { invokeFunction } from "@silverbulletmd/plugos-silverbullet-syscall/system"; import { invokeFunction } from "@silverbulletmd/plugos-silverbullet-syscall/system";
import { parseQuery, renderQuery } from "./engine"; import { parseQuery, renderQuery } from "./engine";
import { replaceTemplateVars } from "../core/template"; import { replaceTemplateVars } from "../core/template";
import { jsonToMDTable, queryRegex, removeQueries } from "./util"; import { jsonToMDTable, queryRegex } from "./util";
import { dispatch } from "@plugos/plugos-syscall/event"; import { dispatch } from "@plugos/plugos-syscall/event";
import { replaceAsync } from "../lib/util"; import { replaceAsync } from "../lib/util";
@ -32,14 +31,47 @@ export async function updateMaterializedQueriesCommand() {
} }
} }
export const templateInstRegex =
/(<!--\s*#inst\s+"([^"]+)"(.+?)-->)(.+?)(<!--\s*\/inst\s*-->)/gs;
async function updateTemplateInstantiations(
text: string,
pageName: string
): Promise<string> {
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 // Called from client, running on server
export async function updateMaterializedQueriesOnPage( export async function updateMaterializedQueriesOnPage(
pageName: string pageName: string
): Promise<boolean> { ): Promise<boolean> {
let { text } = await readPage(pageName); let { text } = await readPage(pageName);
let newText = await replaceAsync( let newText = await updateTemplateInstantiations(text, pageName);
text, newText = await replaceAsync(
newText,
queryRegex, queryRegex,
async (fullMatch, startQuery, query, body, endQuery) => { async (fullMatch, startQuery, query, body, endQuery) => {
let parsedQuery = parseQuery(replaceTemplateVars(query, pageName)); let parsedQuery = parseQuery(replaceTemplateVars(query, pageName));

View File

@ -6,11 +6,11 @@ import {
} from "@silverbulletmd/common/tree"; } from "@silverbulletmd/common/tree";
export const queryRegex = export const queryRegex =
/(<!--\s*#query\s+(.+?)-->)(.+?)(<!--\s*#end\s*-->)/gs; /(<!--\s*#query\s+(.+?)-->)(.+?)(<!--\s*\/query\s*-->)/gs;
export const queryStartRegex = /<!--\s*#query\s+(.+?)-->/s; export const queryStartRegex = /<!--\s*#query\s+(.+?)-->/s;
export const queryEndRegex = /<!--\s*#end\s*-->/s; export const queryEndRegex = /<!--\s*\/query\s*-->/s;
// export function whiteOutQueries(text: string): string { // export function whiteOutQueries(text: string): string {
// return text.replaceAll(queryRegex, (match) => // return text.replaceAll(queryRegex, (match) =>

View File

@ -1,8 +1,6 @@
import { useEffect, useRef } from "react"; import { useEffect, useRef } from "react";
// @ts-ignore // @ts-ignore
import iframeHtml from "bundle-text:./panel.html"; import iframeHtml from "bundle-text:./panel.html";
import { System } from "@plugos/plugos/system";
import { SilverBulletHooks } from "@silverbulletmd/common/manifest";
import { Editor } from "../editor"; import { Editor } from "../editor";
export function Panel({ export function Panel({