2024-08-24 18:35:09 +08:00
|
|
|
import type { IndexTreeEvent } from "@silverbulletmd/silverbullet/types";
|
2024-08-07 02:11:38 +08:00
|
|
|
import {
|
|
|
|
editor,
|
2024-08-24 22:14:29 +08:00
|
|
|
jsonschema,
|
2024-08-07 02:11:38 +08:00
|
|
|
markdown,
|
|
|
|
space,
|
2024-08-24 22:14:29 +08:00
|
|
|
system,
|
2024-08-07 02:11:38 +08:00
|
|
|
YAML,
|
|
|
|
} from "@silverbulletmd/silverbullet/syscalls";
|
2023-10-03 20:16:33 +08:00
|
|
|
|
2024-02-29 22:23:05 +08:00
|
|
|
import type { LintDiagnostic, PageMeta } from "../../plug-api/types.ts";
|
2024-08-07 02:11:38 +08:00
|
|
|
import { extractFrontmatter } from "@silverbulletmd/silverbullet/lib/frontmatter";
|
|
|
|
import { extractAttributes } from "@silverbulletmd/silverbullet/lib/attribute";
|
2024-08-24 18:35:09 +08:00
|
|
|
import { indexObjects, queryDeleteObjects } from "./api.ts";
|
2024-02-29 22:23:05 +08:00
|
|
|
import {
|
|
|
|
findNodeOfType,
|
|
|
|
renderToText,
|
|
|
|
traverseTreeAsync,
|
2024-08-24 18:35:09 +08:00
|
|
|
} from "@silverbulletmd/silverbullet/lib/tree";
|
2024-08-07 02:11:38 +08:00
|
|
|
import { updateITags } from "@silverbulletmd/silverbullet/lib/tags";
|
2024-08-24 18:35:09 +08:00
|
|
|
import type { AspiringPageObject } from "./page_links.ts";
|
2024-08-24 22:14:29 +08:00
|
|
|
import { deepObjectMerge } from "@silverbulletmd/silverbullet/lib/json";
|
2023-10-03 20:16:33 +08:00
|
|
|
|
|
|
|
export async function indexPage({ name, tree }: IndexTreeEvent) {
|
2023-10-31 17:33:38 +08:00
|
|
|
if (name.startsWith("_")) {
|
|
|
|
// Don't index pages starting with _
|
|
|
|
return;
|
|
|
|
}
|
2023-12-22 19:10:09 +08:00
|
|
|
const pageMeta = await space.getPageMeta(name);
|
2023-11-06 16:14:16 +08:00
|
|
|
const frontmatter = await extractFrontmatter(tree);
|
2024-02-28 03:05:12 +08:00
|
|
|
const toplevelAttributes = await extractAttributes(
|
|
|
|
["page", ...frontmatter.tags || []],
|
|
|
|
tree,
|
|
|
|
);
|
2023-10-03 20:16:33 +08:00
|
|
|
|
|
|
|
// Push them all into the page object
|
2023-12-22 18:27:07 +08:00
|
|
|
// Note the order here, making sure that the actual page meta data overrules
|
|
|
|
// any attempt to manually set built-in attributes like 'name' or 'lastModified'
|
2023-12-22 19:10:09 +08:00
|
|
|
// pageMeta appears at the beginning and the end due to the ordering behavior of ojects in JS (making builtin attributes appear first)
|
2024-01-11 20:20:50 +08:00
|
|
|
const combinedPageMeta: PageMeta = {
|
2023-12-22 19:10:09 +08:00
|
|
|
...pageMeta,
|
|
|
|
...frontmatter,
|
|
|
|
...toplevelAttributes,
|
|
|
|
...pageMeta,
|
|
|
|
};
|
2023-10-03 20:16:33 +08:00
|
|
|
|
2023-12-22 19:10:09 +08:00
|
|
|
combinedPageMeta.tags = [
|
|
|
|
...new Set([
|
|
|
|
...frontmatter.tags || [],
|
|
|
|
...toplevelAttributes.tags || [],
|
|
|
|
]),
|
|
|
|
];
|
2023-10-03 20:16:33 +08:00
|
|
|
|
2024-01-11 20:20:50 +08:00
|
|
|
combinedPageMeta.tag = "page";
|
2023-11-27 17:03:23 +08:00
|
|
|
|
2024-01-28 20:38:31 +08:00
|
|
|
if (combinedPageMeta.aliases && !Array.isArray(combinedPageMeta.aliases)) {
|
|
|
|
console.warn(
|
|
|
|
"Aliases must be an array",
|
|
|
|
combinedPageMeta.aliases,
|
|
|
|
"falling back to empty array",
|
|
|
|
);
|
|
|
|
combinedPageMeta.aliases = [];
|
|
|
|
}
|
|
|
|
|
2024-01-11 20:20:50 +08:00
|
|
|
updateITags(combinedPageMeta, frontmatter);
|
|
|
|
|
2024-08-24 18:35:09 +08:00
|
|
|
// Make sure this page is no (longer) in the aspiring pages list
|
|
|
|
await queryDeleteObjects<AspiringPageObject>("aspiring-page", {
|
|
|
|
filter: ["=", ["attr", "name"], ["string", name]],
|
|
|
|
});
|
2024-08-24 22:14:29 +08:00
|
|
|
|
|
|
|
const tagSchema = (await system.getSpaceConfig("schema")).tag;
|
|
|
|
// Validate the page meta against schemas, and only index the tags that validate
|
|
|
|
for (const tag of combinedPageMeta.tags) {
|
|
|
|
let schema = tagSchema[tag];
|
|
|
|
if (schema) {
|
|
|
|
schema = deepObjectMerge({ type: "object" }, schema);
|
|
|
|
const validationError = await jsonschema.validateObject(
|
|
|
|
schema,
|
|
|
|
combinedPageMeta,
|
|
|
|
);
|
|
|
|
if (validationError) {
|
|
|
|
console.warn(
|
|
|
|
"Validation failed for",
|
|
|
|
combinedPageMeta,
|
|
|
|
"for tag",
|
|
|
|
tag,
|
|
|
|
". Error:",
|
|
|
|
validationError,
|
|
|
|
". Removing tag until this is resolved.",
|
|
|
|
);
|
|
|
|
combinedPageMeta.tags.splice(combinedPageMeta.tags.indexOf(tag), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log("Page object", combinedPageMeta);
|
|
|
|
await indexObjects<PageMeta>(name, [combinedPageMeta]);
|
2023-08-28 23:12:15 +08:00
|
|
|
}
|
2023-11-21 23:24:20 +08:00
|
|
|
|
|
|
|
export async function lintFrontmatter(): Promise<LintDiagnostic[]> {
|
|
|
|
const text = await editor.getText();
|
|
|
|
const tree = await markdown.parseMarkdown(text);
|
|
|
|
const diagnostics: LintDiagnostic[] = [];
|
|
|
|
await traverseTreeAsync(tree, async (node) => {
|
|
|
|
if (node.type === "FrontMatterCode") {
|
|
|
|
const lintResult = await lintYaml(
|
|
|
|
renderToText(node),
|
|
|
|
node.from!,
|
|
|
|
node.to!,
|
|
|
|
);
|
|
|
|
if (lintResult) {
|
|
|
|
diagnostics.push(lintResult);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (node.type === "FencedCode") {
|
|
|
|
const codeInfo = findNodeOfType(node, "CodeInfo")!;
|
|
|
|
if (!codeInfo) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const codeLang = codeInfo.children![0].text!;
|
|
|
|
// All known YAML formats
|
|
|
|
if (
|
|
|
|
codeLang === "template" || codeLang === "yaml" ||
|
|
|
|
codeLang.startsWith("#")
|
|
|
|
) {
|
|
|
|
const codeText = findNodeOfType(node, "CodeText");
|
|
|
|
if (!codeText) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const yamlCode = renderToText(codeText);
|
|
|
|
const lintResult = await lintYaml(
|
|
|
|
yamlCode,
|
|
|
|
codeText.from!,
|
|
|
|
codeText.to!,
|
|
|
|
);
|
|
|
|
if (lintResult) {
|
|
|
|
diagnostics.push(lintResult);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
return diagnostics;
|
|
|
|
}
|
|
|
|
|
|
|
|
const errorRegex = /at line (\d+),? column (\d+)/;
|
|
|
|
|
|
|
|
async function lintYaml(
|
|
|
|
yamlText: string,
|
|
|
|
from: number,
|
|
|
|
to: number,
|
|
|
|
): Promise<LintDiagnostic | undefined> {
|
|
|
|
try {
|
|
|
|
await YAML.parse(yamlText);
|
2024-10-10 18:52:28 +08:00
|
|
|
} catch (e: any) {
|
2023-11-21 23:24:20 +08:00
|
|
|
const errorMatch = errorRegex.exec(e.message);
|
|
|
|
if (errorMatch) {
|
|
|
|
console.log("YAML error", e.message);
|
|
|
|
|
|
|
|
return {
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
severity: "error",
|
|
|
|
message: e.message,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|