2022-03-20 09:22:38 +00:00
|
|
|
import { IndexEvent } from "../../webapp/app_event";
|
|
|
|
import { pageLinkRegex } from "../../webapp/constant";
|
2022-03-25 11:03:06 +00:00
|
|
|
import { syscall } from "../lib/syscall";
|
2022-02-28 13:35:51 +00:00
|
|
|
|
|
|
|
const wikilinkRegex = new RegExp(pageLinkRegex, "g");
|
|
|
|
|
|
|
|
export async function indexLinks({ name, text }: IndexEvent) {
|
|
|
|
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-02-28 13:35:51 +00:00
|
|
|
for (let match of text.matchAll(wikilinkRegex)) {
|
|
|
|
let toPage = match[1];
|
2022-03-28 13:25:05 +00:00
|
|
|
if (toPage.includes("@")) {
|
|
|
|
toPage = toPage.split("@")[0];
|
|
|
|
}
|
2022-02-28 13:35:51 +00:00
|
|
|
let pos = match.index!;
|
|
|
|
backLinks.push({
|
|
|
|
key: `pl:${toPage}:${pos}`,
|
|
|
|
value: name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
console.log("Found", backLinks.length, "wiki link(s)");
|
|
|
|
await syscall("indexer.batchSet", name, backLinks);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deletePage() {
|
2022-03-09 11:25:42 +00:00
|
|
|
let pageName = await syscall("editor.getCurrentPage");
|
2022-02-28 13:35:51 +00:00
|
|
|
console.log("Navigating to start page");
|
|
|
|
await syscall("editor.navigate", "start");
|
|
|
|
console.log("Deleting page from space");
|
2022-03-09 11:25:42 +00:00
|
|
|
await syscall("space.deletePage", pageName);
|
2022-02-28 13:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function renamePage() {
|
2022-03-09 11:25:42 +00:00
|
|
|
const oldName = await syscall("editor.getCurrentPage");
|
2022-03-03 09:35:32 +00:00
|
|
|
console.log("Old name is", oldName);
|
|
|
|
const newName = await syscall(
|
|
|
|
"editor.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);
|
|
|
|
|
|
|
|
let text = await syscall("editor.getText");
|
|
|
|
console.log("Writing new page to space");
|
|
|
|
await syscall("space.writePage", newName, text);
|
|
|
|
console.log("Navigating to new page");
|
|
|
|
await syscall("editor.navigate", newName);
|
2022-03-23 14:41:12 +00:00
|
|
|
console.log("Deleting page from space");
|
|
|
|
await syscall("space.deletePage", 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) {
|
|
|
|
console.log("Now going to update links in", pageToUpdate);
|
|
|
|
let { text } = await syscall("space.readPage", pageToUpdate);
|
2022-03-11 10:49:42 +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;
|
|
|
|
}
|
|
|
|
let newText = text.replaceAll(`[[${oldName}]]`, `[[${newName}]]`);
|
|
|
|
if (text !== newText) {
|
|
|
|
console.log("Changes made, saving...");
|
|
|
|
await syscall("space.writePage", pageToUpdate, newText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type BackLink = {
|
|
|
|
page: string;
|
|
|
|
pos: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function getBackLinks(pageName: string): Promise<BackLink[]> {
|
|
|
|
let allBackLinks = await syscall(
|
|
|
|
"indexer.scanPrefixGlobal",
|
|
|
|
`pl:${pageName}:`
|
|
|
|
);
|
|
|
|
let pagesToUpdate: BackLink[] = [];
|
|
|
|
for (let { key, value } of allBackLinks) {
|
|
|
|
let keyParts = key.split(":");
|
|
|
|
pagesToUpdate.push({
|
|
|
|
page: value,
|
|
|
|
pos: +keyParts[keyParts.length - 1],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return pagesToUpdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function showBackLinks() {
|
2022-03-09 11:25:42 +00:00
|
|
|
const pageName = await syscall("editor.getCurrentPage");
|
|
|
|
let backLinks = await getBackLinks(pageName);
|
2022-02-28 13:35:51 +00:00
|
|
|
|
|
|
|
console.log("Backlinks", backLinks);
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:25:05 +00:00
|
|
|
export async function reindexCommand() {
|
|
|
|
await syscall("editor.flashNotification", "Reindexing...");
|
|
|
|
await syscall("system.invokeFunctionOnServer", "reindexSpace");
|
|
|
|
await syscall("editor.flashNotification", "Reindexing done");
|
|
|
|
}
|
|
|
|
|
2022-03-29 10:13:46 +00:00
|
|
|
// Completion
|
|
|
|
export async function pageComplete() {
|
|
|
|
let prefix = await syscall("editor.matchBefore", "\\[\\[[\\w\\s]*");
|
|
|
|
if (!prefix) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let allPages = await syscall("space.listPages");
|
|
|
|
return {
|
|
|
|
from: prefix.from + 2,
|
|
|
|
options: allPages.map((pageMeta: any) => ({
|
|
|
|
label: pageMeta.name,
|
|
|
|
type: "page",
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:25:05 +00:00
|
|
|
// Server functions
|
|
|
|
export async function reindexSpace() {
|
|
|
|
console.log("Clearing page index...");
|
|
|
|
await syscall("indexer.clearPageIndex");
|
|
|
|
console.log("Listing all pages");
|
|
|
|
let pages = await syscall("space.listPages");
|
|
|
|
for (let { name } of pages) {
|
|
|
|
console.log("Indexing", name);
|
|
|
|
const pageObj = await syscall("space.readPage", name);
|
|
|
|
await syscall("event.dispatch", "page:index", {
|
|
|
|
name,
|
|
|
|
text: pageObj.text,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function clearPageIndex(page: string) {
|
|
|
|
console.log("Clearing page index for page", page);
|
|
|
|
await syscall("indexer.clearPageIndexForPage", page);
|
2022-02-28 13:35:51 +00:00
|
|
|
}
|