2022-04-03 16:12:16 +00:00
|
|
|
|
import {flashNotification, getCurrentPage, reloadPage, save,} from "plugos-silverbullet-syscall/editor";
|
2022-04-01 15:07:08 +00:00
|
|
|
|
|
2022-04-03 16:12:16 +00:00
|
|
|
|
import {readPage, writePage} from "plugos-silverbullet-syscall/space";
|
|
|
|
|
import {invokeFunctionOnServer} from "plugos-silverbullet-syscall/system";
|
|
|
|
|
import {scanPrefixGlobal} from "plugos-silverbullet-syscall";
|
2022-03-29 15:02:28 +00:00
|
|
|
|
|
|
|
|
|
export const queryRegex =
|
2022-04-03 16:12:16 +00:00
|
|
|
|
/(<!--\s*#query\s+(?<table>\w+)\s*(filter\s+["'“”‘’](?<filter>[^"'“”‘’]+)["'“”‘’])?\s*(group by\s+(?<groupBy>\w+))?\s*-->)(.+?)(<!--\s*#end\s*-->)/gs;
|
2022-03-29 15:02:28 +00:00
|
|
|
|
|
|
|
|
|
export function whiteOutQueries(text: string): string {
|
|
|
|
|
return text.replaceAll(queryRegex, (match) =>
|
|
|
|
|
new Array(match.length + 1).join(" ")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function replaceAsync(
|
|
|
|
|
str: string,
|
|
|
|
|
regex: RegExp,
|
|
|
|
|
asyncFn: (match: string, ...args: any[]) => Promise<string>
|
|
|
|
|
) {
|
|
|
|
|
const promises: Promise<string>[] = [];
|
|
|
|
|
str.replace(regex, (match: string, ...args: any[]): string => {
|
|
|
|
|
const promise = asyncFn(match, ...args);
|
|
|
|
|
promises.push(promise);
|
|
|
|
|
return "";
|
|
|
|
|
});
|
|
|
|
|
const data = await Promise.all(promises);
|
|
|
|
|
return str.replace(regex, () => data.shift()!);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updateMaterializedQueriesCommand() {
|
2022-04-01 15:07:08 +00:00
|
|
|
|
const currentPage = await getCurrentPage();
|
|
|
|
|
await save();
|
|
|
|
|
await invokeFunctionOnServer("updateMaterializedQueriesOnPage", currentPage);
|
|
|
|
|
await reloadPage();
|
|
|
|
|
await flashNotification("Updated materialized queries");
|
2022-03-29 15:02:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called from client, running on server
|
|
|
|
|
export async function updateMaterializedQueriesOnPage(pageName: string) {
|
2022-04-01 15:07:08 +00:00
|
|
|
|
let { text } = await readPage(pageName);
|
2022-03-29 15:02:28 +00:00
|
|
|
|
text = await replaceAsync(text, queryRegex, async (match, ...args) => {
|
|
|
|
|
let { table, filter, groupBy } = args[args.length - 1];
|
|
|
|
|
const startQuery = args[0];
|
|
|
|
|
const endQuery = args[args.length - 4];
|
|
|
|
|
let results = [];
|
|
|
|
|
switch (table) {
|
|
|
|
|
case "task":
|
|
|
|
|
for (let {
|
|
|
|
|
key,
|
|
|
|
|
page,
|
|
|
|
|
value: { task, complete, children },
|
2022-04-01 15:07:08 +00:00
|
|
|
|
} of await scanPrefixGlobal("task:")) {
|
2022-03-29 15:02:28 +00:00
|
|
|
|
let [, pos] = key.split(":");
|
|
|
|
|
if (!filter || (filter && task.includes(filter))) {
|
|
|
|
|
results.push(
|
2022-04-01 15:07:08 +00:00
|
|
|
|
`* [${complete ? "x" : " "}] [[${page}@${pos}]] ${task}` +
|
|
|
|
|
(children ? "\n" + children.join("\n") : "")
|
2022-03-29 15:02:28 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-01 15:07:08 +00:00
|
|
|
|
return `${startQuery}\n${results.sort().join("\n")}\n${endQuery}`;
|
2022-03-31 15:25:34 +00:00
|
|
|
|
case "link":
|
|
|
|
|
let uniqueLinks = new Set<string>();
|
2022-04-01 15:07:08 +00:00
|
|
|
|
for (let { key, page, value: name } of await scanPrefixGlobal(
|
|
|
|
|
`pl:${pageName}:`
|
2022-03-31 15:25:34 +00:00
|
|
|
|
)) {
|
|
|
|
|
let [, pos] = key.split(":");
|
|
|
|
|
if (!filter || (filter && name.includes(filter))) {
|
|
|
|
|
uniqueLinks.add(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const uniqueResult of uniqueLinks) {
|
|
|
|
|
results.push(`* [[${uniqueResult}]]`);
|
|
|
|
|
}
|
|
|
|
|
return `${startQuery}\n${results.sort().join("\n")}\n${endQuery}`;
|
2022-03-29 15:02:28 +00:00
|
|
|
|
case "item":
|
|
|
|
|
for (let {
|
|
|
|
|
key,
|
|
|
|
|
page,
|
2022-04-01 13:02:35 +00:00
|
|
|
|
value: { item, children },
|
2022-04-01 15:07:08 +00:00
|
|
|
|
} of await scanPrefixGlobal("it:")) {
|
2022-03-29 15:02:28 +00:00
|
|
|
|
let [, pos] = key.split(":");
|
|
|
|
|
if (!filter || (filter && item.includes(filter))) {
|
2022-04-01 15:07:08 +00:00
|
|
|
|
results.push(
|
|
|
|
|
`* [[${page}@${pos}]] ${item}` +
|
|
|
|
|
(children ? "\n" + children.join("\n") : "")
|
|
|
|
|
);
|
2022-03-29 15:02:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-01 15:07:08 +00:00
|
|
|
|
return `${startQuery}\n${results.sort().join("\n")}\n${endQuery}`;
|
2022-03-29 15:02:28 +00:00
|
|
|
|
default:
|
|
|
|
|
return match;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// console.log("New text", text);
|
2022-04-01 15:07:08 +00:00
|
|
|
|
await writePage(pageName, text);
|
2022-03-29 15:02:28 +00:00
|
|
|
|
}
|