2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
listPages,
|
|
|
|
readPage,
|
|
|
|
writePage,
|
2022-04-25 09:24:13 +00:00
|
|
|
} from "@silverbulletmd/plugos-silverbullet-syscall/space";
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
filterBox,
|
2022-07-04 13:07:27 +00:00
|
|
|
getCurrentPage,
|
|
|
|
getCursor,
|
|
|
|
insertAtCursor,
|
2022-05-06 16:55:04 +00:00
|
|
|
moveCursor,
|
2022-04-25 08:33:38 +00:00
|
|
|
navigate,
|
|
|
|
prompt,
|
2022-04-25 09:24:13 +00:00
|
|
|
} from "@silverbulletmd/plugos-silverbullet-syscall/editor";
|
|
|
|
import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown";
|
2022-04-13 12:46:52 +00:00
|
|
|
import { extractMeta } from "../query/data";
|
2022-04-21 11:57:45 +00:00
|
|
|
import { renderToText } from "@silverbulletmd/common/tree";
|
2022-04-13 12:46:52 +00:00
|
|
|
import { niceDate } from "./dates";
|
|
|
|
|
|
|
|
const pageTemplatePrefix = `template/page/`;
|
2022-07-04 13:07:27 +00:00
|
|
|
const snippetPrefix = `snippet/`;
|
2022-04-13 12:46:52 +00:00
|
|
|
|
|
|
|
export async function instantiateTemplateCommand() {
|
|
|
|
let allPages = await listPages();
|
|
|
|
let allPageTemplates = allPages.filter((pageMeta) =>
|
|
|
|
pageMeta.name.startsWith(pageTemplatePrefix)
|
|
|
|
);
|
|
|
|
|
|
|
|
let selectedTemplate = await filterBox(
|
|
|
|
"Template",
|
|
|
|
allPageTemplates,
|
|
|
|
"Select the template to create a new page from"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!selectedTemplate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log("Selected template", selectedTemplate);
|
|
|
|
|
|
|
|
let { text } = await readPage(selectedTemplate.name);
|
|
|
|
|
|
|
|
let parseTree = await parseMarkdown(text);
|
2022-05-06 16:55:04 +00:00
|
|
|
let additionalPageMeta = extractMeta(parseTree, ["name"]);
|
2022-04-13 12:46:52 +00:00
|
|
|
console.log("Page meta", additionalPageMeta);
|
|
|
|
|
|
|
|
let pageName = await prompt("Name of new page", additionalPageMeta.name);
|
|
|
|
if (!pageName) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
let pageText = replaceTemplateVars(renderToText(parseTree), pageName);
|
2022-04-19 14:54:47 +00:00
|
|
|
await writePage(pageName, pageText);
|
2022-04-21 09:46:33 +00:00
|
|
|
await navigate(pageName);
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 13:07:27 +00:00
|
|
|
export async function insertSnippet() {
|
|
|
|
let allPages = await listPages();
|
|
|
|
let cursorPos = await getCursor();
|
|
|
|
let page = await getCurrentPage();
|
|
|
|
let allSnippets = allPages.filter((pageMeta) =>
|
|
|
|
pageMeta.name.startsWith(snippetPrefix)
|
|
|
|
);
|
|
|
|
|
|
|
|
let selectedSnippet = await filterBox(
|
|
|
|
"Snippet",
|
|
|
|
allSnippets,
|
|
|
|
`Select the snippet to insert (listing any page starting with <tt>${snippetPrefix}</tt>)`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!selectedSnippet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let { text } = await readPage(selectedSnippet.name);
|
|
|
|
|
|
|
|
let templateText = replaceTemplateVars(text, page);
|
|
|
|
let carretPos = templateText.indexOf("|^|");
|
|
|
|
templateText = templateText.replace("|^|", "");
|
|
|
|
templateText = replaceTemplateVars(templateText, page);
|
|
|
|
await insertAtCursor(templateText);
|
|
|
|
if (carretPos !== -1) {
|
|
|
|
await moveCursor(cursorPos + carretPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 16:55:04 +00:00
|
|
|
// TODO: This should probably be replaced with handlebards somehow?
|
2022-04-19 14:54:47 +00:00
|
|
|
export function replaceTemplateVars(s: string, pageName: string): string {
|
|
|
|
return s.replaceAll(/\{\{([^\}]+)\}\}/g, (match, v) => {
|
2022-04-20 08:56:43 +00:00
|
|
|
switch (v) {
|
|
|
|
case "today":
|
|
|
|
return niceDate(new Date());
|
2022-07-04 13:07:27 +00:00
|
|
|
case "tomorrow":
|
|
|
|
let tomorrow = new Date();
|
|
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
return niceDate(tomorrow);
|
2022-04-20 08:56:43 +00:00
|
|
|
case "yesterday":
|
|
|
|
let yesterday = new Date();
|
|
|
|
yesterday.setDate(yesterday.getDate() - 1);
|
|
|
|
return niceDate(yesterday);
|
|
|
|
case "lastWeek":
|
|
|
|
let lastWeek = new Date();
|
|
|
|
lastWeek.setDate(lastWeek.getDate() - 7);
|
|
|
|
return niceDate(lastWeek);
|
2022-07-04 13:07:27 +00:00
|
|
|
case "page":
|
|
|
|
return pageName;
|
2022-04-13 12:46:52 +00:00
|
|
|
}
|
|
|
|
return match;
|
|
|
|
});
|
|
|
|
}
|
2022-05-06 16:55:04 +00:00
|
|
|
|
|
|
|
export async function quickNoteCommand() {
|
|
|
|
let isoDate = new Date().toISOString();
|
|
|
|
let [date, time] = isoDate.split("T");
|
|
|
|
time = time.split(".")[0];
|
|
|
|
let pageName = `📥 ${date} ${time}`;
|
|
|
|
await navigate(pageName);
|
|
|
|
}
|
|
|
|
|
2022-07-04 13:07:27 +00:00
|
|
|
export async function insertTemplateText(cmdDef: any) {
|
|
|
|
let cursorPos = await getCursor();
|
|
|
|
let page = await getCurrentPage();
|
|
|
|
let templateText: string = cmdDef.value;
|
|
|
|
let carretPos = templateText.indexOf("|^|");
|
|
|
|
templateText = templateText.replace("|^|", "");
|
|
|
|
templateText = replaceTemplateVars(templateText, page);
|
|
|
|
await insertAtCursor(templateText);
|
|
|
|
if (carretPos !== -1) {
|
|
|
|
await moveCursor(cursorPos + carretPos);
|
|
|
|
}
|
2022-05-06 16:55:04 +00:00
|
|
|
}
|