Plugin stuff

This commit is contained in:
Zef Hemel
2022-03-28 15:25:05 +02:00
parent 16fa05d4cc
commit bf32d6d0bd
36 changed files with 523 additions and 219 deletions
+17 -5
View File
@@ -1,4 +1,10 @@
functions:
clearPageIndex:
path: "./page.ts:clearPageIndex"
env: server
events:
- page:saved
- page:deleted
indexLinks:
path: "./page.ts:indexLinks"
events:
@@ -7,6 +13,13 @@ functions:
path: "./page.ts:deletePage"
command:
name: "Page: Delete"
reindexSpaceCommand:
path: "./page.ts:reindexCommand"
command:
name: "Space: Reindex"
reindexSpace:
path: "./page.ts:reindexSpace"
env: server
showBackLinks:
path: "./page.ts:showBackLinks"
command:
@@ -29,10 +42,6 @@ functions:
path: "./navigate.ts:clickNavigate"
events:
- page:click
taskToggle:
path: "./task.ts:taskToggle"
events:
- page:click
insertToday:
path: "./dates.ts:insertToday"
command:
@@ -43,4 +52,7 @@ functions:
events:
- plug:load
env: server
# renderMD:
# path: "./markdown.ts:renderMD"
# command:
# name: Render Markdown
+23
View File
@@ -0,0 +1,23 @@
import { syscall } from "../lib/syscall";
import mdParser from "../../webapp/parser";
export async function renderMD() {
let text = await syscall("editor.getText");
let tree = mdParser.parser.parse(text);
let slicesToRemove: [number, number][] = [];
tree.iterate({
enter(type, from, to): false | void {
switch (type.name) {
case "Comment":
slicesToRemove.push([from, to]);
return false;
}
},
});
console.log("output peices", JSON.stringify(tree));
slicesToRemove.reverse().forEach(([from, to]) => {
text = text.slice(0, from) + text.slice(to);
});
console.log("Clean md", text);
}
+6 -1
View File
@@ -8,7 +8,12 @@ async function navigate(syntaxNode: any) {
console.log("Attempting to navigate based on syntax node", syntaxNode);
switch (syntaxNode.name) {
case "WikiLinkPage":
await syscall("editor.navigate", syntaxNode.text);
let pageLink = syntaxNode.text;
let pos = 0;
if (pageLink.includes("@")) {
[pageLink, pos] = syntaxNode.text.split("@");
}
await syscall("editor.navigate", pageLink, +pos);
break;
case "URL":
await syscall("editor.openUrl", syntaxNode.text);
+29 -4
View File
@@ -7,9 +7,12 @@ const wikilinkRegex = new RegExp(pageLinkRegex, "g");
export async function indexLinks({ name, text }: IndexEvent) {
let backLinks: { key: string; value: string }[] = [];
// [[Style Links]]
console.log("Now indexing", name);
for (let match of text.matchAll(wikilinkRegex)) {
let toPage = match[1];
if (toPage.includes("@")) {
toPage = toPage.split("@")[0];
}
let pos = match.index!;
backLinks.push({
key: `pl:${toPage}:${pos}`,
@@ -17,7 +20,6 @@ export async function indexLinks({ name, text }: IndexEvent) {
});
}
console.log("Found", backLinks.length, "wiki link(s)");
// throw Error("Boom");
await syscall("indexer.batchSet", name, backLinks);
}
@@ -102,6 +104,29 @@ export async function showBackLinks() {
console.log("Backlinks", backLinks);
}
export async function reindex() {
await syscall("space.reindex");
export async function reindexCommand() {
await syscall("editor.flashNotification", "Reindexing...");
await syscall("system.invokeFunctionOnServer", "reindexSpace");
await syscall("editor.flashNotification", "Reindexing done");
}
// 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);
}
-31
View File
@@ -1,31 +0,0 @@
import type { ClickEvent } from "../../webapp/app_event";
import { syscall } from "../lib/syscall";
export async function taskToggle(event: ClickEvent) {
let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos);
if (syntaxNode && syntaxNode.name === "TaskMarker") {
if (syntaxNode.text === "[x]" || syntaxNode.text === "[X]") {
await syscall("editor.dispatch", {
changes: {
from: syntaxNode.from,
to: syntaxNode.to,
insert: "[ ]",
},
selection: {
anchor: event.pos,
},
});
} else {
await syscall("editor.dispatch", {
changes: {
from: syntaxNode.from,
to: syntaxNode.to,
insert: "[x]",
},
selection: {
anchor: event.pos,
},
});
}
}
}