UNPKG

@paroicms/site-generator-plugin

Version:

ParoiCMS Site Generator Plugin

72 lines (71 loc) 3 kB
export function getJtRoutingDocumentType(siteSchema, typeName) { const nodeType = siteSchema.nodeTypes?.find((nodeType) => nodeType.typeName === typeName); if (!nodeType) throw new Error(`Node type not found: "${typeName}"`); if (nodeType.kind !== "document" || nodeType.documentKind !== "routing") { throw new Error(`Invalid routing document: "${typeName}"`); } return nodeType; } export function getJtRegularDocumentType(siteSchema, typeName) { const nodeType = siteSchema.nodeTypes?.find((nodeType) => nodeType.typeName === typeName); if (!nodeType) throw new Error(`Node type not found: "${typeName}"`); if (nodeType.kind !== "document" || nodeType.documentKind !== "regular") { throw new Error(`Invalid regular document: "${typeName}"`); } return nodeType; } export function getJtPartType(siteSchema, typeName) { const nodeType = siteSchema.nodeTypes?.find((nodeType) => nodeType.typeName === typeName); if (!nodeType) throw new Error(`Node type not found: "${typeName}"`); if (nodeType.kind !== "part") throw new Error(`Invalid part type: "${typeName}"`); return nodeType; } export function hasTemporalChildren(siteSchema, documentType) { return (documentType.regularChildren?.some((childName) => { const child = getJtRegularDocumentType(siteSchema, childName); return child.route === ":yyyy/:mm/:dd/:relativeId-:slug"; }) ?? false); } export function getJtHomeType(siteSchema) { const homeType = siteSchema.nodeTypes?.find((type) => type.typeName === "home"); if (!homeType) throw new Error("Home type not found in site schema"); if (homeType.kind !== "document" || homeType.documentKind !== "routing") { throw new Error("Invalid home type in site schema"); } return homeType; } export function getJtSiteType(siteSchema) { const siteType = siteSchema.nodeTypes?.find((type) => type.kind === "site"); if (!siteType) throw new Error("Site type not found"); return siteType; } export function isMultilingual(siteSchema) { return siteSchema.languages.length > 1; } export function getFirstSiteLanguage(siteSchema) { if (siteSchema.languages.length === 0) throw new Error("Missing language in site schema"); return siteSchema.languages[0]; } export function getPossibleJtLabelingFieldTypes(siteSchema, possibleTypes) { const searchInTypes = possibleTypes ?? siteSchema.nodeTypes?.filter((type) => type.kind === "document" && type.documentKind === "regular") ?? []; const result = []; for (const documentType of searchInTypes) { for (const fieldType of documentType.fields ?? []) { if (typeof fieldType !== "string" && fieldType.storedAs === "labeling" && !result.find((alreadyThere) => alreadyThere.name === fieldType.name)) { result.push(fieldType); } } } return result; }