2022-10-14 21:11:33 +08:00
|
|
|
import type {
|
2022-12-21 21:55:24 +08:00
|
|
|
CompleteEvent,
|
2022-10-14 21:11:33 +08:00
|
|
|
IndexEvent,
|
|
|
|
QueryProviderEvent,
|
|
|
|
} from "$sb/app_event.ts";
|
2022-04-01 23:07:08 +08:00
|
|
|
import {
|
2022-10-14 21:11:33 +08:00
|
|
|
editor,
|
|
|
|
index,
|
|
|
|
markdown,
|
|
|
|
space,
|
|
|
|
} from "$sb/silverbullet-syscall/mod.ts";
|
|
|
|
|
2022-10-16 01:02:56 +08:00
|
|
|
import { events } from "$sb/plugos-syscall/mod.ts";
|
2022-07-12 18:30:36 +08:00
|
|
|
|
2023-07-14 17:52:35 +08:00
|
|
|
import { applyQuery } from "$sb/lib/query.ts";
|
2023-07-02 17:25:32 +08:00
|
|
|
import { invokeFunction } from "$sb/silverbullet-syscall/system.ts";
|
2023-07-28 21:20:56 +08:00
|
|
|
import { backlinkPrefix } from "./page_links.ts";
|
2022-04-21 17:46:33 +08:00
|
|
|
|
|
|
|
// Key space:
|
2023-07-14 17:52:35 +08:00
|
|
|
// meta: => metaJson
|
2022-02-28 21:35:51 +08:00
|
|
|
|
2022-04-19 22:54:47 +08:00
|
|
|
export async function pageQueryProvider({
|
|
|
|
query,
|
2022-04-28 17:55:38 +08:00
|
|
|
}: QueryProviderEvent): Promise<any[]> {
|
2022-11-20 17:24:42 +08:00
|
|
|
return applyQuery(query, await space.listPages());
|
2022-04-19 22:54:47 +08:00
|
|
|
}
|
|
|
|
|
2022-02-28 21:35:51 +08:00
|
|
|
export async function deletePage() {
|
2022-10-14 21:11:33 +08:00
|
|
|
const pageName = await editor.getCurrentPage();
|
2022-11-18 23:04:37 +08:00
|
|
|
if (
|
|
|
|
!await editor.confirm(`Are you sure you would like to delete ${pageName}?`)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-28 20:14:15 +08:00
|
|
|
console.log("Navigating to index page");
|
2022-10-14 21:11:33 +08:00
|
|
|
await editor.navigate("");
|
2022-02-28 21:35:51 +08:00
|
|
|
console.log("Deleting page from space");
|
2022-10-14 21:11:33 +08:00
|
|
|
await space.deletePage(pageName);
|
2022-02-28 21:35:51 +08:00
|
|
|
}
|
|
|
|
|
2023-06-14 15:20:15 +08:00
|
|
|
export async function copyPage() {
|
|
|
|
const oldName = await editor.getCurrentPage();
|
|
|
|
const newName = await editor.prompt(`New page title:`, `${oldName} (copy)`);
|
|
|
|
|
|
|
|
if (!newName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// This throws an error if the page does not exist, which we expect to be the case
|
|
|
|
await space.getPageMeta(newName);
|
|
|
|
// So when we get to this point, we error out
|
|
|
|
throw new Error(
|
2023-07-02 17:25:32 +08:00
|
|
|
`Page ${newName} already exists, cannot rename to existing page.`,
|
2023-06-14 15:20:15 +08:00
|
|
|
);
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e.message === "Not found") {
|
|
|
|
// Expected not found error, so we can continue
|
|
|
|
} else {
|
|
|
|
await editor.flashNotification(e.message, "error");
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const text = await editor.getText();
|
|
|
|
|
|
|
|
console.log("Writing new page to space");
|
|
|
|
await space.writePage(newName, text);
|
|
|
|
|
|
|
|
console.log("Navigating to new page");
|
|
|
|
await editor.navigate(newName);
|
|
|
|
}
|
|
|
|
|
2022-11-18 23:04:37 +08:00
|
|
|
export async function newPageCommand() {
|
|
|
|
const allPages = await space.listPages();
|
|
|
|
let pageName = `Untitled`;
|
|
|
|
let i = 1;
|
|
|
|
while (allPages.find((p) => p.name === pageName)) {
|
|
|
|
pageName = `Untitled ${i}`;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
await editor.navigate(pageName);
|
2022-02-28 21:35:51 +08:00
|
|
|
}
|
|
|
|
|
2022-03-28 21:25:05 +08:00
|
|
|
export async function reindexCommand() {
|
2022-10-14 21:11:33 +08:00
|
|
|
await editor.flashNotification("Reindexing...");
|
2023-05-24 02:53:53 +08:00
|
|
|
await reindexSpace();
|
2022-10-14 21:11:33 +08:00
|
|
|
await editor.flashNotification("Reindexing done");
|
2022-03-28 21:25:05 +08:00
|
|
|
}
|
|
|
|
|
2022-03-29 18:13:46 +08:00
|
|
|
// Completion
|
2022-12-21 21:55:24 +08:00
|
|
|
export async function pageComplete(completeEvent: CompleteEvent) {
|
2023-07-02 17:25:32 +08:00
|
|
|
const match = /\[\[([^\]@:\{}]*)$/.exec(completeEvent.linePrefix);
|
2022-12-21 21:55:24 +08:00
|
|
|
if (!match) {
|
2022-03-29 18:13:46 +08:00
|
|
|
return null;
|
|
|
|
}
|
2022-10-14 21:11:33 +08:00
|
|
|
const allPages = await space.listPages();
|
2022-03-29 18:13:46 +08:00
|
|
|
return {
|
2022-12-21 21:55:24 +08:00
|
|
|
from: completeEvent.pos - match[1].length,
|
2023-07-02 17:25:32 +08:00
|
|
|
options: allPages.map((pageMeta) => {
|
|
|
|
return {
|
|
|
|
label: pageMeta.name,
|
|
|
|
boost: pageMeta.lastModified,
|
|
|
|
type: "page",
|
|
|
|
};
|
|
|
|
}),
|
2022-03-29 18:13:46 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-28 21:25:05 +08:00
|
|
|
export async function reindexSpace() {
|
|
|
|
console.log("Clearing page index...");
|
2022-10-14 21:11:33 +08:00
|
|
|
await index.clearPageIndex();
|
2023-05-24 02:53:53 +08:00
|
|
|
// Executed this way to not have to embed the search plug code here
|
|
|
|
await invokeFunction("client", "search.clearIndex");
|
2022-03-28 21:25:05 +08:00
|
|
|
console.log("Listing all pages");
|
2022-10-14 21:11:33 +08:00
|
|
|
const pages = await space.listPages();
|
2022-10-21 16:00:43 +08:00
|
|
|
let counter = 0;
|
2022-10-14 21:11:33 +08:00
|
|
|
for (const { name } of pages) {
|
2022-10-21 16:00:43 +08:00
|
|
|
counter++;
|
|
|
|
|
|
|
|
console.log(`Indexing page ${counter}/${pages.length}: ${name}`);
|
2022-10-14 21:11:33 +08:00
|
|
|
const text = await space.readPage(name);
|
|
|
|
const parsed = await markdown.parseMarkdown(text);
|
|
|
|
await events.dispatchEvent("page:index", {
|
2022-03-28 21:25:05 +08:00
|
|
|
name,
|
2022-04-20 16:56:43 +08:00
|
|
|
tree: parsed,
|
2022-03-28 21:25:05 +08:00
|
|
|
});
|
|
|
|
}
|
2022-09-14 15:32:47 +08:00
|
|
|
console.log("Indexing completed!");
|
2022-03-28 21:25:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function clearPageIndex(page: string) {
|
2022-10-21 22:56:46 +08:00
|
|
|
// console.log("Clearing page index for page", page);
|
2022-10-14 21:11:33 +08:00
|
|
|
await index.clearPageIndexForPage(page);
|
2022-02-28 21:35:51 +08:00
|
|
|
}
|
2022-04-09 20:28:41 +08:00
|
|
|
|
2022-04-20 16:56:43 +08:00
|
|
|
export async function parseIndexTextRepublish({ name, text }: IndexEvent) {
|
2023-05-24 02:53:53 +08:00
|
|
|
// console.log("Reindexing", name);
|
2022-10-14 21:11:33 +08:00
|
|
|
await events.dispatchEvent("page:index", {
|
2022-04-20 16:56:43 +08:00
|
|
|
name,
|
2022-10-14 21:11:33 +08:00
|
|
|
tree: await markdown.parseMarkdown(text),
|
2022-04-20 16:56:43 +08:00
|
|
|
});
|
|
|
|
}
|