1
0
silverbullet/plugs/core/page.ts

181 lines
5.1 KiB
TypeScript
Raw Normal View History

2022-10-14 13:11:33 +00:00
import type {
CompleteEvent,
2022-10-14 13:11:33 +00:00
IndexEvent,
QueryProviderEvent,
} from "$sb/app_event.ts";
2022-04-01 15:07:08 +00:00
import {
2022-10-14 13:11:33 +00:00
editor,
index,
markdown,
space,
} from "$sb/silverbullet-syscall/mod.ts";
2023-08-10 16:32:41 +00:00
import { events, mq } from "$sb/plugos-syscall/mod.ts";
import { applyQuery } from "$sb/lib/query.ts";
2023-08-27 12:13:18 +00:00
import { invoke } from "$sb/silverbullet-syscall/system.ts";
2023-08-11 18:37:13 +00:00
import type { Message } from "$sb/types.ts";
import { sleep } from "../../common/async_util.ts";
import { cacheFileListing } from "../federation/federation.ts";
import type { PageMeta } from "../../web/types.ts";
// Key space:
// meta: => metaJson
2022-02-28 13:35:51 +00:00
export async function pageQueryProvider({
query,
}: QueryProviderEvent): Promise<any[]> {
return applyQuery(query, await space.listPages());
}
2022-02-28 13:35:51 +00:00
export async function deletePage() {
2022-10-14 13:11:33 +00:00
const pageName = await editor.getCurrentPage();
if (
!await editor.confirm(`Are you sure you would like to delete ${pageName}?`)
) {
return;
}
2022-06-28 12:14:15 +00:00
console.log("Navigating to index page");
2022-10-14 13:11:33 +00:00
await editor.navigate("");
2022-02-28 13:35:51 +00:00
console.log("Deleting page from space");
2022-10-14 13:11:33 +00:00
await space.deletePage(pageName);
2022-02-28 13:35:51 +00:00
}
2023-06-14 07:20:15 +00: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(
`Page ${newName} already exists, cannot rename to existing page.`,
2023-06-14 07:20:15 +00: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);
}
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 13:35:51 +00:00
}
2022-03-28 13:25:05 +00:00
export async function reindexCommand() {
2023-08-11 18:37:13 +00:00
await editor.flashNotification("Performing full page reindex...");
await reindexSpace();
await editor.flashNotification("Done with page index!");
2022-03-28 13:25:05 +00:00
}
2022-03-29 10:13:46 +00:00
// Completion
export async function pageComplete(completeEvent: CompleteEvent) {
const match = /\[\[([^\]@:\{}]*)$/.exec(completeEvent.linePrefix);
if (!match) {
2022-03-29 10:13:46 +00:00
return null;
}
let allPages: PageMeta[] = await space.listPages();
const prefix = match[1];
if (prefix.startsWith("!")) {
// Federation prefix, let's first see if we're matching anything from federation that is locally synced
const prefixMatches = allPages.filter((pageMeta) =>
pageMeta.name.startsWith(prefix)
);
if (prefixMatches.length === 0) {
// Ok, nothing synced in via federation, let's see if this URI is complete enough to try to fetch index.json
if (prefix.includes("/")) {
// Yep
const domain = prefix.split("/")[0];
// Cached listing
allPages = (await cacheFileListing(domain)).filter((fm) =>
fm.name.endsWith(".md")
).map((fm) => ({
...fm,
name: fm.name.slice(0, -3),
}));
}
}
}
2022-03-29 10:13:46 +00:00
return {
from: completeEvent.pos - match[1].length,
options: allPages.map((pageMeta) => {
return {
label: pageMeta.name,
boost: pageMeta.lastModified,
type: "page",
};
}),
2022-03-29 10:13:46 +00:00
};
}
2022-03-28 13:25:05 +00:00
export async function reindexSpace() {
console.log("Clearing page index...");
2022-10-14 13:11:33 +00:00
await index.clearPageIndex();
// Executed this way to not have to embed the search plug code here
2023-08-27 12:13:18 +00:00
await invoke("search.clearIndex");
2022-10-14 13:11:33 +00:00
const pages = await space.listPages();
2023-08-11 18:37:13 +00:00
// Queue all page names to be indexed
await mq.batchSend("indexQueue", pages.map((page) => page.name));
// Now let's wait for the processing to finish
let queueStats = await mq.getQueueStats("indexQueue");
while (queueStats.queued > 0 || queueStats.processing > 0) {
sleep(1000);
queueStats = await mq.getQueueStats("indexQueue");
2022-03-28 13:25:05 +00:00
}
2023-08-11 18:37:13 +00:00
// And notify the user
console.log("Indexing completed!");
2022-03-28 13:25:05 +00:00
}
2023-08-10 16:32:41 +00:00
export async function processIndexQueue(messages: Message[]) {
for (const message of messages) {
const name: string = message.body;
console.log(`Indexing page ${name}`);
const text = await space.readPage(name);
2023-08-26 06:31:51 +00:00
// console.log("Going to parse markdown");
2023-08-10 16:32:41 +00:00
const parsed = await markdown.parseMarkdown(text);
2023-08-26 06:31:51 +00:00
// console.log("Dispatching ;age:index");
2023-08-10 16:32:41 +00:00
await events.dispatchEvent("page:index", {
name,
tree: parsed,
});
}
}
2022-03-28 13:25:05 +00:00
export async function clearPageIndex(page: string) {
2022-10-21 14:56:46 +00:00
// console.log("Clearing page index for page", page);
2022-10-14 13:11:33 +00:00
await index.clearPageIndexForPage(page);
2022-02-28 13:35:51 +00:00
}
2022-04-09 12:28:41 +00:00
export async function parseIndexTextRepublish({ name, text }: IndexEvent) {
2023-08-27 12:13:18 +00:00
console.log("Reindexing", name);
2022-10-14 13:11:33 +00:00
await events.dispatchEvent("page:index", {
name,
2022-10-14 13:11:33 +00:00
tree: await markdown.parseMarkdown(text),
});
}