2022-10-10 12:50:21 +00:00
|
|
|
import { collectNodesOfType } from "../../common/tree.ts";
|
2022-08-30 08:44:20 +00:00
|
|
|
import {
|
|
|
|
batchSet,
|
|
|
|
queryPrefix,
|
2022-10-10 12:50:21 +00:00
|
|
|
} from "../../syscall/silverbullet-syscall/index.ts";
|
2022-08-30 08:44:20 +00:00
|
|
|
import {
|
|
|
|
getCurrentPage,
|
|
|
|
matchBefore,
|
2022-10-10 12:50:21 +00:00
|
|
|
} from "../../syscall/silverbullet-syscall/editor.ts";
|
|
|
|
import type { IndexTreeEvent } from "../../web/app_event.ts";
|
|
|
|
import { removeQueries } from "../query/util.ts";
|
2022-08-30 08:44:20 +00:00
|
|
|
|
|
|
|
// Key space
|
|
|
|
// a:pageName:anchorName => pos
|
|
|
|
|
|
|
|
export async function indexAnchors({ name: pageName, tree }: IndexTreeEvent) {
|
|
|
|
removeQueries(tree);
|
|
|
|
let anchors: { key: string; value: string }[] = [];
|
|
|
|
|
|
|
|
collectNodesOfType(tree, "NamedAnchor").forEach((n) => {
|
|
|
|
let aName = n.children![0].text!;
|
|
|
|
anchors.push({
|
|
|
|
key: `a:${pageName}:${aName}`,
|
|
|
|
value: "" + n.from,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
console.log("Found", anchors.length, "anchors(s)");
|
|
|
|
await batchSet(pageName, anchors);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function anchorComplete() {
|
2022-09-02 13:18:33 +00:00
|
|
|
let prefix = await matchBefore("\\[\\[[^\\]@:]*@[\\w\\.\\-\\/]*");
|
2022-08-30 08:44:20 +00:00
|
|
|
if (!prefix) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const [pageRefPrefix, anchorRef] = prefix.text.split("@");
|
|
|
|
let pageRef = pageRefPrefix.substring(2);
|
|
|
|
if (!pageRef) {
|
|
|
|
pageRef = await getCurrentPage();
|
|
|
|
}
|
|
|
|
let allAnchors = await queryPrefix(`a:${pageRef}:@${anchorRef}`);
|
|
|
|
return {
|
|
|
|
from: prefix.from + pageRefPrefix.length + 1,
|
|
|
|
options: allAnchors.map((a) => ({
|
|
|
|
label: a.key.split("@")[1],
|
|
|
|
type: "anchor",
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|