1
0
silverbullet/plugs/core/anchor.ts
Zef Hemel 3545d00d46
Major mini editor refactoring (#225)
Replaces most editing components with CM components, enabling vim mode and completions everywhere

Fixes #205 
Fixes #221 
Fixes #222 
Fixes #223
2022-12-21 14:55:24 +01:00

45 lines
1.3 KiB
TypeScript

import { collectNodesOfType } from "$sb/lib/tree.ts";
import { editor, index } from "$sb/silverbullet-syscall/mod.ts";
import type { CompleteEvent, IndexTreeEvent } from "$sb/app_event.ts";
import { removeQueries } from "$sb/lib/query.ts";
// Key space
// a:pageName:anchorName => pos
export async function indexAnchors({ name: pageName, tree }: IndexTreeEvent) {
removeQueries(tree);
const anchors: { key: string; value: string }[] = [];
collectNodesOfType(tree, "NamedAnchor").forEach((n) => {
const aName = n.children![0].text!.substring(1);
anchors.push({
key: `a:${pageName}:${aName}`,
value: "" + n.from,
});
});
// console.log("Found", anchors.length, "anchors(s)");
await index.batchSet(pageName, anchors);
}
export async function anchorComplete(completeEvent: CompleteEvent) {
const match = /\[\[([^\]@:]*@[\w\.\-\/]*)$/.exec(completeEvent.linePrefix);
if (!match) {
return null;
}
let [pageRef, anchorRef] = match[1].split("@");
if (!pageRef) {
pageRef = await editor.getCurrentPage();
}
const allAnchors = await index.queryPrefix(
`a:${pageRef}:${anchorRef}`,
);
return {
from: completeEvent.pos - anchorRef.length,
options: allAnchors.map((a) => ({
label: a.key.split(":")[2],
type: "anchor",
})),
};
}