@paroicms/site-generator-plugin
Version:
ParoiCMS Site Generator Plugin
48 lines (47 loc) • 1.7 kB
JavaScript
export function dedupMessages(messages) {
const counters = new Map();
const result = [];
for (const m of messages) {
const counter = counters.get(m);
if (counter) {
counters.set(m, counter + 1);
continue;
}
counters.set(m, 1);
result.push(m);
}
return result.map((m) => {
const counter = counters.get(m);
return counter && counter > 1 ? `${m} (×${counter})` : m;
});
}
/**
* Get the type names of taxonomy term documents.
* A term type is a regular document that is a child of a taxonomy routing document.
* A taxonomy routing document is identified by being referenced in a labeling field.
*/
export function getTermTypeNames(siteSchema) {
// First, collect all taxonomy routing document type names from labeling fields
const taxonomyRoutingTypeNames = new Set();
for (const nodeType of Object.values(siteSchema.nodeTypes)) {
if (!nodeType.fields)
continue;
for (const field of nodeType.fields) {
if (field.dataType === "labeling") {
taxonomyRoutingTypeNames.add(field.taxonomy);
}
}
}
// Then, find the regular children of those taxonomy routing documents - those are the term types
const termTypeNames = new Set();
for (const nodeType of Object.values(siteSchema.nodeTypes)) {
if (nodeType.kind !== "document")
continue;
if (!taxonomyRoutingTypeNames.has(nodeType.typeName))
continue;
for (const childTypeName of nodeType.regularChildren ?? []) {
termTypeNames.add(childTypeName);
}
}
return termTypeNames;
}