Skip to content

Commit

Permalink
Small cleanup for #1175 to take excluded collections into account whe…
Browse files Browse the repository at this point in the history
…n setting up TemplateMap
  • Loading branch information
zachleat committed Jun 26, 2024
1 parent fbad000 commit fa0b636
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
56 changes: 39 additions & 17 deletions src/Data/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -668,43 +686,47 @@ 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"];
}
}

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 [];
Expand Down
20 changes: 8 additions & 12 deletions src/TemplateMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit fa0b636

Please sign in to comment.