Replaced #inst with #template and #include, made query parsing more robust
This commit is contained in:
@@ -184,6 +184,22 @@ functions:
|
||||
<!-- #query |^| -->
|
||||
|
||||
<!-- /query -->
|
||||
insertInclude:
|
||||
path: "./template.ts:insertTemplateText"
|
||||
slashCommand:
|
||||
name: include
|
||||
value: |
|
||||
<!-- #include "|^|" -->
|
||||
|
||||
<!-- /include -->
|
||||
insertInlineTemplate:
|
||||
path: "./template.ts:insertTemplateText"
|
||||
slashCommand:
|
||||
name: inline-template
|
||||
value: |
|
||||
<!-- #template "|^|" {} -->
|
||||
|
||||
<!-- /template -->
|
||||
quickNoteCommand:
|
||||
path: ./template.ts:quickNoteCommand
|
||||
command:
|
||||
|
||||
@@ -30,11 +30,22 @@ export async function queryComplete() {
|
||||
};
|
||||
}
|
||||
|
||||
prefix = await matchBefore('#inst "[^"]*');
|
||||
prefix = await matchBefore('#template "[^"]*');
|
||||
if (prefix) {
|
||||
let allPages = await listPages();
|
||||
return {
|
||||
from: prefix.from + '#inst "'.length,
|
||||
from: prefix.from + '#template "'.length,
|
||||
options: allPages.map((pageMeta) => ({
|
||||
label: pageMeta.name,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
prefix = await matchBefore('#include "[^"]*');
|
||||
if (prefix) {
|
||||
let allPages = await listPages();
|
||||
return {
|
||||
from: prefix.from + '#include "'.length,
|
||||
options: allPages.map((pageMeta) => ({
|
||||
label: pageMeta.name,
|
||||
})),
|
||||
|
||||
@@ -16,6 +16,8 @@ import { replaceTemplateVars } from "../core/template";
|
||||
import { jsonToMDTable, queryRegex } from "./util";
|
||||
import { dispatch } from "@plugos/plugos-syscall/event";
|
||||
import { replaceAsync } from "../lib/util";
|
||||
import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown";
|
||||
import { nodeAtPos } from "@silverbulletmd/common/tree";
|
||||
|
||||
export async function updateMaterializedQueriesCommand() {
|
||||
const currentPage = await getCurrentPage();
|
||||
@@ -32,7 +34,7 @@ export async function updateMaterializedQueriesCommand() {
|
||||
}
|
||||
|
||||
export const templateInstRegex =
|
||||
/(<!--\s*#inst\s+"([^"]+)"(.+?)-->)(.+?)(<!--\s*\/inst\s*-->)/gs;
|
||||
/(<!--\s*#(template|include)\s+"([^"]+)"(.+?)-->)(.+?)(<!--\s*\/\2\s*-->)/gs;
|
||||
|
||||
async function updateTemplateInstantiations(
|
||||
text: string,
|
||||
@@ -41,7 +43,7 @@ async function updateTemplateInstantiations(
|
||||
return replaceAsync(
|
||||
text,
|
||||
templateInstRegex,
|
||||
async (fullMatch, startInst, template, args, body, endInst) => {
|
||||
async (fullMatch, startInst, type, template, args, body, endInst) => {
|
||||
args = args.trim();
|
||||
let parsedArgs = {};
|
||||
if (args) {
|
||||
@@ -52,12 +54,26 @@ async function updateTemplateInstantiations(
|
||||
return fullMatch;
|
||||
}
|
||||
}
|
||||
let { text: templateText } = await readPage(template);
|
||||
let templateFn = Handlebars.compile(
|
||||
replaceTemplateVars(templateText, pageName),
|
||||
{ noEscape: true }
|
||||
);
|
||||
let newBody = templateFn(parsedArgs);
|
||||
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 (note a literal "include")
|
||||
if (type === "template") {
|
||||
let templateFn = Handlebars.compile(
|
||||
replaceTemplateVars(templateText, pageName),
|
||||
{ noEscape: true }
|
||||
);
|
||||
newBody = templateFn(parsedArgs);
|
||||
}
|
||||
return `${startInst}\n${newBody.trim()}\n${endInst}`;
|
||||
}
|
||||
);
|
||||
@@ -79,10 +95,19 @@ export async function updateMaterializedQueriesOnPage(
|
||||
return false;
|
||||
}
|
||||
let newText = await updateTemplateInstantiations(text, pageName);
|
||||
let tree = await parseMarkdown(newText);
|
||||
|
||||
newText = await replaceAsync(
|
||||
newText,
|
||||
queryRegex,
|
||||
async (fullMatch, startQuery, query, body, endQuery) => {
|
||||
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;
|
||||
}
|
||||
// console.log("Text slice", newText.substring(index, index + 100));
|
||||
|
||||
let parsedQuery = parseQuery(replaceTemplateVars(query, pageName));
|
||||
|
||||
console.log("Parsed query", parsedQuery);
|
||||
|
||||
Reference in New Issue
Block a user