1
0
silverbullet/plugs/core/page.ts

238 lines
6.4 KiB
TypeScript
Raw Normal View History

2022-10-14 13:11:33 +00:00
import type {
IndexEvent,
IndexTreeEvent,
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,
system,
} from "$sb/silverbullet-syscall/mod.ts";
2022-10-15 17:02:56 +00:00
import { events } from "$sb/plugos-syscall/mod.ts";
import {
addParentPointers,
collectNodesMatching,
2022-04-11 18:34:09 +00:00
ParseTree,
renderToText,
2022-04-25 08:33:38 +00:00
replaceNodesMatching,
2022-10-14 13:11:33 +00:00
} from "$sb/lib/tree.ts";
import { applyQuery } from "$sb/lib/query.ts";
import { extractMeta } from "../query/data.ts";
// Key space:
// pl:toPage:pos => pageName
// meta => metaJson
2022-02-28 13:35:51 +00:00
export async function indexLinks({ name, tree }: IndexTreeEvent) {
2022-10-14 13:11:33 +00:00
const backLinks: { key: string; value: string }[] = [];
2022-03-14 09:07:38 +00:00
// [[Style Links]]
2022-03-28 13:25:05 +00:00
console.log("Now indexing", name);
2022-10-14 13:11:33 +00:00
const pageMeta = extractMeta(tree);
if (Object.keys(pageMeta).length > 0) {
2022-08-09 13:37:47 +00:00
console.log("Extracted page meta data", pageMeta);
// Don't index meta data starting with $
2022-10-14 13:11:33 +00:00
for (const key in pageMeta) {
if (key.startsWith("$")) {
delete pageMeta[key];
}
}
2022-10-14 13:11:33 +00:00
await index.set(name, "meta:", pageMeta);
}
collectNodesMatching(tree, (n) => n.type === "WikiLinkPage").forEach((n) => {
let toPage = n.children![0].text!;
if (toPage.includes("@")) {
toPage = toPage.split("@")[0];
2022-03-28 13:25:05 +00:00
}
backLinks.push({
key: `pl:${toPage}:${n.from}`,
value: name,
});
});
2022-02-28 13:35:51 +00:00
console.log("Found", backLinks.length, "wiki link(s)");
2022-10-14 13:11:33 +00:00
await index.batchSet(name, backLinks);
2022-02-28 13:35:51 +00:00
}
export async function pageQueryProvider({
query,
}: QueryProviderEvent): Promise<any[]> {
2022-10-14 13:11:33 +00:00
let allPages = await space.listPages();
const allPageMap: Map<string, any> = new Map(
2022-10-12 09:47:13 +00:00
allPages.map((pm) => [pm.name, pm]),
);
2022-10-14 13:11:33 +00:00
for (const { page, value } of await index.queryPrefix("meta:")) {
const p = allPageMap.get(page);
if (p) {
2022-10-15 17:02:56 +00:00
for (const [k, v] of Object.entries(value)) {
p[k] = v;
}
}
}
allPages = [...allPageMap.values()];
return applyQuery(query, allPages);
}
export async function linkQueryProvider({
query,
pageName,
}: QueryProviderEvent): Promise<any[]> {
2022-10-14 13:11:33 +00:00
const links: any[] = [];
for (
const { value: name, key } of await index.queryPrefix(`pl:${pageName}:`)
) {
2022-08-09 13:37:47 +00:00
const [, , pos] = key.split(":"); // Key: pl:page:pos
links.push({ name, pos });
}
2022-08-09 13:37:47 +00:00
return applyQuery(query, links);
}
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();
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
}
export async function renamePage() {
2022-10-14 13:11:33 +00:00
const oldName = await editor.getCurrentPage();
const cursor = await editor.getCursor();
2022-03-03 09:35:32 +00:00
console.log("Old name is", oldName);
2022-10-14 13:11:33 +00:00
const newName = await editor.prompt(`Rename ${oldName} to:`, oldName);
2022-02-28 13:35:51 +00:00
if (!newName) {
return;
}
if (newName.trim() === oldName.trim()) {
return;
}
2022-02-28 13:35:51 +00:00
console.log("New name", newName);
2022-10-14 13:11:33 +00:00
const pagesToUpdate = await getBackLinks(oldName);
2022-02-28 13:35:51 +00:00
console.log("All pages containing backlinks", pagesToUpdate);
2022-10-14 13:11:33 +00:00
const text = await editor.getText();
2022-02-28 13:35:51 +00:00
console.log("Writing new page to space");
2022-10-14 13:11:33 +00:00
await space.writePage(newName, text);
2022-02-28 13:35:51 +00:00
console.log("Navigating to new page");
2022-10-14 13:11:33 +00:00
await editor.navigate(newName, cursor, true);
2022-03-23 14:41:12 +00:00
console.log("Deleting page from space");
2022-10-14 13:11:33 +00:00
await space.deletePage(oldName);
2022-02-28 13:35:51 +00:00
2022-10-14 13:11:33 +00:00
const pageToUpdateSet = new Set<string>();
for (const pageToUpdate of pagesToUpdate) {
2022-02-28 13:35:51 +00:00
pageToUpdateSet.add(pageToUpdate.page);
}
2022-10-14 13:11:33 +00:00
for (const pageToUpdate of pageToUpdateSet) {
2022-04-10 09:04:07 +00:00
if (pageToUpdate === oldName) {
continue;
}
2022-02-28 13:35:51 +00:00
console.log("Now going to update links in", pageToUpdate);
2022-10-14 13:11:33 +00:00
const text = await space.readPage(pageToUpdate);
// console.log("Received text", text);
2022-02-28 13:35:51 +00:00
if (!text) {
// Page likely does not exist, but at least we can skip it
continue;
}
2022-10-14 13:11:33 +00:00
const mdTree = await markdown.parseMarkdown(text);
addParentPointers(mdTree);
2022-04-11 18:34:09 +00:00
replaceNodesMatching(mdTree, (n): ParseTree | undefined | null => {
if (n.type === "WikiLinkPage") {
2022-10-14 13:11:33 +00:00
const pageName = n.children![0].text!;
if (pageName === oldName) {
n.children![0].text = newName;
return n;
}
// page name with @pos position
if (pageName.startsWith(`${oldName}@`)) {
2022-10-14 13:11:33 +00:00
const [, pos] = pageName.split("@");
n.children![0].text = `${newName}@${pos}`;
return n;
}
}
return;
});
// let newText = text.replaceAll(`[[${oldName}]]`, `[[${newName}]]`);
2022-10-14 13:11:33 +00:00
const newText = renderToText(mdTree);
2022-02-28 13:35:51 +00:00
if (text !== newText) {
console.log("Changes made, saving...");
2022-10-14 13:11:33 +00:00
await space.writePage(pageToUpdate, newText);
2022-02-28 13:35:51 +00:00
}
}
}
type BackLink = {
page: string;
pos: number;
};
async function getBackLinks(pageName: string): Promise<BackLink[]> {
2022-10-14 13:11:33 +00:00
const allBackLinks = await index.queryPrefix(`pl:${pageName}:`);
const pagesToUpdate: BackLink[] = [];
for (const { key, value } of allBackLinks) {
const keyParts = key.split(":");
2022-02-28 13:35:51 +00:00
pagesToUpdate.push({
page: value,
pos: +keyParts[keyParts.length - 1],
});
}
return pagesToUpdate;
}
2022-03-28 13:25:05 +00:00
export async function reindexCommand() {
2022-10-14 13:11:33 +00:00
await editor.flashNotification("Reindexing...");
await system.invokeFunction("server", "reindexSpace");
await editor.flashNotification("Reindexing done");
2022-03-28 13:25:05 +00:00
}
2022-03-29 10:13:46 +00:00
// Completion
export async function pageComplete() {
2022-10-14 13:11:33 +00:00
const prefix = await editor.matchBefore("\\[\\[[^\\]@:]*");
2022-03-29 10:13:46 +00:00
if (!prefix) {
return null;
}
2022-10-14 13:11:33 +00:00
const allPages = await space.listPages();
2022-03-29 10:13:46 +00:00
return {
from: prefix.from + 2,
2022-04-01 15:07:08 +00:00
options: allPages.map((pageMeta) => ({
2022-03-29 10:13:46 +00:00
label: pageMeta.name,
type: "page",
})),
};
}
2022-03-28 13:25:05 +00:00
// Server functions
export async function reindexSpace() {
console.log("Clearing page index...");
2022-10-14 13:11:33 +00:00
await index.clearPageIndex();
2022-03-28 13:25:05 +00:00
console.log("Listing all pages");
2022-10-14 13:11:33 +00:00
const pages = await space.listPages();
for (const { name } of pages) {
2022-03-28 13:25:05 +00:00
console.log("Indexing", name);
2022-10-14 13:11:33 +00:00
const text = await space.readPage(name);
const parsed = await markdown.parseMarkdown(text);
await events.dispatchEvent("page:index", {
2022-03-28 13:25:05 +00:00
name,
tree: parsed,
2022-03-28 13:25:05 +00:00
});
}
console.log("Indexing completed!");
2022-03-28 13:25:05 +00:00
}
export async function clearPageIndex(page: string) {
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) {
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),
});
}