@paroicms/site-generator-plugin
Version:
ParoiCMS Site Generator Plugin
58 lines (57 loc) • 2.19 kB
JavaScript
export async function updateLNodesWithTaxonomies(ctx, options) {
const { service } = ctx;
const { siteSchema, idPicker, fqdn } = options;
const { nodeTypes } = siteSchema;
for (const nodeType of Object.values(nodeTypes)) {
const labelingFields = nodeType.fields?.filter((f) => f.dataType === "labeling");
if (!labelingFields || labelingFields.length === 0)
continue;
await updateLabelingFields(ctx, {
idPicker,
fqdn,
nodeType,
labelingFields,
}, service);
}
}
async function updateLabelingFields(ctx, options, service) {
const { fqdn, nodeType, labelingFields, idPicker } = options;
if (nodeType.kind !== "document" && nodeType.kind !== "part")
return;
const nodeIds = idPicker.pickNodeIds({ typeName: nodeType.typeName });
for (const nodeId of nodeIds) {
const fieldValues = [];
for (const field of labelingFields) {
const taxonomyIds = idPicker.pickNodeIds({ typeName: field.taxonomy });
if (taxonomyIds.length !== 1) {
if (taxonomyIds.length > 1) {
ctx.logger.warn(`Expected one taxonomy ID for "${field.taxonomy}", got ${taxonomyIds.length}`);
}
continue;
}
const taxonomyNodeId = taxonomyIds[0];
const max = field.multiple ? Math.floor(Math.random() * 2) + 1 : 1;
const termNodeIds = idPicker.pickNodeIds({ parentNodeId: taxonomyNodeId }, max);
if (termNodeIds.length === 0)
continue;
fieldValues.push([
field.name,
{
dataType: "labeling",
localized: false,
value: { t: termNodeIds },
},
]);
}
if (fieldValues.length === 0)
continue;
await service.connector.updateNodeContent(fqdn, {
nodeId,
content: {
kind: nodeType.kind,
typeName: nodeType.typeName,
fields: Object.fromEntries(fieldValues),
},
});
}
}