2022-04-21 11:57:45 +00:00
|
|
|
import type { IndexEvent, IndexTreeEvent } from "@silverbulletmd/web/app_event";
|
2022-04-01 15:07:08 +00:00
|
|
|
import {
|
|
|
|
batchSet,
|
|
|
|
clearPageIndex as clearPageIndexSyscall,
|
|
|
|
clearPageIndexForPage,
|
2022-05-17 13:54:55 +00:00
|
|
|
queryPrefix,
|
2022-04-25 08:33:38 +00:00
|
|
|
set,
|
2022-04-25 09:24:13 +00:00
|
|
|
} from "@silverbulletmd/plugos-silverbullet-syscall/index";
|
2022-07-12 10:30:36 +00:00
|
|
|
|
|
|
|
import { set as storeSet } from "@plugos/plugos-syscall/store";
|
|
|
|
|
2022-04-08 15:46:09 +00:00
|
|
|
import {
|
|
|
|
flashNotification,
|
|
|
|
getCurrentPage,
|
2022-05-06 16:55:04 +00:00
|
|
|
getCursor,
|
2022-04-08 15:46:09 +00:00
|
|
|
getText,
|
2022-05-06 16:55:04 +00:00
|
|
|
insertAtCursor,
|
2022-04-08 15:46:09 +00:00
|
|
|
matchBefore,
|
2022-05-06 16:55:04 +00:00
|
|
|
moveCursor,
|
2022-04-08 15:46:09 +00:00
|
|
|
navigate,
|
2022-04-25 08:33:38 +00:00
|
|
|
prompt,
|
2022-04-25 09:24:13 +00:00
|
|
|
} from "@silverbulletmd/plugos-silverbullet-syscall/editor";
|
2022-04-01 15:07:08 +00:00
|
|
|
|
2022-04-25 08:33:38 +00:00
|
|
|
import { dispatch } from "@plugos/plugos-syscall/event";
|
2022-04-21 11:57:45 +00:00
|
|
|
import {
|
|
|
|
deletePage as deletePageSyscall,
|
|
|
|
listPages,
|
|
|
|
readPage,
|
2022-04-25 08:33:38 +00:00
|
|
|
writePage,
|
2022-04-25 09:24:13 +00:00
|
|
|
} from "@silverbulletmd/plugos-silverbullet-syscall/space";
|
|
|
|
import { invokeFunction } from "@silverbulletmd/plugos-silverbullet-syscall/system";
|
|
|
|
import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown";
|
2022-04-08 15:46:09 +00:00
|
|
|
import {
|
|
|
|
addParentPointers,
|
|
|
|
collectNodesMatching,
|
2022-04-11 18:34:09 +00:00
|
|
|
ParseTree,
|
|
|
|
renderToText,
|
2022-04-25 08:33:38 +00:00
|
|
|
replaceNodesMatching,
|
2022-04-21 11:57:45 +00:00
|
|
|
} from "@silverbulletmd/common/tree";
|
2022-04-19 14:54:47 +00:00
|
|
|
import { applyQuery, QueryProviderEvent } from "../query/engine";
|
2022-04-21 09:46:33 +00:00
|
|
|
import { extractMeta } from "../query/data";
|
|
|
|
|
|
|
|
// Key space:
|
|
|
|
// pl:toPage:pos => pageName
|
|
|
|
// meta => metaJson
|
2022-02-28 13:35:51 +00:00
|
|
|
|
2022-04-20 08:56:43 +00:00
|
|
|
export async function indexLinks({ name, tree }: IndexTreeEvent) {
|
2022-02-28 13:35:51 +00:00
|
|
|
let 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-04-21 09:46:33 +00:00
|
|
|
let pageMeta = extractMeta(tree);
|
|
|
|
if (Object.keys(pageMeta).length > 0) {
|
|
|
|
await set(name, "meta:", pageMeta);
|
|
|
|
}
|
|
|
|
|
2022-04-20 08:56:43 +00:00
|
|
|
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
|
|
|
}
|
2022-04-20 08:56:43 +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-04-01 15:07:08 +00:00
|
|
|
await batchSet(name, backLinks);
|
2022-02-28 13:35:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
export async function pageQueryProvider({
|
|
|
|
query,
|
2022-04-28 09:55:38 +00:00
|
|
|
}: QueryProviderEvent): Promise<any[]> {
|
2022-04-19 14:54:47 +00:00
|
|
|
let allPages = await listPages();
|
2022-04-28 09:55:38 +00:00
|
|
|
let allPageMap: Map<string, any> = new Map(
|
|
|
|
allPages.map((pm) => [pm.name, pm])
|
|
|
|
);
|
2022-05-17 13:54:55 +00:00
|
|
|
for (let { page, value } of await queryPrefix("meta:")) {
|
2022-04-28 09:55:38 +00:00
|
|
|
let p = allPageMap.get(page);
|
|
|
|
if (p) {
|
|
|
|
for (let [k, v] of Object.entries(value)) {
|
|
|
|
p[k] = v;
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-28 09:55:38 +00:00
|
|
|
allPages = [...allPageMap.values()];
|
|
|
|
return applyQuery(query, allPages);
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function linkQueryProvider({
|
|
|
|
query,
|
|
|
|
pageName,
|
2022-04-28 09:55:38 +00:00
|
|
|
}: QueryProviderEvent): Promise<any[]> {
|
2022-04-19 14:54:47 +00:00
|
|
|
let uniqueLinks = new Set<string>();
|
2022-05-17 13:54:55 +00:00
|
|
|
for (let { value: name } of await queryPrefix(`pl:${pageName}:`)) {
|
2022-04-19 14:54:47 +00:00
|
|
|
uniqueLinks.add(name);
|
|
|
|
}
|
2022-04-28 09:55:38 +00:00
|
|
|
return applyQuery(
|
2022-04-19 14:54:47 +00:00
|
|
|
query,
|
|
|
|
[...uniqueLinks].map((l) => ({ name: l }))
|
2022-04-28 09:55:38 +00:00
|
|
|
);
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 13:35:51 +00:00
|
|
|
export async function deletePage() {
|
2022-04-01 15:07:08 +00:00
|
|
|
let pageName = await getCurrentPage();
|
2022-06-28 12:14:15 +00:00
|
|
|
console.log("Navigating to index page");
|
|
|
|
await navigate("index");
|
2022-02-28 13:35:51 +00:00
|
|
|
console.log("Deleting page from space");
|
2022-04-01 15:07:08 +00:00
|
|
|
await deletePageSyscall(pageName);
|
2022-02-28 13:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function renamePage() {
|
2022-04-01 15:07:08 +00:00
|
|
|
const oldName = await getCurrentPage();
|
2022-03-03 09:35:32 +00:00
|
|
|
console.log("Old name is", oldName);
|
2022-04-01 15:07:08 +00:00
|
|
|
const newName = await prompt(`Rename ${oldName} to:`, oldName);
|
2022-02-28 13:35:51 +00:00
|
|
|
if (!newName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log("New name", newName);
|
|
|
|
|
|
|
|
let pagesToUpdate = await getBackLinks(oldName);
|
|
|
|
console.log("All pages containing backlinks", pagesToUpdate);
|
|
|
|
|
2022-04-01 15:07:08 +00:00
|
|
|
let text = await getText();
|
2022-02-28 13:35:51 +00:00
|
|
|
console.log("Writing new page to space");
|
2022-04-01 15:07:08 +00:00
|
|
|
await writePage(newName, text);
|
2022-02-28 13:35:51 +00:00
|
|
|
console.log("Navigating to new page");
|
2022-04-01 15:07:08 +00:00
|
|
|
await navigate(newName);
|
2022-03-23 14:41:12 +00:00
|
|
|
console.log("Deleting page from space");
|
2022-04-01 15:07:08 +00:00
|
|
|
await deletePageSyscall(oldName);
|
2022-02-28 13:35:51 +00:00
|
|
|
|
|
|
|
let pageToUpdateSet = new Set<string>();
|
|
|
|
for (let pageToUpdate of pagesToUpdate) {
|
|
|
|
pageToUpdateSet.add(pageToUpdate.page);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let 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-04-01 15:07:08 +00:00
|
|
|
let { text } = await readPage(pageToUpdate);
|
2022-04-08 15:46:09 +00:00
|
|
|
// 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-04-08 15:46:09 +00:00
|
|
|
let mdTree = await parseMarkdown(text);
|
|
|
|
addParentPointers(mdTree);
|
2022-04-11 18:34:09 +00:00
|
|
|
replaceNodesMatching(mdTree, (n): ParseTree | undefined | null => {
|
2022-04-08 15:46:09 +00:00
|
|
|
if (n.type === "WikiLinkPage") {
|
|
|
|
let pageName = n.children![0].text!;
|
|
|
|
if (pageName === oldName) {
|
|
|
|
n.children![0].text = newName;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
// page name with @pos position
|
|
|
|
if (pageName.startsWith(`${oldName}@`)) {
|
|
|
|
let [, pos] = pageName.split("@");
|
|
|
|
n.children![0].text = `${newName}@${pos}`;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
// let newText = text.replaceAll(`[[${oldName}]]`, `[[${newName}]]`);
|
2022-04-11 18:34:09 +00:00
|
|
|
let newText = renderToText(mdTree);
|
2022-02-28 13:35:51 +00:00
|
|
|
if (text !== newText) {
|
|
|
|
console.log("Changes made, saving...");
|
2022-04-01 15:07:08 +00:00
|
|
|
await 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-05-17 13:54:55 +00:00
|
|
|
let allBackLinks = await queryPrefix(`pl:${pageName}:`);
|
2022-02-28 13:35:51 +00:00
|
|
|
let pagesToUpdate: BackLink[] = [];
|
|
|
|
for (let { key, value } of allBackLinks) {
|
|
|
|
let keyParts = key.split(":");
|
|
|
|
pagesToUpdate.push({
|
|
|
|
page: value,
|
|
|
|
pos: +keyParts[keyParts.length - 1],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return pagesToUpdate;
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:25:05 +00:00
|
|
|
export async function reindexCommand() {
|
2022-04-01 15:07:08 +00:00
|
|
|
await flashNotification("Reindexing...");
|
2022-04-05 15:02:17 +00:00
|
|
|
await invokeFunction("server", "reindexSpace");
|
2022-07-12 10:30:36 +00:00
|
|
|
await storeSet("$spaceIndexed", true);
|
2022-04-01 15:07:08 +00:00
|
|
|
await 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-04-01 15:07:08 +00:00
|
|
|
let prefix = await matchBefore("\\[\\[[\\w\\s]*");
|
2022-03-29 10:13:46 +00:00
|
|
|
if (!prefix) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-01 15:07:08 +00:00
|
|
|
let allPages = await 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-04-01 15:07:08 +00:00
|
|
|
await clearPageIndexSyscall();
|
2022-03-28 13:25:05 +00:00
|
|
|
console.log("Listing all pages");
|
2022-04-01 15:07:08 +00:00
|
|
|
let pages = await listPages();
|
2022-03-28 13:25:05 +00:00
|
|
|
for (let { name } of pages) {
|
|
|
|
console.log("Indexing", name);
|
2022-04-20 08:56:43 +00:00
|
|
|
const { text } = await readPage(name);
|
|
|
|
let parsed = await parseMarkdown(text);
|
2022-04-01 15:07:08 +00:00
|
|
|
await dispatch("page:index", {
|
2022-03-28 13:25:05 +00:00
|
|
|
name,
|
2022-04-20 08:56:43 +00:00
|
|
|
tree: parsed,
|
2022-03-28 13:25:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function clearPageIndex(page: string) {
|
|
|
|
console.log("Clearing page index for page", page);
|
2022-04-01 15:07:08 +00:00
|
|
|
await clearPageIndexForPage(page);
|
2022-02-28 13:35:51 +00:00
|
|
|
}
|
2022-04-09 12:28:41 +00:00
|
|
|
|
2022-04-20 08:56:43 +00:00
|
|
|
export async function parseIndexTextRepublish({ name, text }: IndexEvent) {
|
|
|
|
await dispatch("page:index", {
|
|
|
|
name,
|
|
|
|
tree: await parseMarkdown(text),
|
|
|
|
});
|
|
|
|
}
|