@paroicms/site-generator-plugin
Version:
ParoiCMS Site Generator Plugin
70 lines (69 loc) • 2.29 kB
JavaScript
// Default authors/author types (injected by site-schema-factory at runtime, but needed for template generation)
const defaultAuthorsType = {
typeName: "authors",
kind: "document",
documentKind: "routing",
route: "authors",
regularChildren: ["author"],
regularChildrenSorting: "title asc",
};
const defaultAuthorType = {
typeName: "author",
kind: "document",
documentKind: "regular",
route: ":relativeId-:slug",
autoPublish: true,
};
/**
* Create a copy of the site schema with authors/author types injected if missing.
* This is needed because the site-schema-factory injects these at runtime,
* but we need them during template generation.
*/
export function createSchemaWithAuthors(siteSchema) {
const nodeTypes = siteSchema.nodeTypes ?? [];
const nodeTypeNames = new Set(nodeTypes.map((nt) => nt.typeName));
const hasAuthors = nodeTypeNames.has("authors");
const hasAuthor = nodeTypeNames.has("author");
// If both authors and author exist, check if home references authors
if (hasAuthors && hasAuthor) {
return ensureHomeReferencesAuthors(siteSchema);
}
const newNodeTypes = [...nodeTypes];
// Add author first (authors depends on it)
if (!hasAuthor) {
newNodeTypes.push(defaultAuthorType);
}
// Add authors
if (!hasAuthors) {
newNodeTypes.push(defaultAuthorsType);
}
return ensureHomeReferencesAuthors({
...siteSchema,
nodeTypes: newNodeTypes,
});
}
/**
* Ensure the home document type has "authors" in its routingChildren.
*/
function ensureHomeReferencesAuthors(siteSchema) {
const nodeTypes = siteSchema.nodeTypes ?? [];
const homeIndex = nodeTypes.findIndex((nt) => nt.typeName === "home");
if (homeIndex === -1) {
return siteSchema;
}
const homeType = nodeTypes[homeIndex];
const routingChildren = homeType.routingChildren ?? [];
if (routingChildren.includes("authors")) {
return siteSchema;
}
// Create a new array with updated home type
const newNodeTypes = [...nodeTypes];
newNodeTypes[homeIndex] = {
...homeType,
routingChildren: [...routingChildren, "authors"],
};
return {
...siteSchema,
nodeTypes: newNodeTypes,
};
}