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,
},
});
}
}
}
-2
View File
@@ -14,8 +14,6 @@ functions:
commit:
path: "./git.ts:commit"
env: server
cron:
- "*/15 * * * *"
sync:
path: "./git.ts:sync"
env: server
+6
View File
@@ -0,0 +1,6 @@
functions:
mdTest:
path: "./markdown.ts:renderMarkdown"
env: client
command:
name: "Markdown: Render"
+16
View File
@@ -0,0 +1,16 @@
import MarkdownIt from "markdown-it";
import { syscall } from "../lib/syscall";
var taskLists = require("markdown-it-task-lists");
const md = new MarkdownIt({
linkify: true,
html: false,
typographer: true,
}).use(taskLists);
export async function renderMarkdown() {
let text = await syscall("editor.getText");
let html = md.render(text);
await syscall("editor.showRhs", `<html><body>${html}</body></html>`);
}
+12
View File
@@ -0,0 +1,12 @@
{
"name": "markdown",
"dependencies": {
"commonmark": "^0.30.0",
"markdown-it": "^12.3.2",
"markdown-it-task-lists": "^2.1.1"
},
"devDependencies": {
"@types/commonmark": "^0.27.5",
"@types/markdown-it": "^12.2.3"
}
}
+103
View File
@@ -0,0 +1,103 @@
import type { ClickEvent } from "../../webapp/app_event";
import { IndexEvent } from "../../webapp/app_event";
import { syscall } from "../lib/syscall";
const allTasksPageName = "ALL TASKS";
const taskRe = /[\-\*]\s*\[([ Xx])\]\s*(.*)/g;
const extractPageLink = /[\-\*]\s*\[[ Xx]\]\s\[\[([^\]]+)@(\d+)\]\]\s*(.*)/;
type Task = { task: string; complete: boolean; pos?: number };
export async function indexTasks({ name, text }: IndexEvent) {
if (name === allTasksPageName) {
return;
}
console.log("Indexing tasks");
let tasks: { key: string; value: Task }[] = [];
for (let match of text.matchAll(taskRe)) {
let complete = match[1] !== " ";
let task = match[2];
let pos = match.index!;
tasks.push({
key: `task:${pos}`,
value: {
task,
complete,
},
});
}
console.log("Found", tasks.length, "task(s)");
await syscall("indexer.batchSet", name, tasks);
}
export async function updateTaskPage() {
let allTasks = await syscall("indexer.scanPrefixGlobal", "task:");
let pageTasks = new Map<string, Task[]>();
for (let {
key,
page,
value: { task, complete, pos },
} of allTasks) {
if (complete) {
continue;
}
let [, pos] = key.split(":");
let tasks = pageTasks.get(page) || [];
tasks.push({ task, complete, pos });
pageTasks.set(page, tasks);
}
let mdPieces = [];
for (let pageName of [...pageTasks.keys()].sort()) {
mdPieces.push(`\n## ${pageName}\n`);
for (let task of pageTasks.get(pageName)!) {
mdPieces.push(
`* [${task.complete ? "x" : " "}] [[${pageName}@${task.pos}]] ${
task.task
}`
);
}
}
let taskMd = mdPieces.join("\n");
await syscall("space.writePage", allTasksPageName, taskMd);
}
export async function taskToggle(event: ClickEvent) {
let syntaxNode = await syscall("editor.getSyntaxNodeAtPos", event.pos);
if (syntaxNode && syntaxNode.name === "TaskMarker") {
let changeTo = "[x]";
if (syntaxNode.text === "[x]" || syntaxNode.text === "[X]") {
changeTo = "[ ]";
}
await syscall("editor.dispatch", {
changes: {
from: syntaxNode.from,
to: syntaxNode.to,
insert: changeTo,
},
selection: {
anchor: event.pos,
},
});
if (event.page === allTasksPageName) {
// Propagate back to the page in question
let line = (await syscall("editor.getLineUnderCursor")) as string;
let match = line.match(extractPageLink);
if (match) {
let [, page, posS] = match;
let pos = +posS;
let pageData = await syscall("space.readPage", page);
let text = pageData.text;
// Apply the toggle
text =
text.substring(0, pos) +
text.substring(pos).replace(/^([\-\*]\s*)\[[ xX]\]/, "$1" + changeTo);
await syscall("space.writePage", page, text);
}
}
}
}
+14
View File
@@ -0,0 +1,14 @@
functions:
indexTasks:
path: "./task.ts:indexTasks"
events:
- page:index
updateTaskPage:
path: "./task.ts:updateTaskPage"
command:
name: "Tasks: Update Page"
taskToggle:
path: "./task.ts:taskToggle"
events:
- page:click