UNPKG

@paroicms/site-generator-plugin

Version:

ParoiCMS Site Generator Plugin

119 lines (118 loc) 4.91 kB
import { getPredefinedFields } from "../lib/create-prompt.js"; export function createThemeCreatorContext(siteSchema) { const languages = siteSchema.languages ?? []; const liquidFiles = new Map(); const localeFiles = new Map(); const otherFiles = new Map(); const issues = []; const getParentTypes = makeGetParentTypes(siteSchema); const predefinedFieldsWithFieldName = getPredefinedFields().map((f) => ({ ...f, fieldName: extractFieldName(f.qualifiedFieldName), })); const predefinedFields = new Map(predefinedFieldsWithFieldName.map((f) => [f.qualifiedFieldName, f])); return { siteSchema, labeledDocuments: getLabeledDocuments(siteSchema, getParentTypes), getParentTypes, predefinedFields, setLocalizedLabel(label) { for (const language of languages) { const value = label[language]; if (!value) continue; let f = localeFiles.get(language); if (!f) { f = {}; localeFiles.set(language, f); } f[language] = value; } }, hasLiquidFile(directory, filename) { const path = directory === "root" ? `templates/${filename}` : `templates/${directory}/${filename}`; return liquidFiles.has(path); }, addLiquidFile(directory, filename, content, { skipIfExists = false } = {}) { const path = directory === "root" ? `templates/${filename}` : `templates/${directory}/${filename}`; if (liquidFiles.has(path)) { if (skipIfExists) return; issues.push(`Liquid file already exists, overwrite: "${path}"`); } liquidFiles.set(path, content); }, addFile(path, content) { if (otherFiles.has(path)) throw new Error(`File already exists: "${path}"`); otherFiles.set(path, content); }, toFiles() { const files = [ ...Array.from(liquidFiles.entries()).map(([path, content]) => ({ path, content })), ...Array.from(localeFiles.entries()).map(([language, content]) => ({ path: `locales/${language}.json`, content: JSON.stringify(content, null, 2), })), ...Array.from(otherFiles.entries()).map(([path, content]) => ({ path, content })), ]; files.sort((a, b) => a.path.localeCompare(b.path)); return { files, issues: issues.length > 0 ? issues : undefined }; }, }; } function makeGetParentTypes(siteSchema) { const parentsByChild = new Map(); const nodeTypes = siteSchema.nodeTypes ?? []; for (const type of nodeTypes) { if (type.kind !== "document") continue; if (type.documentKind === "routing") { for (const childName of type.routingChildren ?? []) { const list = parentsByChild.get(childName) ?? []; list.push(type); parentsByChild.set(childName, list); } } for (const childName of type.regularChildren ?? []) { const list = parentsByChild.get(childName) ?? []; list.push(type); parentsByChild.set(childName, list); } } return (documentType) => parentsByChild.get(documentType.typeName) ?? []; } function getLabeledDocuments(siteSchema, getParentTypes) { const result = []; for (const documentType of siteSchema.nodeTypes ?? []) { if (documentType.kind !== "document" || documentType.documentKind !== "regular") continue; for (const field of documentType.fields ?? []) { if (typeof field === "string" || field.storedAs !== "labeling") continue; const taxonomyType = siteSchema.nodeTypes?.find((nt) => nt.typeName === field.taxonomy); if (!taxonomyType || taxonomyType.kind !== "document" || taxonomyType.documentKind !== "routing") { continue; } for (const parentDocumentType of getParentTypes(documentType)) { if (parentDocumentType.kind !== "document" || parentDocumentType.documentKind !== "routing") { continue; } result.push({ documentType, parentDocumentType, field, taxonomyType, }); } } } return result; } function extractFieldName(qualifiedFieldName) { const bracketIndex = qualifiedFieldName.indexOf("["); return bracketIndex === -1 ? qualifiedFieldName : qualifiedFieldName.slice(0, bracketIndex); }