Skip to content

Commit

Permalink
Refactor to add eleventyImportCollections, fixes #975
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 16, 2022
1 parent a114ff0 commit 243539e
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 40 deletions.
146 changes: 106 additions & 40 deletions src/TemplateMap.js
Expand Up @@ -51,12 +51,19 @@ class TemplateMap {
return this._config;
}

get tagPrefix() {
static get tagPrefix() {
return "___TAG___";
}

get tagPrefix() {
return TemplateMap.tagPrefix;
}

async add(template) {
// This is where the data is first generated for the template
if (!template) {
return;
}

let data = await template.getData();

for (let map of await template.getTemplateMapEntries(data)) {
Expand Down Expand Up @@ -94,6 +101,43 @@ class TemplateMap {
}
}

addTagsToGraph(graph, inputPath, tags) {
if (!Array.isArray(tags)) {
return;
}
for (let tag of tags) {
let tagWithPrefix = this.tagPrefix + tag;
if (!graph.hasNode(tagWithPrefix)) {
graph.addNode(tagWithPrefix);
}

// Populates to collections.tagName
// Dependency from tag to inputPath
graph.addDependency(tagWithPrefix, inputPath);
}
}

addDeclaredDependenciesToGraph(graph, inputPath, deps) {
if (!Array.isArray(deps)) {
return;
}

for (let tag of deps) {
let tagWithPrefix = this.tagPrefix + tag;
if (!graph.hasNode(tagWithPrefix)) {
graph.addNode(tagWithPrefix);
}

// Dependency from inputPath to collection/tag
graph.addDependency(inputPath, tagWithPrefix);
}
}

// Exclude: Pagination templates consuming `collections` or `collections.all`
// Exclude: Pagination templates that consume config API collections

// Include: Pagination templates that don’t consume config API collections
// Include: Templates that don’t use Pagination
getMappedDependencies() {
let graph = new DependencyGraph();
let tagPrefix = this.tagPrefix;
Expand Down Expand Up @@ -125,27 +169,24 @@ class TemplateMap {
}

if (!entry.data.eleventyExcludeFromCollections) {
// collections.all
// Populates to collections.all
graph.addDependency(tagPrefix + "all", entry.inputPath);

if (entry.data.tags) {
for (let tag of entry.data.tags) {
let tagWithPrefix = tagPrefix + tag;
if (!graph.hasNode(tagWithPrefix)) {
graph.addNode(tagWithPrefix);
}

// collections.tagName
// Dependency from tag to inputPath
graph.addDependency(tagWithPrefix, entry.inputPath);
}
}
this.addTagsToGraph(graph, entry.inputPath, entry.data.tags);
}

this.addDeclaredDependenciesToGraph(
graph,
entry.inputPath,
entry.data.eleventyImportCollections
);
}

return graph.overallOrder();
}

// Exclude: Pagination templates consuming `collections` or `collections.all`
// Include: Pagination templates that consume config API collections
getDelayedMappedDependencies() {
let graph = new DependencyGraph();
let tagPrefix = this.tagPrefix;
Expand All @@ -157,7 +198,6 @@ class TemplateMap {
// Add tags from named user config collections
for (let tag of userConfigCollections) {
graph.addNode(tagPrefix + tag);
// graph.addDependency( tagPrefix + tag, tagPrefix + "all" );
}

for (let entry of this.map) {
Expand All @@ -176,27 +216,25 @@ class TemplateMap {
graph.addDependency(entry.inputPath, tagPrefix + paginationTagTarget);

if (!entry.data.eleventyExcludeFromCollections) {
// collections.all
// Populates into collections.all
graph.addDependency(tagPrefix + "all", entry.inputPath);

if (entry.data.tags) {
for (let tag of entry.data.tags) {
let tagWithPrefix = tagPrefix + tag;
if (!graph.hasNode(tagWithPrefix)) {
graph.addNode(tagWithPrefix);
}
// collections.tagName
// Dependency from tag to inputPath
graph.addDependency(tagWithPrefix, entry.inputPath);
}
}
this.addTagsToGraph(graph, entry.inputPath, entry.data.tags);
}

this.addDeclaredDependenciesToGraph(
graph,
entry.inputPath,
entry.data.eleventyImportCollections
);
}
}
let order = graph.overallOrder();
return order;

return graph.overallOrder();
}

// Exclude: Pagination templates consuming `collections.all`
// Include: Pagination templates consuming `collections`
getPaginatedOverCollectionsMappedDependencies() {
let graph = new DependencyGraph();
let tagPrefix = this.tagPrefix;
Expand All @@ -219,13 +257,23 @@ class TemplateMap {
if (!entry.data.eleventyExcludeFromCollections) {
// collections.all
graph.addDependency(tagPrefix + "all", entry.inputPath);

// Note that `tags` are otherwise ignored here
// TODO should we throw an error?
}

this.addDeclaredDependenciesToGraph(
graph,
entry.inputPath,
entry.data.eleventyImportCollections
);
}
}

return graph.overallOrder();
}

// Include: Pagination templates consuming `collections.all`
getPaginatedOverAllCollectionMappedDependencies() {
let graph = new DependencyGraph();
let tagPrefix = this.tagPrefix;
Expand All @@ -246,9 +294,19 @@ class TemplateMap {
}

if (!entry.data.eleventyExcludeFromCollections) {
// collections.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(
graph,
entry.inputPath,
entry.data.eleventyImportCollections
);
}
}

Expand Down Expand Up @@ -324,6 +382,15 @@ class TemplateMap {
}
}

getFullTemplateMapOrder() {
return [
this.getMappedDependencies(),
this.getDelayedMappedDependencies(),
this.getPaginatedOverCollectionsMappedDependencies(),
this.getPaginatedOverAllCollectionMappedDependencies(),
];
}

async cache() {
debug("Caching collections objects.");
this.collectionsData = {};
Expand All @@ -332,18 +399,16 @@ class TemplateMap {
entry.data.collections = this.collectionsData;
}

let dependencyMap = this.getMappedDependencies();
await this.initDependencyMap(dependencyMap);
let [
dependencyMap,
delayedDependencyMap,
firstPaginatedDepMap,
secondPaginatedDepMap,
] = this.getFullTemplateMapOrder();

let delayedDependencyMap = this.getDelayedMappedDependencies();
await this.initDependencyMap(dependencyMap);
await this.initDependencyMap(delayedDependencyMap);

let firstPaginatedDepMap =
this.getPaginatedOverCollectionsMappedDependencies();
await this.initDependencyMap(firstPaginatedDepMap);

let secondPaginatedDepMap =
this.getPaginatedOverAllCollectionMappedDependencies();
await this.initDependencyMap(secondPaginatedDepMap);

await this.resolveRemainingComputedData();
Expand Down Expand Up @@ -445,6 +510,7 @@ class TemplateMap {
}
}

// Filter out any tag nodes
getOrderedInputPaths(
dependencyMap,
delayedDependencyMap,
Expand Down
104 changes: 104 additions & 0 deletions test/_issues/975/975-test.js
@@ -0,0 +1,104 @@
const test = require("ava");
const TemplateMap = require("../../../src/TemplateMap");
const TemplateConfig = require("../../../src/TemplateConfig");

const getNewTemplateForTests = require("../../_getNewTemplateForTests");

function getNewTemplate(filename, input, output, eleventyConfig) {
return getNewTemplateForTests(
filename,
input,
output,
null,
null,
eleventyConfig
);
}

test("Get ordered list of templates", async (t) => {
let eleventyConfig = new TemplateConfig();
let tm = new TemplateMap(eleventyConfig);

// These two templates are add-order-dependent
await tm.add(
getNewTemplate(
"./test/_issues/975/post.md",
"./test/_issues/975/",
"./test/_issues/975/_site",
eleventyConfig
)
);

await tm.add(
getNewTemplate(
"./test/_issues/975/another-post.md",
"./test/_issues/975/",
"./test/_issues/975/_site",
eleventyConfig
)
);

// This template should always be last
await tm.add(
getNewTemplate(
"./test/_issues/975/index.md",
"./test/_issues/975/",
"./test/_issues/975/_site",
eleventyConfig
)
);

let [first, second, third, fourth] = tm.getFullTemplateMapOrder();
t.deepEqual(
first.filter((entry) => !entry.startsWith(TemplateMap.tagPrefix)),
[
"./test/_issues/975/post.md",
"./test/_issues/975/another-post.md",
"./test/_issues/975/index.md",
]
);
});

test("Get ordered list of templates (reverse add)", async (t) => {
let eleventyConfig = new TemplateConfig();
let tm = new TemplateMap(eleventyConfig);

// This template should always be last
await tm.add(
getNewTemplate(
"./test/_issues/975/index.md",
"./test/_issues/975/",
"./test/_issues/975/_site",
eleventyConfig
)
);

// These two templates are add-order-dependent
await tm.add(
getNewTemplate(
"./test/_issues/975/another-post.md",
"./test/_issues/975/",
"./test/_issues/975/_site",
eleventyConfig
)
);

await tm.add(
getNewTemplate(
"./test/_issues/975/post.md",
"./test/_issues/975/",
"./test/_issues/975/_site",
eleventyConfig
)
);

let [first, second, third, fourth] = tm.getFullTemplateMapOrder();
t.deepEqual(
first.filter((entry) => !entry.startsWith(TemplateMap.tagPrefix)),
[
"./test/_issues/975/another-post.md",
"./test/_issues/975/post.md",
"./test/_issues/975/index.md",
]
);
});
4 changes: 4 additions & 0 deletions test/_issues/975/another-post.md
@@ -0,0 +1,4 @@
---
tags:
- post
---
4 changes: 4 additions & 0 deletions test/_issues/975/index.md
@@ -0,0 +1,4 @@
---
eleventyImportCollections:
- post
---
4 changes: 4 additions & 0 deletions test/_issues/975/post.md
@@ -0,0 +1,4 @@
---
tags:
- post
---

0 comments on commit 243539e

Please sign in to comment.