UNPKG

@paroicms/site-generator-plugin

Version:

ParoiCMS Site Generator Plugin

57 lines (56 loc) 2.83 kB
import { isDef } from "@paroicms/public-anywhere-lib"; import { camelToKebabCase } from "../lib/utils.js"; import { templateOfDocumentCard } from "./document-card-template-creator.js"; export function templatesOfLabeledList(ctx, documentType) { const { addLiquidFile } = ctx; const labeledByUs = ctx.labeledDocuments.filter((doc) => { const parentTypes = ctx.getParentTypes(documentType); return parentTypes.some((parentType) => parentType.typeName === doc.taxonomyType.typeName); }); return labeledByUs .map((labeledDocument) => { const parentRoutingKey = getRoutingKeyInCluster(ctx, labeledDocument.parentDocumentType, { sameClusterAs: labeledDocument.taxonomyType, }); if (!parentRoutingKey) return undefined; const labeledListKey = `labeled${upperCaseFirstLetter(labeledDocument.parentDocumentType.typeName)}`; const cardTemplateName = `${camelToKebabCase(labeledDocument.documentType.typeName)}-card.public`; addLiquidFile("partials", `${cardTemplateName}.liquid`, templateOfDocumentCard(ctx, "doc", { parentType: labeledDocument.parentDocumentType }), { skipIfExists: true }); return `{% set ${labeledListKey} = paginatedDocs(${parentRoutingKey}.children, by: 10, term: doc, labeledWith: "${labeledDocument.field.name}") %} <div class="Container"> {% out infiniteLoading(class: "Page List", paginatedDocs: ${labeledListKey}, template: "partials/${cardTemplateName}") %} </div>`; }) .filter(isDef); } function upperCaseFirstLetter(str) { return `${str.charAt(0).toUpperCase()}${str.slice(1)}`; } function getRoutingKeyInCluster(ctx, documentType, options) { const { siteSchema } = ctx; const { nodeTypes } = siteSchema; if (!nodeTypes) return; const getNodeType = (typeName) => nodeTypes.find((nt) => nt.typeName === typeName); function findPath(target, current, parentPath) { const currentPath = [...parentPath, current]; if (current === target) return currentPath; const node = getNodeType(current); if (!node || node.kind !== "document" || !node.routingChildren) return; for (const childName of node.routingChildren) { const found = findPath(target, childName, currentPath); if (found) return found; } } const clusterRootTypeName = "home"; // limitation here: only works for the "home" cluster const targetPath = findPath(documentType.typeName, clusterRootTypeName, []); const sameClusterPath = findPath(options.sameClusterAs.typeName, clusterRootTypeName, []); if (!targetPath || !sameClusterPath) return; targetPath.shift(); // remove leading 'home' return `doc.cluster.routing.${targetPath.join(".")}`; }