2023-11-06 08:14:16 +00:00
|
|
|
import { editor, handlebars, markdown, space, YAML } from "$sb/syscalls.ts";
|
2023-11-03 11:04:51 +00:00
|
|
|
import { niceDate, niceTime } from "$sb/lib/dates.ts";
|
2022-10-14 13:11:33 +00:00
|
|
|
import { readSettings } from "$sb/lib/settings_page.ts";
|
2023-08-17 18:27:05 +00:00
|
|
|
import { cleanPageRef } from "$sb/lib/resolve.ts";
|
2023-11-09 08:26:44 +00:00
|
|
|
import { PageMeta } from "$sb/types.ts";
|
2023-11-06 08:14:16 +00:00
|
|
|
import { getObjectByRef, queryObjects } from "../index/plug_api.ts";
|
2023-11-09 08:26:44 +00:00
|
|
|
import { TemplateObject } from "./types.ts";
|
|
|
|
import { renderTemplate } from "./api.ts";
|
2023-11-06 08:14:16 +00:00
|
|
|
|
2023-12-21 17:37:50 +00:00
|
|
|
export async function newPageCommand(
|
|
|
|
_cmdDef: any,
|
|
|
|
templateName?: string,
|
|
|
|
askName = true,
|
|
|
|
) {
|
|
|
|
if (!templateName) {
|
|
|
|
const allPageTemplates = await queryObjects<TemplateObject>("template", {
|
|
|
|
// Only return templates that have a trigger
|
|
|
|
filter: ["=", ["attr", "type"], ["string", "page"]],
|
|
|
|
});
|
|
|
|
const selectedTemplate = await editor.filterBox(
|
|
|
|
"Page template",
|
|
|
|
allPageTemplates
|
|
|
|
.map((pageMeta) => ({
|
|
|
|
...pageMeta,
|
|
|
|
name: pageMeta.displayName || pageMeta.ref,
|
|
|
|
})),
|
|
|
|
`Select the template to create a new page from (listing any page tagged with <tt>#template</tt> and 'page' set as 'type')`,
|
|
|
|
);
|
2023-11-13 14:49:21 +00:00
|
|
|
|
2023-12-21 17:37:50 +00:00
|
|
|
if (!selectedTemplate) {
|
|
|
|
return;
|
2023-11-13 14:49:21 +00:00
|
|
|
}
|
2023-12-21 17:37:50 +00:00
|
|
|
templateName = selectedTemplate.ref;
|
2023-11-06 08:14:16 +00:00
|
|
|
}
|
2023-12-21 17:37:50 +00:00
|
|
|
console.log("Selected template", templateName);
|
2022-07-15 09:17:02 +00:00
|
|
|
|
2023-12-21 17:37:50 +00:00
|
|
|
const templateText = await space.readPage(templateName!);
|
2022-04-13 12:46:52 +00:00
|
|
|
|
2023-07-02 09:25:32 +00:00
|
|
|
const tempPageMeta: PageMeta = {
|
2023-11-06 08:14:16 +00:00
|
|
|
tags: ["page"],
|
|
|
|
ref: "",
|
2023-07-02 09:25:32 +00:00
|
|
|
name: "",
|
2023-11-06 08:14:16 +00:00
|
|
|
created: "",
|
|
|
|
lastModified: "",
|
2023-07-02 09:25:32 +00:00
|
|
|
perm: "rw",
|
|
|
|
};
|
2023-12-21 17:37:50 +00:00
|
|
|
// Just used to extract the frontmatter
|
|
|
|
const { frontmatter } = await renderTemplate(
|
|
|
|
templateText,
|
|
|
|
tempPageMeta,
|
|
|
|
);
|
2023-07-02 09:25:32 +00:00
|
|
|
|
2023-12-21 17:37:50 +00:00
|
|
|
let pageName: string | undefined = await replaceTemplateVars(
|
|
|
|
frontmatter?.pageName || "",
|
|
|
|
tempPageMeta,
|
2022-10-14 13:11:33 +00:00
|
|
|
);
|
2023-12-21 17:37:50 +00:00
|
|
|
|
|
|
|
if (askName) {
|
|
|
|
pageName = await editor.prompt(
|
|
|
|
"Name of new page",
|
|
|
|
await replaceTemplateVars(frontmatter?.pageName || "", tempPageMeta),
|
|
|
|
);
|
|
|
|
if (!pageName) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-13 12:46:52 +00:00
|
|
|
}
|
2023-07-02 09:25:32 +00:00
|
|
|
tempPageMeta.name = pageName;
|
2023-02-27 14:51:54 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Fails if doesn't exist
|
2023-03-06 13:17:03 +00:00
|
|
|
await space.getPageMeta(pageName);
|
|
|
|
|
2023-02-27 14:51:54 +00:00
|
|
|
// So, page exists, let's warn
|
|
|
|
if (
|
|
|
|
!await editor.confirm(
|
|
|
|
`Page ${pageName} already exists, are you sure you want to override it?`,
|
|
|
|
)
|
|
|
|
) {
|
2023-12-21 17:37:50 +00:00
|
|
|
// Just navigate there without instantiating
|
|
|
|
return editor.navigate(pageName);
|
2023-02-27 14:51:54 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// The preferred scenario, let's keep going
|
|
|
|
}
|
|
|
|
|
2023-12-21 17:37:50 +00:00
|
|
|
const { text: pageText, renderedFrontmatter } = await renderTemplate(
|
|
|
|
templateText,
|
2023-10-03 12:16:33 +00:00
|
|
|
tempPageMeta,
|
|
|
|
);
|
2023-12-21 17:37:50 +00:00
|
|
|
let fullPageText = renderedFrontmatter
|
|
|
|
? "---\n" + renderedFrontmatter + "---\n" + pageText
|
|
|
|
: pageText;
|
|
|
|
const carretPos = fullPageText.indexOf("|^|");
|
|
|
|
fullPageText = fullPageText.replace("|^|", "");
|
|
|
|
await space.writePage(
|
|
|
|
pageName,
|
|
|
|
fullPageText,
|
2023-08-09 08:55:35 +00:00
|
|
|
);
|
2023-12-21 17:37:50 +00:00
|
|
|
await editor.navigate(pageName, carretPos !== -1 ? carretPos : undefined);
|
2023-08-09 08:55:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 16:52:56 +00:00
|
|
|
export async function loadPageObject(pageName?: string): Promise<PageMeta> {
|
|
|
|
if (!pageName) {
|
|
|
|
return {
|
|
|
|
ref: "",
|
|
|
|
name: "",
|
|
|
|
tags: ["page"],
|
|
|
|
lastModified: "",
|
|
|
|
created: "",
|
|
|
|
} as PageMeta;
|
|
|
|
}
|
2023-11-06 08:14:16 +00:00
|
|
|
return (await getObjectByRef<PageMeta>(
|
|
|
|
pageName,
|
|
|
|
"page",
|
|
|
|
pageName,
|
|
|
|
)) || {
|
|
|
|
ref: pageName,
|
|
|
|
name: pageName,
|
|
|
|
tags: ["page"],
|
|
|
|
lastModified: "",
|
|
|
|
created: "",
|
|
|
|
} as PageMeta;
|
|
|
|
}
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
export function replaceTemplateVars(
|
|
|
|
s: string,
|
|
|
|
pageMeta: PageMeta,
|
|
|
|
): Promise<string> {
|
|
|
|
return handlebars.renderTemplate(s, {}, { page: pageMeta });
|
2022-04-13 12:46:52 +00:00
|
|
|
}
|
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: "📥 ",
|
|
|
|
});
|
2023-11-03 11:04:51 +00:00
|
|
|
const date = niceDate(new Date());
|
|
|
|
const time = niceTime(new Date());
|
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({
|
2023-08-17 18:27:05 +00:00
|
|
|
dailyNoteTemplate: "[[template/page/Daily Note]]",
|
2022-08-08 11:56:04 +00:00
|
|
|
dailyNotePrefix: "📅 ",
|
|
|
|
});
|
2022-10-14 13:11:33 +00:00
|
|
|
const date = niceDate(new Date());
|
|
|
|
const pageName = `${dailyNotePrefix}${date}`;
|
2023-07-02 09:25:32 +00:00
|
|
|
let carretPos = 0;
|
2023-05-23 18:53:53 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await space.getPageMeta(pageName);
|
|
|
|
} catch {
|
|
|
|
// Doesn't exist, let's create
|
|
|
|
let dailyNoteTemplateText = "";
|
2022-08-08 11:56:04 +00:00
|
|
|
try {
|
2023-08-17 18:27:05 +00:00
|
|
|
dailyNoteTemplateText = await space.readPage(
|
|
|
|
cleanPageRef(dailyNoteTemplate),
|
|
|
|
);
|
2023-07-02 09:25:32 +00:00
|
|
|
carretPos = dailyNoteTemplateText.indexOf("|^|");
|
|
|
|
if (carretPos === -1) {
|
|
|
|
carretPos = 0;
|
|
|
|
}
|
|
|
|
dailyNoteTemplateText = dailyNoteTemplateText.replace("|^|", "");
|
2022-08-08 11:56:04 +00:00
|
|
|
} catch {
|
2023-05-23 18:53:53 +00:00
|
|
|
console.warn(`No daily note template found at ${dailyNoteTemplate}`);
|
2022-08-08 11:56:04 +00:00
|
|
|
}
|
2023-07-02 09:25:32 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
await space.writePage(
|
|
|
|
pageName,
|
2023-10-03 12:16:33 +00:00
|
|
|
await replaceTemplateVars(dailyNoteTemplateText, {
|
2023-11-06 08:14:16 +00:00
|
|
|
tags: ["page"],
|
|
|
|
ref: pageName,
|
2023-07-02 09:25:32 +00:00
|
|
|
name: pageName,
|
2023-11-06 08:14:16 +00:00
|
|
|
created: "",
|
|
|
|
lastModified: "",
|
2023-07-02 09:25:32 +00:00
|
|
|
perm: "rw",
|
|
|
|
}),
|
2023-05-23 18:53:53 +00:00
|
|
|
);
|
2022-08-08 11:56:04 +00:00
|
|
|
}
|
2023-07-02 09:25:32 +00:00
|
|
|
await editor.navigate(pageName, carretPos);
|
2022-07-06 10:18:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 08:06:30 +00:00
|
|
|
function getWeekStartDate(monday = false) {
|
|
|
|
const d = new Date();
|
|
|
|
const day = d.getDay();
|
|
|
|
let diff = d.getDate() - day;
|
|
|
|
if (monday) {
|
|
|
|
diff += day == 0 ? -6 : 1;
|
|
|
|
}
|
|
|
|
return new Date(d.setDate(diff));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function weeklyNoteCommand() {
|
|
|
|
const { weeklyNoteTemplate, weeklyNotePrefix, weeklyNoteMonday } =
|
|
|
|
await readSettings({
|
2023-08-17 18:27:05 +00:00
|
|
|
weeklyNoteTemplate: "[[template/page/Weekly Note]]",
|
2022-11-02 08:06:30 +00:00
|
|
|
weeklyNotePrefix: "🗓️ ",
|
|
|
|
weeklyNoteMonday: false,
|
|
|
|
});
|
|
|
|
let weeklyNoteTemplateText = "";
|
|
|
|
try {
|
2023-08-17 18:27:05 +00:00
|
|
|
weeklyNoteTemplateText = await space.readPage(
|
|
|
|
cleanPageRef(weeklyNoteTemplate),
|
|
|
|
);
|
2022-11-02 08:06:30 +00:00
|
|
|
} catch {
|
|
|
|
console.warn(`No weekly note template found at ${weeklyNoteTemplate}`);
|
|
|
|
}
|
|
|
|
const date = niceDate(getWeekStartDate(weeklyNoteMonday));
|
|
|
|
const pageName = `${weeklyNotePrefix}${date}`;
|
|
|
|
if (weeklyNoteTemplateText) {
|
|
|
|
try {
|
|
|
|
await space.getPageMeta(pageName);
|
|
|
|
} catch {
|
|
|
|
// Doesn't exist, let's create
|
|
|
|
await space.writePage(
|
|
|
|
pageName,
|
2023-10-03 12:16:33 +00:00
|
|
|
await replaceTemplateVars(weeklyNoteTemplateText, {
|
2023-07-02 09:25:32 +00:00
|
|
|
name: pageName,
|
2023-11-06 08:14:16 +00:00
|
|
|
ref: pageName,
|
|
|
|
tags: ["page"],
|
|
|
|
created: "",
|
|
|
|
lastModified: "",
|
2023-07-02 09:25:32 +00:00
|
|
|
perm: "rw",
|
|
|
|
}),
|
2022-11-02 08:06:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
await editor.navigate(pageName);
|
|
|
|
} else {
|
|
|
|
await editor.navigate(pageName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2023-11-06 08:14:16 +00:00
|
|
|
const pageMeta = await loadPageObject(page);
|
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("|^|", "");
|
2023-11-06 08:14:16 +00:00
|
|
|
templateText = await replaceTemplateVars(templateText, pageMeta);
|
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
|
|
|
}
|
2022-11-18 15:04:37 +00:00
|
|
|
|
|
|
|
export async function applyLineReplace(cmdDef: any) {
|
|
|
|
const cursorPos = await editor.getCursor();
|
|
|
|
const text = await editor.getText();
|
|
|
|
const matchRegex = new RegExp(cmdDef.match);
|
|
|
|
let startOfLine = cursorPos;
|
|
|
|
while (startOfLine > 0 && text[startOfLine - 1] !== "\n") {
|
|
|
|
startOfLine--;
|
|
|
|
}
|
|
|
|
let currentLine = text.slice(startOfLine, cursorPos);
|
|
|
|
|
|
|
|
const emptyLine = !currentLine;
|
|
|
|
|
|
|
|
currentLine = currentLine.replace(matchRegex, cmdDef.replace);
|
|
|
|
|
|
|
|
await editor.dispatch({
|
|
|
|
changes: {
|
|
|
|
from: startOfLine,
|
|
|
|
to: cursorPos,
|
|
|
|
insert: currentLine,
|
|
|
|
},
|
|
|
|
selection: emptyLine
|
|
|
|
? {
|
|
|
|
anchor: startOfLine + currentLine.length,
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
});
|
|
|
|
}
|