2022-10-14 13:11:33 +00:00
|
|
|
import { editor, markdown, space } from "$sb/silverbullet-syscall/mod.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { extractMeta } from "../query/data.ts";
|
2022-10-14 13:11:33 +00:00
|
|
|
import { renderToText } from "$sb/lib/tree.ts";
|
|
|
|
import { niceDate } from "$sb/lib/dates.ts";
|
|
|
|
import { readSettings } from "$sb/lib/settings_page.ts";
|
2022-04-13 12:46:52 +00:00
|
|
|
|
|
|
|
export async function instantiateTemplateCommand() {
|
2022-10-14 13:11:33 +00:00
|
|
|
const allPages = await space.listPages();
|
2022-10-10 12:50:21 +00:00
|
|
|
const { pageTemplatePrefix } = await readSettings({
|
2022-07-15 09:17:02 +00:00
|
|
|
pageTemplatePrefix: "template/page/",
|
|
|
|
});
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const selectedTemplate = await editor.filterBox(
|
2022-04-13 12:46:52 +00:00
|
|
|
"Template",
|
2022-08-08 11:56:04 +00:00
|
|
|
allPages
|
|
|
|
.filter((pageMeta) => pageMeta.name.startsWith(pageTemplatePrefix))
|
|
|
|
.map((pageMeta) => ({
|
|
|
|
...pageMeta,
|
|
|
|
name: pageMeta.name.slice(pageTemplatePrefix.length),
|
|
|
|
})),
|
2022-10-10 12:50:21 +00:00
|
|
|
`Select the template to create a new page from (listing any page starting with <tt>${pageTemplatePrefix}</tt>)`,
|
2022-04-13 12:46:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!selectedTemplate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log("Selected template", selectedTemplate);
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const text = await space.readPage(
|
2022-10-10 12:50:21 +00:00
|
|
|
`${pageTemplatePrefix}${selectedTemplate.name}`,
|
2022-08-08 11:56:04 +00:00
|
|
|
);
|
2022-04-13 12:46:52 +00:00
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const parseTree = await markdown.parseMarkdown(text);
|
2022-10-10 12:50:21 +00:00
|
|
|
const additionalPageMeta = extractMeta(parseTree, [
|
2022-08-09 13:37:47 +00:00
|
|
|
"$name",
|
|
|
|
"$disableDirectives",
|
|
|
|
]);
|
2022-04-13 12:46:52 +00:00
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const pageName = await editor.prompt(
|
|
|
|
"Name of new page",
|
|
|
|
additionalPageMeta.$name,
|
|
|
|
);
|
2022-04-13 12:46:52 +00:00
|
|
|
if (!pageName) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
const pageText = replaceTemplateVars(renderToText(parseTree), pageName);
|
2022-10-14 13:11:33 +00:00
|
|
|
await space.writePage(pageName, pageText);
|
|
|
|
await editor.navigate(pageName);
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 13:07:27 +00:00
|
|
|
export async function insertSnippet() {
|
2022-10-14 13:11:33 +00:00
|
|
|
const allPages = await space.listPages();
|
|
|
|
const { snippetPrefix } = await readSettings({
|
2022-07-15 09:17:02 +00:00
|
|
|
snippetPrefix: "snippet/",
|
|
|
|
});
|
2022-10-14 13:11:33 +00:00
|
|
|
const cursorPos = await editor.getCursor();
|
|
|
|
const page = await editor.getCurrentPage();
|
|
|
|
const allSnippets = allPages
|
2022-07-15 09:17:02 +00:00
|
|
|
.filter((pageMeta) => pageMeta.name.startsWith(snippetPrefix))
|
|
|
|
.map((pageMeta) => ({
|
|
|
|
...pageMeta,
|
|
|
|
name: pageMeta.name.slice(snippetPrefix.length),
|
|
|
|
}));
|
2022-07-04 13:07:27 +00:00
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const selectedSnippet = await editor.filterBox(
|
2022-07-04 13:07:27 +00:00
|
|
|
"Snippet",
|
|
|
|
allSnippets,
|
2022-10-10 12:50:21 +00:00
|
|
|
`Select the snippet to insert (listing any page starting with <tt>${snippetPrefix}</tt>)`,
|
2022-07-04 13:07:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!selectedSnippet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const text = await space.readPage(`${snippetPrefix}${selectedSnippet.name}`);
|
2022-07-04 13:07:27 +00:00
|
|
|
let templateText = replaceTemplateVars(text, page);
|
2022-10-14 13:11:33 +00:00
|
|
|
const carretPos = templateText.indexOf("|^|");
|
2022-07-04 13:07:27 +00:00
|
|
|
templateText = templateText.replace("|^|", "");
|
|
|
|
templateText = replaceTemplateVars(templateText, page);
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.insertAtCursor(templateText);
|
2022-07-04 13:07:27 +00:00
|
|
|
if (carretPos !== -1) {
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.moveCursor(cursorPos + carretPos);
|
2022-07-04 13:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-14 13:11:33 +00:00
|
|
|
case "tomorrow": {
|
|
|
|
const tomorrow = new Date();
|
2022-07-04 13:07:27 +00:00
|
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
return niceDate(tomorrow);
|
2022-10-14 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "yesterday": {
|
|
|
|
const yesterday = new Date();
|
2022-04-20 08:56:43 +00:00
|
|
|
yesterday.setDate(yesterday.getDate() - 1);
|
|
|
|
return niceDate(yesterday);
|
2022-10-14 13:11:33 +00:00
|
|
|
}
|
|
|
|
case "lastWeek": {
|
|
|
|
const lastWeek = new Date();
|
2022-04-20 08:56:43 +00:00
|
|
|
lastWeek.setDate(lastWeek.getDate() - 7);
|
|
|
|
return niceDate(lastWeek);
|
2022-10-14 13:11:33 +00:00
|
|
|
}
|
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() {
|
2022-10-14 13:11:33 +00:00
|
|
|
const { quickNotePrefix } = await readSettings({
|
2022-08-08 11:56:04 +00:00
|
|
|
quickNotePrefix: "📥 ",
|
|
|
|
});
|
2022-10-14 13:11:33 +00:00
|
|
|
const isoDate = new Date().toISOString();
|
2022-05-06 16:55:04 +00:00
|
|
|
let [date, time] = isoDate.split("T");
|
|
|
|
time = time.split(".")[0];
|
2022-10-14 13:11:33 +00:00
|
|
|
const pageName = `${quickNotePrefix}${date} ${time}`;
|
|
|
|
await editor.navigate(pageName);
|
2022-05-06 16:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 10:18:47 +00:00
|
|
|
export async function dailyNoteCommand() {
|
2022-10-14 13:11:33 +00:00
|
|
|
const { dailyNoteTemplate, dailyNotePrefix } = await readSettings({
|
2022-08-08 11:56:04 +00:00
|
|
|
dailyNoteTemplate: "template/page/Daily Note",
|
|
|
|
dailyNotePrefix: "📅 ",
|
|
|
|
});
|
|
|
|
let dailyNoteTemplateText = "";
|
|
|
|
try {
|
2022-10-14 13:11:33 +00:00
|
|
|
const text = await space.readPage(dailyNoteTemplate);
|
2022-08-08 11:56:04 +00:00
|
|
|
dailyNoteTemplateText = text;
|
|
|
|
} catch {
|
|
|
|
console.warn(`No daily note template found at ${dailyNoteTemplate}`);
|
|
|
|
}
|
2022-10-14 13:11:33 +00:00
|
|
|
const date = niceDate(new Date());
|
|
|
|
const pageName = `${dailyNotePrefix}${date}`;
|
2022-08-08 11:56:04 +00:00
|
|
|
if (dailyNoteTemplateText) {
|
|
|
|
try {
|
2022-10-14 13:11:33 +00:00
|
|
|
await space.getPageMeta(pageName);
|
2022-08-08 11:56:04 +00:00
|
|
|
} catch {
|
|
|
|
// Doesn't exist, let's create
|
2022-10-14 13:11:33 +00:00
|
|
|
await space.writePage(
|
2022-08-08 11:56:04 +00:00
|
|
|
pageName,
|
2022-10-10 12:50:21 +00:00
|
|
|
replaceTemplateVars(dailyNoteTemplateText, pageName),
|
2022-08-08 11:56:04 +00:00
|
|
|
);
|
|
|
|
}
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.navigate(pageName);
|
2022-08-08 11:56:04 +00:00
|
|
|
} else {
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.navigate(pageName);
|
2022-08-08 11:56:04 +00:00
|
|
|
}
|
2022-07-06 10:18:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 13:07:27 +00:00
|
|
|
export async function insertTemplateText(cmdDef: any) {
|
2022-10-14 13:11:33 +00:00
|
|
|
const cursorPos = await editor.getCursor();
|
|
|
|
const page = await editor.getCurrentPage();
|
2022-07-04 13:07:27 +00:00
|
|
|
let templateText: string = cmdDef.value;
|
2022-10-14 13:11:33 +00:00
|
|
|
const carretPos = templateText.indexOf("|^|");
|
2022-07-04 13:07:27 +00:00
|
|
|
templateText = templateText.replace("|^|", "");
|
|
|
|
templateText = replaceTemplateVars(templateText, page);
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.insertAtCursor(templateText);
|
2022-07-04 13:07:27 +00:00
|
|
|
if (carretPos !== -1) {
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.moveCursor(cursorPos + carretPos);
|
2022-07-04 13:07:27 +00:00
|
|
|
}
|
2022-05-06 16:55:04 +00:00
|
|
|
}
|