From fa0b636a17f50a43d1fa5945c003f1bde4e9f4c0 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Wed, 26 Jun 2024 14:50:10 -0500 Subject: [PATCH] Small cleanup for #1175 to take excluded collections into account when setting up TemplateMap --- src/Data/TemplateData.js | 56 ++++++++++++++++++++++++++++------------ src/TemplateMap.js | 20 ++++++-------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/Data/TemplateData.js b/src/Data/TemplateData.js index 21e5f9a26..273fbf043 100644 --- a/src/Data/TemplateData.js +++ b/src/Data/TemplateData.js @@ -651,6 +651,24 @@ class TemplateData { return Merge(target, ...source); } + /* Like cleanupData() but does not mutate */ + static getCleanedTagsImmutable(data) { + let tags = []; + + if (isPlainObject(data) && data.tags) { + if (typeof data.tags === "string") { + tags = (data.tags || "").split(","); + } else if (Array.isArray(data.tags)) { + tags = data.tags; + } + + // Deduplicate tags + return [...new Set(tags)]; + } + + return tags; + } + static cleanupData(data) { if (isPlainObject(data) && "tags" in data) { if (typeof data.tags === "string") { @@ -668,30 +686,32 @@ class TemplateData { static getNormalizedExcludedCollections(data) { let excludes = []; - if ("eleventyExcludeFromCollections" in data) { - if (data.eleventyExcludeFromCollections !== true) { - if (Array.isArray(data.eleventyExcludeFromCollections)) { - excludes = data.eleventyExcludeFromCollections; - } else if (typeof data.eleventyExcludeFromCollections === "string") { - excludes = [data.eleventyExcludeFromCollections]; - } + let key = "eleventyExcludeFromCollections"; + + if (data?.[key] !== true) { + if (Array.isArray(data[key])) { + excludes = data[key]; + } else if (typeof data[key] === "string") { + excludes = (data[key] || "").split(","); } } + return { excludes, - excludeAll: data.eleventyExcludeFromCollections === true, + excludeAll: data?.eleventyExcludeFromCollections === true, }; } + /* Same as getIncludedTagNames() but may also include "all" */ static getIncludedCollectionNames(data) { - TemplateData.cleanupData(data); + let tags = TemplateData.getCleanedTagsImmutable(data); - if ("tags" in data) { - let excludes = TemplateData.getNormalizedExcludedCollections(data); - if (excludes.excludeAll) { + if (tags.length > 0) { + let { excludes, excludeAll } = TemplateData.getNormalizedExcludedCollections(data); + if (excludeAll) { return []; } else { - return ["all", ...data.tags].filter((tag) => !excludes.excludes.includes(tag)); + return ["all", ...tags].filter((tag) => !excludes.includes(tag)); } } else { return ["all"]; @@ -699,12 +719,14 @@ class TemplateData { } static getIncludedTagNames(data) { - if ("tags" in data) { - let excludes = TemplateData.getNormalizedExcludedCollections(data); - if (excludes.excludeAll) { + let tags = TemplateData.getCleanedTagsImmutable(data); + + if (tags.length > 0) { + let { excludes, excludeAll } = TemplateData.getNormalizedExcludedCollections(data); + if (excludeAll) { return []; } else { - return data.tags.filter((tag) => !excludes.excludes.includes(tag)); + return tags.filter((tag) => !excludes.includes(tag)); } } else { return []; diff --git a/src/TemplateMap.js b/src/TemplateMap.js index 618baf10c..bbdd75c73 100644 --- a/src/TemplateMap.js +++ b/src/TemplateMap.js @@ -241,12 +241,12 @@ class TemplateMap { graph.addNode(entry.inputPath); } - if (!entry.data.eleventyExcludeFromCollections) { + let collectionNames = TemplateData.getIncludedCollectionNames(entry.data); + if (collectionNames.includes("all")) { // collections.all graph.addDependency(tagPrefix + "all", entry.inputPath); // Note that `tags` are otherwise ignored here - // TODO should we throw an error? } this.addDeclaredDependenciesToGraph( @@ -280,13 +280,13 @@ class TemplateMap { graph.addNode(entry.inputPath); } - if (!entry.data.eleventyExcludeFromCollections) { + let collectionNames = TemplateData.getIncludedCollectionNames(entry.data); + if (collectionNames.includes("all")) { // Populates into collections.all // This is circular! graph.addDependency(tagPrefix + "all", entry.inputPath); // Note that `tags` are otherwise ignored here - // TODO should we throw an error? } this.addDeclaredDependenciesToGraph( @@ -320,14 +320,9 @@ class TemplateMap { this.config.uses.addDependencyConsumesCollection(entry.inputPath, paginationTagTarget); } - if (!entry.data.eleventyExcludeFromCollections) { - this.config.uses.addDependencyPublishesToCollection(entry.inputPath, "all"); - - if (Array.isArray(entry.data.tags)) { - for (let tag of entry.data.tags) { - this.config.uses.addDependencyPublishesToCollection(entry.inputPath, tag); - } - } + let collectionNames = TemplateData.getIncludedCollectionNames(entry.data); + for (let name of collectionNames) { + this.config.uses.addDependencyPublishesToCollection(entry.inputPath, name); } if (Array.isArray(entry.data.eleventyImport?.collections)) { @@ -400,6 +395,7 @@ class TemplateMap { if (counter === 0 || map.data.pagination?.addAllPagesToCollections) { if (map.data.eleventyExcludeFromCollections !== true) { + // is in *some* collections this.collection.add(page); } }