Avoid builtin page attributes to be overridden

This commit is contained in:
Zef Hemel
2023-12-22 11:27:07 +01:00
parent 8577fb95db
commit 82391682f6
11 changed files with 38 additions and 48 deletions
+5 -1
View File
@@ -192,7 +192,11 @@ export async function objectSourceProvider({
}
export async function discoverSources() {
return (await datastore.query({ prefix: [indexKey, "tag"] })).map((
return (await datastore.query({
prefix: [indexKey, "tag"],
select: [{ name: "name" }],
distinct: true,
})).map((
{ value },
) => value.name);
}
+1
View File
@@ -48,6 +48,7 @@ export const builtins: Record<string, Record<string, string>> = {
attributeType: "!string",
type: "!string",
page: "!string",
readOnly: "!boolean",
},
anchor: {
ref: "!string",
+5 -5
View File
@@ -13,11 +13,12 @@ import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
export async function lintYAML({ tree }: LintEvent): Promise<LintDiagnostic[]> {
const diagnostics: LintDiagnostic[] = [];
const frontmatter = await extractFrontmatter(tree);
const tags = ["page", ...frontmatter.tags || []];
// Query all readOnly attributes for pages with this tag set
const readOnlyAttributes = await queryObjects<AttributeObject>("attribute", {
filter: ["and", ["=", ["attr", "tag"], [
"array",
frontmatter.tags.map((tag): QueryExpression => ["string", tag]),
tags.map((tag): QueryExpression => ["string", tag]),
]], [
"=",
["attr", "readOnly"],
@@ -26,7 +27,6 @@ export async function lintYAML({ tree }: LintEvent): Promise<LintDiagnostic[]> {
distinct: true,
select: [{ name: "name" }],
});
// console.log("All read only attributes", readOnlyAttributes);
await traverseTreeAsync(tree, async (node) => {
if (node.type === "FrontMatterCode") {
const lintResult = await lintYaml(
@@ -75,17 +75,17 @@ const errorRegex = /\((\d+):(\d+)\)/;
async function lintYaml(
yamlText: string,
from: number,
disallowedKeys: string[] = [],
readOnlyKeys: string[] = [],
): Promise<LintDiagnostic | undefined> {
try {
const parsed = await YAML.parse(yamlText);
for (const key of disallowedKeys) {
for (const key of readOnlyKeys) {
if (parsed[key]) {
return {
from,
to: from + yamlText.length,
severity: "error",
message: `Disallowed key "${key}"`,
message: `Cannot set read-only attribute "${key}"`,
};
}
}
+3 -1
View File
@@ -22,7 +22,9 @@ export async function indexPage({ name, tree }: IndexTreeEvent) {
const toplevelAttributes = await extractAttributes(tree, false);
// Push them all into the page object
pageMeta = { ...pageMeta, ...frontmatter, ...toplevelAttributes };
// 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'
pageMeta = { ...frontmatter, ...toplevelAttributes, ...pageMeta };
pageMeta.tags = [...new Set(["page", ...pageMeta.tags || []])];
+2
View File
@@ -167,5 +167,7 @@ async function allQuerySources(): Promise<string[]> {
const allObjectTypes: string[] = (await events.dispatchEvent("query_", {}))
.flat();
// console.log("All object types", allObjectTypes);
return [...allSources, ...allObjectTypes];
}