2022-10-14 13:11:33 +00:00
|
|
|
import type {
|
2022-12-21 13:55:24 +00:00
|
|
|
CompleteEvent,
|
2022-10-14 13:11:33 +00:00
|
|
|
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,
|
|
|
|
} from "$sb/silverbullet-syscall/mod.ts";
|
|
|
|
|
2022-10-15 17:02:56 +00:00
|
|
|
import { events } from "$sb/plugos-syscall/mod.ts";
|
2022-07-12 10:30:36 +00:00
|
|
|
|
2022-04-08 15:46:09 +00:00
|
|
|
import {
|
|
|
|
addParentPointers,
|
2023-07-14 09:52:35 +00:00
|
|
|
findNodeOfType,
|
2022-04-11 18:34:09 +00:00
|
|
|
ParseTree,
|
|
|
|
renderToText,
|
2022-04-25 08:33:38 +00:00
|
|
|
replaceNodesMatching,
|
2023-07-14 09:52:35 +00:00
|
|
|
traverseTree,
|
2022-10-14 13:11:33 +00:00
|
|
|
} from "$sb/lib/tree.ts";
|
2023-07-14 09:52:35 +00:00
|
|
|
import { applyQuery } from "$sb/lib/query.ts";
|
2022-11-24 11:04:00 +00:00
|
|
|
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
|
2023-07-02 09:25:32 +00:00
|
|
|
import { invokeFunction } from "$sb/silverbullet-syscall/system.ts";
|
2023-07-06 14:47:50 +00:00
|
|
|
import { isValidPageName } from "$sb/lib/page.ts";
|
2023-07-24 17:54:31 +00:00
|
|
|
import { extractAttributes } from "$sb/lib/attribute.ts";
|
2022-04-21 09:46:33 +00:00
|
|
|
|
|
|
|
// Key space:
|
2023-07-14 09:52:35 +00:00
|
|
|
// l:toPage:pos => {name: pageName, inDirective: true}
|
|
|
|
// meta: => metaJson
|
2022-02-28 13:35:51 +00:00
|
|
|
|
2023-07-14 09:52:35 +00:00
|
|
|
const backlinkPrefix = `l:`;
|
|
|
|
|
2023-07-24 09:09:17 +00:00
|
|
|
export type BacklinkEntry = {
|
2023-07-14 09:52:35 +00:00
|
|
|
name: string;
|
|
|
|
alias?: string;
|
|
|
|
inDirective?: boolean;
|
|
|
|
};
|
2022-04-20 08:56:43 +00:00
|
|
|
export async function indexLinks({ name, tree }: IndexTreeEvent) {
|
2023-07-14 09:52:35 +00:00
|
|
|
const backLinks: { key: string; value: BacklinkEntry }[] = [];
|
2022-03-14 09:07:38 +00:00
|
|
|
// [[Style Links]]
|
2023-05-23 18:53:53 +00:00
|
|
|
// console.log("Now indexing links for", name);
|
|
|
|
const pageMeta = await extractFrontmatter(tree);
|
2023-07-26 15:12:56 +00:00
|
|
|
const toplevelAttributes = await extractAttributes(tree, false);
|
2023-07-24 17:54:31 +00:00
|
|
|
if (
|
|
|
|
Object.keys(pageMeta).length > 0 ||
|
|
|
|
Object.keys(toplevelAttributes).length > 0
|
|
|
|
) {
|
|
|
|
for (const [k, v] of Object.entries(toplevelAttributes)) {
|
|
|
|
pageMeta[k] = v;
|
|
|
|
}
|
2022-08-10 07:26:07 +00:00
|
|
|
// Don't index meta data starting with $
|
2022-10-14 13:11:33 +00:00
|
|
|
for (const key in pageMeta) {
|
2022-08-10 07:26:07 +00:00
|
|
|
if (key.startsWith("$")) {
|
|
|
|
delete pageMeta[key];
|
|
|
|
}
|
|
|
|
}
|
2023-07-25 15:33:07 +00:00
|
|
|
// console.log("Extracted page meta data", pageMeta);
|
2022-10-14 13:11:33 +00:00
|
|
|
await index.set(name, "meta:", pageMeta);
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 09:52:35 +00:00
|
|
|
let directiveDepth = 0;
|
|
|
|
traverseTree(tree, (n): boolean => {
|
|
|
|
if (n.type === "DirectiveStart") {
|
|
|
|
directiveDepth++;
|
|
|
|
return true;
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
2023-07-14 09:52:35 +00:00
|
|
|
if (n.type === "DirectiveStop") {
|
|
|
|
directiveDepth--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n.type === "WikiLink") {
|
|
|
|
const wikiLinkPage = findNodeOfType(n, "WikiLinkPage")!;
|
|
|
|
const wikiLinkAlias = findNodeOfType(n, "WikiLinkAlias");
|
|
|
|
let toPage = wikiLinkPage.children![0].text!;
|
|
|
|
if (toPage.includes("@")) {
|
|
|
|
toPage = toPage.split("@")[0];
|
|
|
|
}
|
|
|
|
const blEntry: BacklinkEntry = { name };
|
|
|
|
if (directiveDepth > 0) {
|
|
|
|
blEntry.inDirective = true;
|
|
|
|
}
|
|
|
|
if (wikiLinkAlias) {
|
|
|
|
blEntry.alias = wikiLinkAlias.children![0].text!;
|
|
|
|
}
|
|
|
|
backLinks.push({
|
|
|
|
key: `${backlinkPrefix}${toPage}:${wikiLinkPage.from}`,
|
|
|
|
value: blEntry,
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2022-04-20 08:56:43 +00:00
|
|
|
});
|
2022-10-21 08:00:43 +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
|
|
|
}
|
|
|
|
|
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-11-20 09:24:42 +00:00
|
|
|
return applyQuery(query, await space.listPages());
|
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-10-14 13:11:33 +00:00
|
|
|
const links: any[] = [];
|
|
|
|
for (
|
2023-07-14 09:52:35 +00:00
|
|
|
const { value: blEntry, key } of await index.queryPrefix(
|
|
|
|
`${backlinkPrefix}${pageName}:`,
|
|
|
|
)
|
2022-10-14 13:11:33 +00:00
|
|
|
) {
|
2023-07-14 09:52:35 +00:00
|
|
|
const [, , pos] = key.split(":"); // Key: l:page:pos
|
|
|
|
if (!blEntry.inDirective) {
|
|
|
|
blEntry.inDirective = false;
|
|
|
|
}
|
|
|
|
links.push({ ...blEntry, pos });
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|
2022-08-09 13:37:47 +00:00
|
|
|
return applyQuery(query, links);
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|
|
|
|
|
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-11-18 15:04:37 +00:00
|
|
|
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(
|
2023-07-02 09:25:32 +00:00
|
|
|
`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);
|
|
|
|
}
|
|
|
|
|
2022-12-13 08:49:31 +00:00
|
|
|
export async function renamePage(cmdDef: any) {
|
|
|
|
console.log("Got a target name", cmdDef.page);
|
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-12-13 08:49:31 +00:00
|
|
|
const newName = cmdDef.page ||
|
2022-11-18 15:04:37 +00:00
|
|
|
await editor.prompt(`Rename ${oldName} to:`, oldName);
|
2022-02-28 13:35:51 +00:00
|
|
|
if (!newName) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-26 10:49:04 +00:00
|
|
|
|
2023-07-06 14:47:50 +00:00
|
|
|
if (!isValidPageName(newName)) {
|
|
|
|
return editor.flashNotification(
|
2023-07-07 09:44:05 +00:00
|
|
|
"Invalid page name: page names cannot end with a file extension nor start with a '.'",
|
2023-07-06 14:47:50 +00:00
|
|
|
"error",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-21 13:55:24 +00:00
|
|
|
console.log("New name", newName);
|
|
|
|
|
2022-07-26 10:49:04 +00:00
|
|
|
if (newName.trim() === oldName.trim()) {
|
2022-12-21 13:55:24 +00:00
|
|
|
// Nothing to do here
|
|
|
|
console.log("Name unchanged, exiting");
|
2022-07-26 10:49:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-21 13:55:24 +00:00
|
|
|
|
|
|
|
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.`,
|
|
|
|
);
|
|
|
|
} catch (e: any) {
|
2023-05-23 18:53:53 +00:00
|
|
|
if (e.message === "Not found") {
|
2022-12-21 13:55:24 +00:00
|
|
|
// Expected not found error, so we can continue
|
|
|
|
} else {
|
|
|
|
await editor.flashNotification(e.message, "error");
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2022-02-28 13:35:51 +00:00
|
|
|
|
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();
|
2023-07-02 09:25:32 +00:00
|
|
|
|
2022-02-28 13:35:51 +00:00
|
|
|
console.log("Writing new page to space");
|
2022-11-18 15:04:37 +00:00
|
|
|
const newPageMeta = 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-11-18 15:04:37 +00:00
|
|
|
|
|
|
|
// Handling the edge case of a changing page name just in casing on a case insensitive FS
|
|
|
|
const oldPageMeta = await space.getPageMeta(oldName);
|
|
|
|
if (oldPageMeta.lastModified !== newPageMeta.lastModified) {
|
|
|
|
// If they're the same, let's assume it's the same file (case insensitive FS) and not delete, otherwise...
|
|
|
|
console.log("Deleting page from space");
|
|
|
|
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-11-18 15:04:37 +00:00
|
|
|
let updatedReferences = 0;
|
|
|
|
|
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);
|
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-10-14 13:11:33 +00:00
|
|
|
const mdTree = await markdown.parseMarkdown(text);
|
2022-04-08 15:46:09 +00:00
|
|
|
addParentPointers(mdTree);
|
2023-07-02 09:25:32 +00:00
|
|
|
// The links in the page are going to be relative pointers to the old name
|
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") {
|
2022-10-14 13:11:33 +00:00
|
|
|
const pageName = n.children![0].text!;
|
2022-04-08 15:46:09 +00:00
|
|
|
if (pageName === oldName) {
|
|
|
|
n.children![0].text = newName;
|
2022-11-18 15:04:37 +00:00
|
|
|
updatedReferences++;
|
2022-04-08 15:46:09 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
// page name with @pos position
|
|
|
|
if (pageName.startsWith(`${oldName}@`)) {
|
2022-10-14 13:11:33 +00:00
|
|
|
const [, pos] = pageName.split("@");
|
2022-04-08 15:46:09 +00:00
|
|
|
n.children![0].text = `${newName}@${pos}`;
|
2022-11-18 15:04:37 +00:00
|
|
|
updatedReferences++;
|
2022-04-08 15:46:09 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-11-18 15:04:37 +00:00
|
|
|
await editor.flashNotification(
|
|
|
|
`Renamed page, and updated ${updatedReferences} references`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
type BackLink = {
|
|
|
|
page: string;
|
|
|
|
pos: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function getBackLinks(pageName: string): Promise<BackLink[]> {
|
2023-07-14 09:52:35 +00:00
|
|
|
const allBackLinks = await index.queryPrefix(
|
|
|
|
`${backlinkPrefix}${pageName}:`,
|
|
|
|
);
|
2022-10-14 13:11:33 +00:00
|
|
|
const pagesToUpdate: BackLink[] = [];
|
2023-07-14 09:52:35 +00:00
|
|
|
for (const { key, value: { name } } of allBackLinks) {
|
2022-10-14 13:11:33 +00:00
|
|
|
const keyParts = key.split(":");
|
2022-02-28 13:35:51 +00:00
|
|
|
pagesToUpdate.push({
|
2023-07-14 09:52:35 +00:00
|
|
|
page: name,
|
2022-02-28 13:35:51 +00:00
|
|
|
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...");
|
2023-05-23 18:53:53 +00:00
|
|
|
await reindexSpace();
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.flashNotification("Reindexing done");
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 10:13:46 +00:00
|
|
|
// Completion
|
2022-12-21 13:55:24 +00:00
|
|
|
export async function pageComplete(completeEvent: CompleteEvent) {
|
2023-07-02 09:25:32 +00:00
|
|
|
const match = /\[\[([^\]@:\{}]*)$/.exec(completeEvent.linePrefix);
|
2022-12-21 13:55:24 +00:00
|
|
|
if (!match) {
|
2022-03-29 10:13:46 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-10-14 13:11:33 +00:00
|
|
|
const allPages = await space.listPages();
|
2022-03-29 10:13:46 +00:00
|
|
|
return {
|
2022-12-21 13:55:24 +00:00
|
|
|
from: completeEvent.pos - match[1].length,
|
2023-07-02 09:25:32 +00:00
|
|
|
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();
|
2023-05-23 18:53:53 +00:00
|
|
|
// Executed this way to not have to embed the search plug code here
|
|
|
|
await invokeFunction("client", "search.clearIndex");
|
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();
|
2022-10-21 08:00:43 +00:00
|
|
|
let counter = 0;
|
2022-10-14 13:11:33 +00:00
|
|
|
for (const { name } of pages) {
|
2022-10-21 08:00:43 +00:00
|
|
|
counter++;
|
|
|
|
|
|
|
|
console.log(`Indexing page ${counter}/${pages.length}: ${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,
|
2022-04-20 08:56:43 +00:00
|
|
|
tree: parsed,
|
2022-03-28 13:25:05 +00:00
|
|
|
});
|
|
|
|
}
|
2022-09-14 07:32:47 +00:00
|
|
|
console.log("Indexing completed!");
|
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
|
|
|
|
2022-04-20 08:56:43 +00:00
|
|
|
export async function parseIndexTextRepublish({ name, text }: IndexEvent) {
|
2023-05-23 18:53:53 +00:00
|
|
|
// console.log("Reindexing", name);
|
2022-10-14 13:11:33 +00:00
|
|
|
await events.dispatchEvent("page:index", {
|
2022-04-20 08:56:43 +00:00
|
|
|
name,
|
2022-10-14 13:11:33 +00:00
|
|
|
tree: await markdown.parseMarkdown(text),
|
2022-04-20 08:56:43 +00:00
|
|
|
});
|
|
|
|
}
|