diff --git a/plugs/index/anchor.ts b/plugs/index/anchor.ts index f374491..d980068 100644 --- a/plugs/index/anchor.ts +++ b/plugs/index/anchor.ts @@ -1,7 +1,7 @@ import { collectNodesOfType } from "$sb/lib/tree.ts"; import type { CompleteEvent, IndexTreeEvent } from "$sb/app_event.ts"; import { removeQueries } from "$sb/lib/query.ts"; -import { ObjectValue } from "$sb/types.ts"; +import { ObjectValue, QueryExpression } from "$sb/types.ts"; import { indexObjects, queryObjects } from "./api.ts"; type AnchorObject = ObjectValue<{ @@ -34,17 +34,20 @@ export async function anchorComplete(completeEvent: CompleteEvent) { return null; } - let [pageRef, anchorRef] = match[1].split("@"); + const pageRef = match[1].split("@")[0]; + let filter: QueryExpression | undefined = ["=", ["attr", "page"], [ + "string", + pageRef, + ]]; if (!pageRef) { - pageRef = completeEvent.pageName; + // "bare" anchor, match any page for completion purposes + filter = undefined; } - const allAnchors = await queryObjects("anchor", { - filter: ["=", ["attr", "page"], ["string", pageRef]], - }); + const allAnchors = await queryObjects("anchor", { filter }); return { - from: completeEvent.pos - anchorRef.length, + from: completeEvent.pos - match[1].length, options: allAnchors.map((a) => ({ - label: a.name, + label: a.page === completeEvent.pageName ? `@${a.name}` : a.ref, type: "anchor", })), };