Rewrote all plugs using MarkdownTree
parent
07453d638b
commit
16577c8ea2
|
@ -1,16 +1,10 @@
|
||||||
import {SysCallMapping} from "../../plugos/system";
|
import {SysCallMapping} from "../../plugos/system";
|
||||||
import {MarkdownTree, nodeAtPos, parse, render} from "../tree";
|
import {MarkdownTree, parse} from "../tree";
|
||||||
|
|
||||||
export function markdownSyscalls(): SysCallMapping {
|
export function markdownSyscalls(): SysCallMapping {
|
||||||
return {
|
return {
|
||||||
"markdown.parse": (ctx, text: string): MarkdownTree => {
|
"markdown.parseMarkdown": (ctx, text: string): MarkdownTree => {
|
||||||
return parse(text);
|
return parse(text);
|
||||||
},
|
},
|
||||||
"markdown.nodeAtPos": (ctx, mdTree: MarkdownTree, pos: number): MarkdownTree | null => {
|
};
|
||||||
return nodeAtPos(mdTree, pos);
|
|
||||||
},
|
|
||||||
"markdown.render": (ctx, mdTree: MarkdownTree): string => {
|
|
||||||
return render(mdTree);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
import {expect, test} from "@jest/globals";
|
|
||||||
import {nodeAtPos, parse, render} from "./tree";
|
|
||||||
|
|
||||||
const mdTest1 = `
|
|
||||||
# Heading
|
|
||||||
## Sub _heading_ cool
|
|
||||||
|
|
||||||
Hello, this is some **bold** text and *italic*. And [a link](http://zef.me).
|
|
||||||
|
|
||||||
- This is a list
|
|
||||||
- With another item
|
|
||||||
- TODOs:
|
|
||||||
- [ ] A task that's not yet done
|
|
||||||
- [x] Hello
|
|
||||||
- And a _third_ one [[Wiki Page]] yo
|
|
||||||
`;
|
|
||||||
|
|
||||||
test("Run a Node sandbox", async () => {
|
|
||||||
let mdTree = parse(mdTest1);
|
|
||||||
console.log(JSON.stringify(mdTree, null, 2));
|
|
||||||
expect(nodeAtPos(mdTree, 4)!.type).toBe("ATXHeading1");
|
|
||||||
expect(nodeAtPos(mdTree, mdTest1.indexOf("Wiki Page"))!.type).toBe(
|
|
||||||
"WikiLink"
|
|
||||||
);
|
|
||||||
expect(render(mdTree)).toBe(mdTest1);
|
|
||||||
});
|
|
148
common/tree.ts
148
common/tree.ts
|
@ -2,114 +2,66 @@ import {SyntaxNode} from "@lezer/common";
|
||||||
import wikiMarkdownLang from "../webapp/parser";
|
import wikiMarkdownLang from "../webapp/parser";
|
||||||
|
|
||||||
export type MarkdownTree = {
|
export type MarkdownTree = {
|
||||||
type?: string; // undefined === text node
|
type?: string; // undefined === text node
|
||||||
from: number;
|
from: number;
|
||||||
to: number;
|
to: number;
|
||||||
text?: string;
|
text?: string;
|
||||||
children?: MarkdownTree[];
|
children?: MarkdownTree[];
|
||||||
parent?: MarkdownTree;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function treeToAST(text: string, n: SyntaxNode): MarkdownTree {
|
function treeToAST(text: string, n: SyntaxNode): MarkdownTree {
|
||||||
let children: MarkdownTree[] = [];
|
let children: MarkdownTree[] = [];
|
||||||
let nodeText: string | undefined;
|
let nodeText: string | undefined;
|
||||||
let child = n.firstChild;
|
let child = n.firstChild;
|
||||||
while (child) {
|
while (child) {
|
||||||
children.push(treeToAST(text, child));
|
children.push(treeToAST(text, child));
|
||||||
child = child.nextSibling;
|
child = child.nextSibling;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (children.length === 0) {
|
if (children.length === 0) {
|
||||||
children = [
|
children = [
|
||||||
{
|
{
|
||||||
from: n.from,
|
|
||||||
to: n.to,
|
|
||||||
text: text.substring(n.from, n.to),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
} else {
|
|
||||||
let newChildren: MarkdownTree[] | string = [];
|
|
||||||
let index = n.from;
|
|
||||||
for (let child of children) {
|
|
||||||
let s = text.substring(index, child.from);
|
|
||||||
if (s) {
|
|
||||||
newChildren.push({
|
|
||||||
from: index,
|
|
||||||
to: child.from,
|
|
||||||
text: s,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
newChildren.push(child);
|
|
||||||
index = child.to;
|
|
||||||
}
|
|
||||||
let s = text.substring(index, n.to);
|
|
||||||
if (s) {
|
|
||||||
newChildren.push({ from: index, to: n.to, text: s });
|
|
||||||
}
|
|
||||||
children = newChildren;
|
|
||||||
}
|
|
||||||
|
|
||||||
let result: MarkdownTree = {
|
|
||||||
type: n.name,
|
|
||||||
from: n.from,
|
from: n.from,
|
||||||
to: n.to,
|
to: n.to,
|
||||||
};
|
text: text.substring(n.from, n.to),
|
||||||
if (children.length > 0) {
|
},
|
||||||
result.children = children;
|
];
|
||||||
|
} else {
|
||||||
|
let newChildren: MarkdownTree[] | string = [];
|
||||||
|
let index = n.from;
|
||||||
|
for (let child of children) {
|
||||||
|
let s = text.substring(index, child.from);
|
||||||
|
if (s) {
|
||||||
|
newChildren.push({
|
||||||
|
from: index,
|
||||||
|
to: child.from,
|
||||||
|
text: s,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
newChildren.push(child);
|
||||||
|
index = child.to;
|
||||||
}
|
}
|
||||||
if (nodeText) {
|
let s = text.substring(index, n.to);
|
||||||
result.text = nodeText;
|
if (s) {
|
||||||
|
newChildren.push({ from: index, to: n.to, text: s });
|
||||||
}
|
}
|
||||||
return result;
|
children = newChildren;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Currently unused
|
let result: MarkdownTree = {
|
||||||
function addParentPointers(mdTree: MarkdownTree) {
|
type: n.name,
|
||||||
if (!mdTree.children) {
|
from: n.from,
|
||||||
return;
|
to: n.to,
|
||||||
}
|
};
|
||||||
for (let child of mdTree.children) {
|
if (children.length > 0) {
|
||||||
child.parent = mdTree;
|
result.children = children;
|
||||||
addParentPointers(child);
|
}
|
||||||
}
|
if (nodeText) {
|
||||||
}
|
result.text = nodeText;
|
||||||
|
}
|
||||||
// Finds non-text node at position
|
return result;
|
||||||
export function nodeAtPos(
|
|
||||||
mdTree: MarkdownTree,
|
|
||||||
pos: number
|
|
||||||
): MarkdownTree | null {
|
|
||||||
if (pos < mdTree.from || pos > mdTree.to) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (!mdTree.children) {
|
|
||||||
return mdTree;
|
|
||||||
}
|
|
||||||
for (let child of mdTree.children) {
|
|
||||||
let n = nodeAtPos(child, pos);
|
|
||||||
if (n && n.text) {
|
|
||||||
// Got a text node, let's return its parent
|
|
||||||
return mdTree;
|
|
||||||
} else if (n) {
|
|
||||||
// Got it
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Turn MarkdownTree back into regular markdown text
|
|
||||||
export function render(mdTree: MarkdownTree): string {
|
|
||||||
let pieces: string[] = [];
|
|
||||||
if (mdTree.text) {
|
|
||||||
return mdTree.text;
|
|
||||||
}
|
|
||||||
for (let child of mdTree.children!) {
|
|
||||||
pieces.push(render(child));
|
|
||||||
}
|
|
||||||
return pieces.join("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parse(text: string): MarkdownTree {
|
export function parse(text: string): MarkdownTree {
|
||||||
return treeToAST(text, wikiMarkdownLang.parser.parse(text).topNode);
|
return treeToAST(text, wikiMarkdownLang.parser.parse(text).topNode);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
"context": "node"
|
"context": "node"
|
||||||
},
|
},
|
||||||
"test": {
|
"test": {
|
||||||
"source": ["common/tree.test.ts"],
|
"source": ["plugs/lib/tree.test.ts"],
|
||||||
"outputFormat": "commonjs",
|
"outputFormat": "commonjs",
|
||||||
"isLibrary": true,
|
"isLibrary": true,
|
||||||
"context": "node"
|
"context": "node"
|
||||||
|
|
|
@ -1,17 +1,6 @@
|
||||||
import {syscall} from "./syscall";
|
import {syscall} from "./syscall";
|
||||||
import type {MarkdownTree} from "../common/tree";
|
import type {MarkdownTree} from "../common/tree";
|
||||||
|
|
||||||
export async function parse(text: string): Promise<MarkdownTree> {
|
export async function parseMarkdown(text: string): Promise<MarkdownTree> {
|
||||||
return syscall("markdown.parse", text);
|
return syscall("markdown.parseMarkdown", text);
|
||||||
}
|
|
||||||
|
|
||||||
export async function nodeAtPos(
|
|
||||||
mdTree: MarkdownTree,
|
|
||||||
pos: number
|
|
||||||
): Promise<MarkdownTree | null> {
|
|
||||||
return syscall("markdown.nodeAtPos", mdTree, pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function render(mdTree: MarkdownTree): Promise<string> {
|
|
||||||
return syscall("markdown.render", mdTree);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Hook, Manifest } from "../types";
|
import {Hook, Manifest} from "../types";
|
||||||
import { System } from "../system";
|
import {System} from "../system";
|
||||||
import { safeRun } from "../util";
|
import {safeRun} from "../util";
|
||||||
|
|
||||||
// System events:
|
// System events:
|
||||||
// - plug:load (plugName: string)
|
// - plug:load (plugName: string)
|
||||||
|
@ -16,7 +16,6 @@ export class EventHook implements Hook<EventHookT> {
|
||||||
if (!this.system) {
|
if (!this.system) {
|
||||||
throw new Error("Event hook is not initialized");
|
throw new Error("Event hook is not initialized");
|
||||||
}
|
}
|
||||||
let promises: Promise<void>[] = [];
|
|
||||||
for (const plug of this.system.loadedPlugs.values()) {
|
for (const plug of this.system.loadedPlugs.values()) {
|
||||||
for (const [name, functionDef] of Object.entries(
|
for (const [name, functionDef] of Object.entries(
|
||||||
plug.manifest!.functions
|
plug.manifest!.functions
|
||||||
|
@ -24,12 +23,11 @@ export class EventHook implements Hook<EventHookT> {
|
||||||
if (functionDef.events && functionDef.events.includes(eventName)) {
|
if (functionDef.events && functionDef.events.includes(eventName)) {
|
||||||
// Only dispatch functions that can run in this environment
|
// Only dispatch functions that can run in this environment
|
||||||
if (plug.canInvoke(name)) {
|
if (plug.canInvoke(name)) {
|
||||||
promises.push(plug.invoke(name, [data]));
|
await plug.invoke(name, [data]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await Promise.all(promises);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
apply(system: System<EventHookT>): void {
|
apply(system: System<EventHookT>): void {
|
||||||
|
|
|
@ -3,7 +3,7 @@ import {EventHook} from "../hooks/event";
|
||||||
|
|
||||||
export function eventSyscalls(eventHook: EventHook): SysCallMapping {
|
export function eventSyscalls(eventHook: EventHook): SysCallMapping {
|
||||||
return {
|
return {
|
||||||
"event.dispatch": async(ctx, eventName: string, data: any) => {
|
"event.dispatch": async (ctx, eventName: string, data: any) => {
|
||||||
return eventHook.dispatchEvent(eventName, data);
|
return eventHook.dispatchEvent(eventName, data);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,7 +7,7 @@ export function fetchSyscalls(): SysCallMapping {
|
||||||
let resp = await fetch(url, init);
|
let resp = await fetch(url, init);
|
||||||
return resp.json();
|
return resp.json();
|
||||||
},
|
},
|
||||||
"fetch.text": async(ctx, url: RequestInfo, init: RequestInit) => {
|
"fetch.text": async (ctx, url: RequestInfo, init: RequestInit) => {
|
||||||
let resp = await fetch(url, init);
|
let resp = await fetch(url, init);
|
||||||
return resp.text();
|
return resp.text();
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,40 +1,49 @@
|
||||||
import { IndexEvent } from "../../webapp/app_event";
|
import {IndexEvent} from "../../webapp/app_event";
|
||||||
import { whiteOutQueries } from "./materialized_queries";
|
import {whiteOutQueries} from "./materialized_queries";
|
||||||
|
|
||||||
import { batchSet } from "plugos-silverbullet-syscall/index";
|
import {batchSet} from "plugos-silverbullet-syscall/index";
|
||||||
|
import {parseMarkdown} from "plugos-silverbullet-syscall/markdown";
|
||||||
|
import {collectNodesMatching, MarkdownTree, render} from "../lib/tree";
|
||||||
|
|
||||||
type Item = {
|
type Item = {
|
||||||
item: string;
|
item: string;
|
||||||
children?: string[];
|
nested?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const pageRefRe = /\[\[[^\]]+@\d+\]\]/;
|
|
||||||
const itemFullRe =
|
|
||||||
/(?<prefix>[\t ]*)[\-\*]\s*([^\n]+)(\n\k<prefix>\s+[\-\*][^\n]+)*/g;
|
|
||||||
|
|
||||||
export async function indexItems({ name, text }: IndexEvent) {
|
export async function indexItems({ name, text }: IndexEvent) {
|
||||||
let items: { key: string; value: Item }[] = [];
|
let items: { key: string; value: Item }[] = [];
|
||||||
text = whiteOutQueries(text);
|
text = whiteOutQueries(text);
|
||||||
for (let match of text.matchAll(itemFullRe)) {
|
|
||||||
let entire = match[0];
|
|
||||||
let item = match[2];
|
|
||||||
if (item.match(pageRefRe)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let pos = match.index!;
|
|
||||||
let lines = entire.split("\n");
|
|
||||||
|
|
||||||
|
console.log("Indexing items", name);
|
||||||
|
let mdTree = await parseMarkdown(text);
|
||||||
|
|
||||||
|
let coll = collectNodesMatching(mdTree, (n) => n.type === "ListItem");
|
||||||
|
|
||||||
|
coll.forEach((n) => {
|
||||||
|
if (!n.children) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let textNodes: MarkdownTree[] = [];
|
||||||
|
let nested: string | undefined;
|
||||||
|
for (let child of n.children!.slice(1)) {
|
||||||
|
if (child.type === "OrderedList" || child.type === "BulletList") {
|
||||||
|
nested = render(child);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
textNodes.push(child);
|
||||||
|
}
|
||||||
|
let item = textNodes.map(render).join("").trim();
|
||||||
let value: Item = {
|
let value: Item = {
|
||||||
item,
|
item,
|
||||||
};
|
};
|
||||||
if (lines.length > 1) {
|
if (nested) {
|
||||||
value.children = lines.slice(1);
|
value.nested = nested;
|
||||||
}
|
}
|
||||||
items.push({
|
items.push({
|
||||||
key: `it:${pos}`,
|
key: `it:${n.from}`,
|
||||||
value,
|
value,
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
console.log("Found", items.length, "item(s)");
|
console.log("Found", items.length, "item(s)");
|
||||||
await batchSet(name, items);
|
await batchSet(name, items);
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,13 +49,13 @@ export async function updateMaterializedQueriesOnPage(pageName: string) {
|
||||||
for (let {
|
for (let {
|
||||||
key,
|
key,
|
||||||
page,
|
page,
|
||||||
value: { task, complete, children },
|
value: { task, complete, nested },
|
||||||
} of await scanPrefixGlobal("task:")) {
|
} of await scanPrefixGlobal("task:")) {
|
||||||
let [, pos] = key.split(":");
|
let [, pos] = key.split(":");
|
||||||
if (!filter || (filter && task.includes(filter))) {
|
if (!filter || (filter && task.includes(filter))) {
|
||||||
results.push(
|
results.push(
|
||||||
`* [${complete ? "x" : " "}] [[${page}@${pos}]] ${task}` +
|
`* [${complete ? "x" : " "}] [[${page}@${pos}]] ${task}` +
|
||||||
(children ? "\n" + children.join("\n") : "")
|
(nested ? "\n " + nested : "")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,13 +78,12 @@ export async function updateMaterializedQueriesOnPage(pageName: string) {
|
||||||
for (let {
|
for (let {
|
||||||
key,
|
key,
|
||||||
page,
|
page,
|
||||||
value: { item, children },
|
value: { item, nested },
|
||||||
} of await scanPrefixGlobal("it:")) {
|
} of await scanPrefixGlobal("it:")) {
|
||||||
let [, pos] = key.split(":");
|
let [, pos] = key.split(":");
|
||||||
if (!filter || (filter && item.includes(filter))) {
|
if (!filter || (filter && item.includes(filter))) {
|
||||||
results.push(
|
results.push(
|
||||||
`* [[${page}@${pos}]] ${item}` +
|
`* [[${page}@${pos}]] ${item}` + (nested ? "\n " + nested : "")
|
||||||
(children ? "\n" + children.join("\n") : "")
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,8 @@ import {ClickEvent} from "../../webapp/app_event";
|
||||||
import {updateMaterializedQueriesCommand} from "./materialized_queries";
|
import {updateMaterializedQueriesCommand} from "./materialized_queries";
|
||||||
import {getCursor, getText, navigate as navigateTo, openUrl,} from "plugos-silverbullet-syscall/editor";
|
import {getCursor, getText, navigate as navigateTo, openUrl,} from "plugos-silverbullet-syscall/editor";
|
||||||
import {taskToggleAtPos} from "../tasks/task";
|
import {taskToggleAtPos} from "../tasks/task";
|
||||||
import {nodeAtPos, parse} from "plugos-silverbullet-syscall/markdown";
|
import {parseMarkdown} from "plugos-silverbullet-syscall/markdown";
|
||||||
|
import {nodeAtPos} from "../lib/tree";
|
||||||
import type {MarkdownTree} from "../../common/tree";
|
import type {MarkdownTree} from "../../common/tree";
|
||||||
|
|
||||||
const materializedQueryPrefix = /<!--\s*#query\s+/;
|
const materializedQueryPrefix = /<!--\s*#query\s+/;
|
||||||
|
@ -39,16 +40,15 @@ async function actionClickOrActionEnter(mdTree: MarkdownTree | null) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function linkNavigate() {
|
export async function linkNavigate() {
|
||||||
let mdTree = await parse(await getText());
|
let mdTree = await parseMarkdown(await getText());
|
||||||
let newNode = await nodeAtPos(mdTree, await getCursor());
|
let newNode = await nodeAtPos(mdTree, await getCursor());
|
||||||
await actionClickOrActionEnter(newNode);
|
await actionClickOrActionEnter(newNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function clickNavigate(event: ClickEvent) {
|
export async function clickNavigate(event: ClickEvent) {
|
||||||
if (event.ctrlKey || event.metaKey) {
|
if (event.ctrlKey || event.metaKey) {
|
||||||
let mdTree = await parse(await getText());
|
let mdTree = await parseMarkdown(await getText());
|
||||||
let newNode = await nodeAtPos(mdTree, event.pos);
|
let newNode = nodeAtPos(mdTree, event.pos);
|
||||||
console.log("New node", newNode);
|
|
||||||
await actionClickOrActionEnter(newNode);
|
await actionClickOrActionEnter(newNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
import {expect, test} from "@jest/globals";
|
||||||
|
import {parse} from "../../common/tree";
|
||||||
|
import {addParentPointers, collectNodesMatching, findParentMatching, nodeAtPos, render,} from "./tree";
|
||||||
|
|
||||||
|
const mdTest1 = `
|
||||||
|
# Heading
|
||||||
|
## Sub _heading_ cool
|
||||||
|
|
||||||
|
Hello, this is some **bold** text and *italic*. And [a link](http://zef.me).
|
||||||
|
|
||||||
|
%% My comment here
|
||||||
|
%% And second line
|
||||||
|
|
||||||
|
And an @mention
|
||||||
|
|
||||||
|
http://zef.plus
|
||||||
|
|
||||||
|
- This is a list [[PageLink]]
|
||||||
|
- With another item
|
||||||
|
- TODOs:
|
||||||
|
- [ ] A task that's not yet done
|
||||||
|
- [x] Hello
|
||||||
|
- And a _third_ one [[Wiki Page]] yo
|
||||||
|
`;
|
||||||
|
|
||||||
|
const mdTest2 = `
|
||||||
|
Hello
|
||||||
|
|
||||||
|
* Item 1
|
||||||
|
*
|
||||||
|
|
||||||
|
Sup`;
|
||||||
|
|
||||||
|
test("Run a Node sandbox", async () => {
|
||||||
|
let mdTree = parse(mdTest1);
|
||||||
|
addParentPointers(mdTree);
|
||||||
|
// console.log(JSON.stringify(mdTree, null, 2));
|
||||||
|
let wikiLink = nodeAtPos(mdTree, mdTest1.indexOf("Wiki Page"))!;
|
||||||
|
expect(wikiLink.type).toBe("WikiLink");
|
||||||
|
expect(
|
||||||
|
findParentMatching(wikiLink, (n) => n.type === "BulletList")
|
||||||
|
).toBeDefined();
|
||||||
|
|
||||||
|
let allTodos = collectNodesMatching(mdTree, (n) => n.type === "Task");
|
||||||
|
expect(allTodos.length).toBe(2);
|
||||||
|
|
||||||
|
// Render back into markdown should be equivalent
|
||||||
|
expect(render(mdTree)).toBe(mdTest1);
|
||||||
|
|
||||||
|
let mdTree2 = parse(mdTest2);
|
||||||
|
console.log(JSON.stringify(mdTree2, null, 2));
|
||||||
|
});
|
|
@ -0,0 +1,101 @@
|
||||||
|
export type MarkdownTree = {
|
||||||
|
type?: string; // undefined === text node
|
||||||
|
from: number;
|
||||||
|
to: number;
|
||||||
|
text?: string;
|
||||||
|
children?: MarkdownTree[];
|
||||||
|
parent?: MarkdownTree;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function addParentPointers(mdTree: MarkdownTree) {
|
||||||
|
if (!mdTree.children) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let child of mdTree.children) {
|
||||||
|
child.parent = mdTree;
|
||||||
|
addParentPointers(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeParentPointers(mdTree: MarkdownTree) {
|
||||||
|
delete mdTree.parent;
|
||||||
|
if (!mdTree.children) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let child of mdTree.children) {
|
||||||
|
removeParentPointers(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findParentMatching(
|
||||||
|
mdTree: MarkdownTree,
|
||||||
|
matchFn: (mdTree: MarkdownTree) => boolean
|
||||||
|
): MarkdownTree | null {
|
||||||
|
let node = mdTree.parent;
|
||||||
|
while (node) {
|
||||||
|
if (matchFn(node)) {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
node = node.parent;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function collectNodesMatching(
|
||||||
|
mdTree: MarkdownTree,
|
||||||
|
matchFn: (mdTree: MarkdownTree) => boolean
|
||||||
|
): MarkdownTree[] {
|
||||||
|
if (matchFn(mdTree)) {
|
||||||
|
return [mdTree];
|
||||||
|
}
|
||||||
|
let results: MarkdownTree[] = [];
|
||||||
|
if (mdTree.children) {
|
||||||
|
for (let child of mdTree.children) {
|
||||||
|
results = [...results, ...collectNodesMatching(child, matchFn)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findNodeMatching(
|
||||||
|
mdTree: MarkdownTree,
|
||||||
|
matchFn: (mdTree: MarkdownTree) => boolean
|
||||||
|
): MarkdownTree | null {
|
||||||
|
return collectNodesMatching(mdTree, matchFn)[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finds non-text node at position
|
||||||
|
export function nodeAtPos(
|
||||||
|
mdTree: MarkdownTree,
|
||||||
|
pos: number
|
||||||
|
): MarkdownTree | null {
|
||||||
|
if (pos < mdTree.from || pos > mdTree.to) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!mdTree.children) {
|
||||||
|
return mdTree;
|
||||||
|
}
|
||||||
|
for (let child of mdTree.children) {
|
||||||
|
let n = nodeAtPos(child, pos);
|
||||||
|
if (n && n.text !== undefined) {
|
||||||
|
// Got a text node, let's return its parent
|
||||||
|
return mdTree;
|
||||||
|
} else if (n) {
|
||||||
|
// Got it
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Turn MarkdownTree back into regular markdown text
|
||||||
|
export function render(mdTree: MarkdownTree): string {
|
||||||
|
let pieces: string[] = [];
|
||||||
|
if (mdTree.text !== undefined) {
|
||||||
|
return mdTree.text;
|
||||||
|
}
|
||||||
|
for (let child of mdTree.children!) {
|
||||||
|
pieces.push(render(child));
|
||||||
|
}
|
||||||
|
return pieces.join("");
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
"name": "plugs",
|
"name": "plugs",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@jest/globals": "^27.5.1",
|
||||||
"plugos-silverbullet-syscall": "file:../plugos-silverbullet-syscall",
|
"plugos-silverbullet-syscall": "file:../plugos-silverbullet-syscall",
|
||||||
"plugos-syscall": "file:../plugos-syscall"
|
"plugos-syscall": "file:../plugos-syscall"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,50 +1,45 @@
|
||||||
import type { ClickEvent } from "../../webapp/app_event";
|
import type {ClickEvent} from "../../webapp/app_event";
|
||||||
import { IndexEvent } from "../../webapp/app_event";
|
import {IndexEvent} from "../../webapp/app_event";
|
||||||
|
|
||||||
import { whiteOutQueries } from "../core/materialized_queries";
|
import {whiteOutQueries} from "../core/materialized_queries";
|
||||||
import { batchSet } from "plugos-silverbullet-syscall/index";
|
import {batchSet} from "plugos-silverbullet-syscall/index";
|
||||||
import { readPage, writePage } from "plugos-silverbullet-syscall/space";
|
import {readPage, writePage} from "plugos-silverbullet-syscall/space";
|
||||||
import {
|
import {parseMarkdown} from "plugos-silverbullet-syscall/markdown";
|
||||||
dispatch,
|
import {dispatch, getText,} from "plugos-silverbullet-syscall/editor";
|
||||||
getLineUnderCursor,
|
import {addParentPointers, collectNodesMatching, nodeAtPos, render,} from "../lib/tree";
|
||||||
getSyntaxNodeAtPos,
|
|
||||||
} from "plugos-silverbullet-syscall/editor";
|
|
||||||
|
|
||||||
const taskFullRe =
|
|
||||||
/(?<prefix>[\t ]*)[\-\*]\s*\[([ Xx])\]\s*([^\n]+)(\n\k<prefix>\s+[\-\*][^\n]+)*/g;
|
|
||||||
|
|
||||||
const extractPageLink = /[\-\*]\s*\[[ Xx]\]\s\[\[([^\]]+)@(\d+)\]\]\s*(.*)/;
|
|
||||||
|
|
||||||
type Task = {
|
type Task = {
|
||||||
task: string;
|
task: string;
|
||||||
complete: boolean;
|
complete: boolean;
|
||||||
pos?: number;
|
pos?: number;
|
||||||
children?: string[];
|
nested?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function indexTasks({ name, text }: IndexEvent) {
|
export async function indexTasks({ name, text }: IndexEvent) {
|
||||||
console.log("Indexing tasks");
|
console.log("Indexing tasks");
|
||||||
let tasks: { key: string; value: Task }[] = [];
|
let tasks: { key: string; value: Task }[] = [];
|
||||||
text = whiteOutQueries(text);
|
text = whiteOutQueries(text);
|
||||||
for (let match of text.matchAll(taskFullRe)) {
|
let mdTree = await parseMarkdown(text);
|
||||||
let entire = match[0];
|
addParentPointers(mdTree);
|
||||||
let complete = match[2] !== " ";
|
collectNodesMatching(mdTree, (n) => n.type === "Task").forEach((n) => {
|
||||||
let task = match[3];
|
let task = n.children!.slice(1).map(render).join("").trim();
|
||||||
let pos = match.index!;
|
let complete = n.children![0].children![0].text! !== "[ ]";
|
||||||
let lines = entire.split("\n");
|
|
||||||
|
|
||||||
let value: Task = {
|
let value: Task = {
|
||||||
task,
|
task,
|
||||||
complete,
|
complete,
|
||||||
};
|
};
|
||||||
if (lines.length > 1) {
|
let taskIndex = n.parent!.children!.indexOf(n);
|
||||||
value.children = lines.slice(1);
|
let nestedItems = n.parent!.children!.slice(taskIndex + 1);
|
||||||
|
if (nestedItems.length > 0) {
|
||||||
|
value.nested = nestedItems.map(render).join("").trim();
|
||||||
}
|
}
|
||||||
tasks.push({
|
tasks.push({
|
||||||
key: `task:${pos}`,
|
key: `task:${n.from}`,
|
||||||
value,
|
value,
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
console.log("Found", tasks.length, "task(s)");
|
console.log("Found", tasks.length, "task(s)");
|
||||||
await batchSet(name, tasks);
|
await batchSet(name, tasks);
|
||||||
}
|
}
|
||||||
|
@ -54,41 +49,55 @@ export async function taskToggle(event: ClickEvent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function taskToggleAtPos(pos: number) {
|
export async function taskToggleAtPos(pos: number) {
|
||||||
let syntaxNode = await getSyntaxNodeAtPos(pos);
|
let text = await getText();
|
||||||
if (syntaxNode && syntaxNode.name === "TaskMarker") {
|
let mdTree = await parseMarkdown(text);
|
||||||
|
addParentPointers(mdTree);
|
||||||
|
|
||||||
|
let node = nodeAtPos(mdTree, pos);
|
||||||
|
if (node && node.type === "TaskMarker") {
|
||||||
let changeTo = "[x]";
|
let changeTo = "[x]";
|
||||||
if (syntaxNode.text === "[x]" || syntaxNode.text === "[X]") {
|
if (node.children![0].text === "[x]" || node.children![0].text === "[X]") {
|
||||||
changeTo = "[ ]";
|
changeTo = "[ ]";
|
||||||
}
|
}
|
||||||
await dispatch({
|
await dispatch({
|
||||||
changes: {
|
changes: {
|
||||||
from: syntaxNode.from,
|
from: node.from,
|
||||||
to: syntaxNode.to,
|
to: node.to,
|
||||||
insert: changeTo,
|
insert: changeTo,
|
||||||
},
|
},
|
||||||
selection: {
|
selection: {
|
||||||
anchor: pos,
|
anchor: pos,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
// In case there's a page reference with @ position in the task, let's propagate this change back to that page
|
|
||||||
// Example: * [ ] [[MyPage@123]] My task
|
|
||||||
let line = await getLineUnderCursor();
|
|
||||||
let match = line.match(extractPageLink);
|
|
||||||
if (match) {
|
|
||||||
console.log("Found a remote task reference, updating other page");
|
|
||||||
let [, page, posS] = match;
|
|
||||||
let pos = +posS;
|
|
||||||
let pageData = await readPage(page);
|
|
||||||
let text = pageData.text;
|
|
||||||
|
|
||||||
// Apply the toggle
|
let parentWikiLinks = collectNodesMatching(
|
||||||
text =
|
node.parent!,
|
||||||
text.substring(0, pos) +
|
(n) => n.type === "WikiLinkPage"
|
||||||
text
|
);
|
||||||
.substring(pos)
|
for (let wikiLink of parentWikiLinks) {
|
||||||
.replace(/^(\s*[\-\*]\s*)\[[ xX]\]/, "$1" + changeTo);
|
let ref = wikiLink.children![0].text!;
|
||||||
|
if (ref.includes("@")) {
|
||||||
|
let [page, pos] = ref.split("@");
|
||||||
|
let pageData = await readPage(page);
|
||||||
|
let text = pageData.text;
|
||||||
|
|
||||||
await writePage(page, text);
|
let referenceMdTree = await parseMarkdown(text);
|
||||||
|
// Adding +1 to immediately hit the task marker
|
||||||
|
let taskMarkerNode = nodeAtPos(referenceMdTree, +pos + 1);
|
||||||
|
|
||||||
|
if (!taskMarkerNode || taskMarkerNode.type !== "TaskMarker") {
|
||||||
|
console.error(
|
||||||
|
"Reference not a task marker, out of date?",
|
||||||
|
taskMarkerNode
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
taskMarkerNode.children![0].text = changeTo;
|
||||||
|
console.log("This will be the new marker", render(taskMarkerNode));
|
||||||
|
text = render(referenceMdTree);
|
||||||
|
console.log("Updated reference paged text", text);
|
||||||
|
await writePage(page, text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { mkdir, readdir, readFile, stat, unlink, writeFile } from "fs/promises";
|
import {mkdir, readdir, readFile, stat, unlink, writeFile} from "fs/promises";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import { PageMeta } from "../common/types";
|
import {PageMeta} from "../common/types";
|
||||||
import { EventHook } from "../plugos/hooks/event";
|
import {EventHook} from "../plugos/hooks/event";
|
||||||
|
|
||||||
export interface Storage {
|
export interface Storage {
|
||||||
listPages(): Promise<PageMeta[]>;
|
listPages(): Promise<PageMeta[]>;
|
||||||
|
@ -29,12 +29,17 @@ export class EventedStorage implements Storage {
|
||||||
async writePage(pageName: string, text: string): Promise<PageMeta> {
|
async writePage(pageName: string, text: string): Promise<PageMeta> {
|
||||||
const newPageMeta = this.wrapped.writePage(pageName, text);
|
const newPageMeta = this.wrapped.writePage(pageName, text);
|
||||||
// This can happen async
|
// This can happen async
|
||||||
this.eventHook.dispatchEvent("page:saved", pageName).then(() => {
|
this.eventHook
|
||||||
return this.eventHook.dispatchEvent("page:index", {
|
.dispatchEvent("page:saved", pageName)
|
||||||
name: pageName,
|
.then(() => {
|
||||||
text: text,
|
return this.eventHook.dispatchEvent("page:index", {
|
||||||
|
name: pageName,
|
||||||
|
text: text,
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
console.error("Error dispatching page:saved event", e);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
return newPageMeta;
|
return newPageMeta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,8 @@
|
||||||
import { styleTags, tags as t } from "@codemirror/highlight";
|
import {styleTags, tags as t} from "@codemirror/highlight";
|
||||||
import {
|
import {BlockContext, LeafBlock, LeafBlockParser, MarkdownConfig, TaskList,} from "@lezer/markdown";
|
||||||
BlockContext,
|
import {commonmark, mkLang} from "./markdown/markdown";
|
||||||
LeafBlock,
|
|
||||||
LeafBlockParser,
|
|
||||||
MarkdownConfig,
|
|
||||||
TaskList,
|
|
||||||
} from "@lezer/markdown";
|
|
||||||
import { commonmark, mkLang } from "./markdown/markdown";
|
|
||||||
import * as ct from "./customtags";
|
import * as ct from "./customtags";
|
||||||
import { pageLinkRegex } from "./constant";
|
import {pageLinkRegex} from "./constant";
|
||||||
|
|
||||||
const pageLinkRegexPrefix = new RegExp(
|
const pageLinkRegexPrefix = new RegExp(
|
||||||
"^" + pageLinkRegex.toString().slice(1, -1)
|
"^" + pageLinkRegex.toString().slice(1, -1)
|
||||||
|
@ -28,7 +22,7 @@ const WikiLink: MarkdownConfig = {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return cx.addElement(
|
return cx.addElement(
|
||||||
cx.elt("WikiLink", pos, pos + match[0].length + 1, [
|
cx.elt("WikiLink", pos, pos + match[0].length, [
|
||||||
cx.elt("WikiLinkPage", pos + 2, pos + match[0].length - 2),
|
cx.elt("WikiLinkPage", pos + 2, pos + match[0].length - 2),
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue