2023-09-01 14:57:29 +00:00
|
|
|
import { CompleteEvent } from "$sb/app_event.ts";
|
2023-11-06 08:14:16 +00:00
|
|
|
import { FileMeta, PageMeta } from "$sb/types.ts";
|
2023-09-01 14:57:29 +00:00
|
|
|
import { cacheFileListing } from "../federation/federation.ts";
|
2023-12-21 17:37:50 +00:00
|
|
|
import { queryObjects } from "../index/plug_api.ts";
|
2023-09-01 14:57:29 +00:00
|
|
|
|
|
|
|
// Completion
|
|
|
|
export async function pageComplete(completeEvent: CompleteEvent) {
|
2023-11-03 11:01:33 +00:00
|
|
|
const match = /\[\[([^\]@$:\{}]*)$/.exec(completeEvent.linePrefix);
|
2023-09-01 14:57:29 +00:00
|
|
|
if (!match) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-21 17:37:50 +00:00
|
|
|
// When we're in fenced code block, we likely want to complete a page name without an alias, and only complete template pages
|
|
|
|
// so let's check if we're in a template context
|
|
|
|
const isInTemplateContext =
|
|
|
|
completeEvent.parentNodes.find((node) => node.startsWith("FencedCode")) &&
|
|
|
|
// either a render [[bla]] clause or page: "[[bla]]" template block
|
|
|
|
/render\s+\[\[|page:\s*["']\[\[/.test(
|
|
|
|
completeEvent.linePrefix,
|
|
|
|
);
|
2024-01-20 18:16:07 +00:00
|
|
|
|
|
|
|
// When in a template context, we only want to complete template pages
|
|
|
|
// When outside of a template context, we want to complete all pages except template pages
|
|
|
|
let allPages: PageMeta[] = isInTemplateContext
|
|
|
|
? await queryObjects<PageMeta>("template", {}, 5)
|
|
|
|
: await queryObjects<PageMeta>("page", {
|
|
|
|
filter: ["!=", ["attr", "tags"], ["string", "template"]],
|
|
|
|
}, 5);
|
2023-09-01 14:57:29 +00:00
|
|
|
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 08:14:16 +00:00
|
|
|
).map(fileMetaToPageMeta);
|
2023-09-01 14:57:29 +00:00
|
|
|
if (federationPages.length > 0) {
|
|
|
|
allPages = allPages.concat(federationPages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
from: completeEvent.pos - match[1].length,
|
|
|
|
options: allPages.map((pageMeta) => {
|
2023-12-21 17:37:50 +00:00
|
|
|
const completions: any[] = [];
|
|
|
|
if (pageMeta.displayName) {
|
|
|
|
completions.push({
|
2023-12-22 14:55:50 +00:00
|
|
|
label: `${pageMeta.displayName}`,
|
|
|
|
boost: new Date(pageMeta.lastModified).getTime(),
|
2023-12-21 17:37:50 +00:00
|
|
|
apply: isInTemplateContext
|
|
|
|
? pageMeta.name
|
|
|
|
: `${pageMeta.name}|${pageMeta.displayName}`,
|
2023-12-22 15:40:38 +00:00
|
|
|
detail: `displayName for: ${pageMeta.name}`,
|
2023-12-21 17:37:50 +00:00
|
|
|
type: "page",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (Array.isArray(pageMeta.aliases)) {
|
|
|
|
for (const alias of pageMeta.aliases) {
|
|
|
|
completions.push({
|
2023-12-22 14:55:50 +00:00
|
|
|
label: `${alias}`,
|
|
|
|
boost: new Date(pageMeta.lastModified).getTime(),
|
2023-12-21 17:37:50 +00:00
|
|
|
apply: isInTemplateContext
|
|
|
|
? pageMeta.name
|
|
|
|
: `${pageMeta.name}|${alias}`,
|
2023-12-22 15:40:38 +00:00
|
|
|
detail: `alias to: ${pageMeta.name}`,
|
2023-12-21 17:37:50 +00:00
|
|
|
type: "page",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
completions.push({
|
2023-12-22 14:55:50 +00:00
|
|
|
label: `${pageMeta.name}`,
|
|
|
|
boost: new Date(pageMeta.lastModified).getTime(),
|
2023-09-01 14:57:29 +00:00
|
|
|
type: "page",
|
2023-12-21 17:37:50 +00:00
|
|
|
});
|
|
|
|
return completions;
|
|
|
|
}).flat(),
|
2023-09-01 14:57:29 +00:00
|
|
|
};
|
|
|
|
}
|
2023-11-06 08:14:16 +00:00
|
|
|
|
|
|
|
function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
|
|
|
|
const name = fileMeta.name.substring(0, fileMeta.name.length - 3);
|
|
|
|
return {
|
|
|
|
...fileMeta,
|
|
|
|
ref: fileMeta.name,
|
2024-01-11 12:20:50 +00:00
|
|
|
tag: "page",
|
2023-11-06 08:14:16 +00:00
|
|
|
name,
|
|
|
|
created: new Date(fileMeta.created).toISOString(),
|
|
|
|
lastModified: new Date(fileMeta.lastModified).toISOString(),
|
|
|
|
} as PageMeta;
|
|
|
|
}
|