silverbullet/plugs/editor/complete.ts

108 lines
3.5 KiB
TypeScript
Raw Normal View History

import { CompleteEvent, FileMeta, PageMeta } from "$type/types.ts";
import { cacheFileListing } from "../federation/federation.ts";
2023-12-22 01:37:50 +08:00
import { queryObjects } from "../index/plug_api.ts";
// Completion
export async function pageComplete(completeEvent: CompleteEvent) {
2024-01-25 21:51:40 +08:00
const match = /\[\[([^\]@$#:\{}]*)$/.exec(completeEvent.linePrefix);
if (!match) {
return null;
}
let allPages: PageMeta[] = [];
if (
2023-12-22 01:37:50 +08:00
completeEvent.parentNodes.find((node) => node.startsWith("FencedCode")) &&
// either a render [[bla]] clause
2024-02-04 23:36:59 +08:00
/(render\s+|template\()\[\[/.test(
2023-12-22 01:37:50 +08:00
completeEvent.linePrefix,
)
) {
// We're in a template context, let's only complete templates
allPages = await queryObjects<PageMeta>("template", {}, 5);
} else if (
completeEvent.parentNodes.find((node) =>
node.startsWith("FencedCode:include") ||
node.startsWith("FencedCode:template")
)
) {
// Include both pages and templates in page completion in ```include and ```template blocks
allPages = await queryObjects<PageMeta>("page", {}, 5);
} else {
// Otherwise, just complete non-template pages
allPages = await queryObjects<PageMeta>("page", {
filter: ["!=", ["attr", "tags"], ["string", "template"]],
}, 5);
}
const prefix = match[1];
if (prefix.startsWith("!")) {
// Federation prefix, let's first see if we're matching anything from federation that is locally synced
const prefixMatches = allPages.filter((pageMeta) =>
pageMeta.name.startsWith(prefix)
);
if (prefixMatches.length === 0) {
// Ok, nothing synced in via federation, let's see if this URI is complete enough to try to fetch index.json
if (prefix.includes("/")) {
// Yep
const domain = prefix.split("/")[0];
// Cached listing
const federationPages = (await cacheFileListing(domain)).filter((fm) =>
fm.name.endsWith(".md")
2023-11-06 16:14:16 +08:00
).map(fileMetaToPageMeta);
if (federationPages.length > 0) {
allPages = allPages.concat(federationPages);
}
}
}
}
return {
from: completeEvent.pos - match[1].length,
options: allPages.map((pageMeta) => {
2023-12-22 01:37:50 +08:00
const completions: any[] = [];
if (pageMeta.displayName) {
completions.push({
2023-12-22 22:55:50 +08:00
label: `${pageMeta.displayName}`,
boost: new Date(pageMeta.lastModified).getTime(),
apply: pageMeta.tag === "template"
2023-12-22 01:37:50 +08:00
? pageMeta.name
: `${pageMeta.name}|${pageMeta.displayName}`,
2023-12-22 23:40:38 +08:00
detail: `displayName for: ${pageMeta.name}`,
2023-12-22 01:37:50 +08:00
type: "page",
});
}
if (Array.isArray(pageMeta.aliases)) {
for (const alias of pageMeta.aliases) {
completions.push({
2023-12-22 22:55:50 +08:00
label: `${alias}`,
boost: new Date(pageMeta.lastModified).getTime(),
apply: pageMeta.tag === "template"
2023-12-22 01:37:50 +08:00
? pageMeta.name
: `${pageMeta.name}|${alias}`,
2023-12-22 23:40:38 +08:00
detail: `alias to: ${pageMeta.name}`,
2023-12-22 01:37:50 +08:00
type: "page",
});
}
}
completions.push({
2023-12-22 22:55:50 +08:00
label: `${pageMeta.name}`,
boost: new Date(pageMeta.lastModified).getTime(),
type: "page",
2023-12-22 01:37:50 +08:00
});
return completions;
}).flat(),
};
}
2023-11-06 16:14:16 +08:00
function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
const name = fileMeta.name.substring(0, fileMeta.name.length - 3);
return {
...fileMeta,
ref: fileMeta.name,
tag: "page",
2023-11-06 16:14:16 +08:00
name,
created: new Date(fileMeta.created).toISOString(),
lastModified: new Date(fileMeta.lastModified).toISOString(),
} as PageMeta;
}