Skip to content

Commit

Permalink
Fixes #1754
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jun 2, 2021
1 parent 5e7bb68 commit f061d84
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 83 deletions.
46 changes: 20 additions & 26 deletions src/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const fs = require("fs-extra");
const parsePath = require("parse-filepath");
const normalize = require("normalize-path");
const isPlainObject = require("lodash/isPlainObject");
const lodashGet = require("lodash/get");
const { DateTime } = require("luxon");

const TemplateData = require("./TemplateData");
Expand All @@ -16,6 +15,7 @@ const Pagination = require("./Plugins/Pagination");
const TemplateContentPrematureUseError = require("./Errors/TemplateContentPrematureUseError");
const TemplateContentUnrenderedTemplateError = require("./Errors/TemplateContentUnrenderedTemplateError");
const ConsoleLogger = require("./Util/ConsoleLogger");
const TemplateBehavior = require("./TemplateBehavior");

const debug = require("debug")("Eleventy:Template");
const debugDev = require("debug")("Dev:Eleventy:Template");
Expand Down Expand Up @@ -62,6 +62,9 @@ class Template extends TemplateContent {
this.filePathStem = this.fileSlug.getFullPathWithoutExtension();

this.outputFormat = "fs";

this.behavior = new TemplateBehavior();
this.behavior.setOutputFormat(this.outputFormat);
}

get logger() {
Expand All @@ -79,6 +82,7 @@ class Template extends TemplateContent {

setOutputFormat(to) {
this.outputFormat = to;
this.behavior.setOutputFormat(to);
}

setIsVerbose(isVerbose) {
Expand Down Expand Up @@ -142,6 +146,9 @@ class Template extends TemplateContent {
permalinkValue,
this.extraOutputSubdirectory
);

this.behavior.setFromPermalink(perm);

return perm;
}

Expand All @@ -153,9 +160,8 @@ class Template extends TemplateContent {
let permalink = data[this.config.keys.permalink];
let permalinkValue;

// v1.0 added support for `permalink: true`
// `permalink: true` is a more accurate alias for `permalink: false` behavior:
// render but no file system write, e.g. use in collections only)
// `permalink: false` means render but no file system write, e.g. use in collections only)
// `permalink: true` throws an error
if (typeof permalink === "boolean") {
debugDev("Using boolean permalink %o", permalink);
permalinkValue = permalink;
Expand Down Expand Up @@ -573,20 +579,12 @@ class Template extends TemplateContent {
await this.computedData.processRemainingData(data);
}

async getTemplates(data, behavior) {
if (!behavior) {
behavior = {
read: true,
render: true,
write: true,
};
}

async getTemplates(data) {
// no pagination on permalink.serverless for local builds
let hasPagination = Pagination.hasPagination(data);
let isServerlessRenderOnBuild = !behavior.render;
let isServerlessRenderOnBuild = !this.behavior.isRenderable();
let isServerlessRenderOnServerless =
behavior.render === "override" &&
this.behavior.isRenderForced() &&
hasPagination &&
"serverless" in data.pagination;

Expand Down Expand Up @@ -624,7 +622,7 @@ class Template extends TemplateContent {
},
get templateContent() {
if (this._templateContent === undefined) {
if (behavior.render) {
if (this.template.behavior.isRenderable()) {
// should at least warn here
throw new TemplateContentPrematureUseError(
`Tried to use templateContent too early (${this.inputPath})`
Expand Down Expand Up @@ -672,7 +670,7 @@ class Template extends TemplateContent {
this._templateContent = content;
},
get templateContent() {
if (behavior.render) {
if (this.template.behavior.isRenderable()) {
if (this._templateContent === undefined) {
throw new TemplateContentPrematureUseError(
`Tried to use templateContent too early (${this.inputPath} page ${this.pageNumber})`
Expand Down Expand Up @@ -723,7 +721,8 @@ class Template extends TemplateContent {
};
}

let engineList = this.templateRender.getReadableEnginesListDifferingFromFileExtension();
let engineList =
this.templateRender.getReadableEnginesListDifferingFromFileExtension();
this.logger.log(
`${lang.start} ${outputPath} from ${this.inputPath}${
engineList ? ` (${engineList})` : ""
Expand Down Expand Up @@ -769,7 +768,7 @@ class Template extends TemplateContent {
let content;

// Note that behavior.render is overridden when using json or ndjson output
if (mapEntry.behavior.render) {
if (mapEntry.template.behavior.isRenderable()) {
// this reuses page.templateContent, it doesn’t render it
content = await this.renderPageEntry(mapEntry, page);
}
Expand All @@ -791,7 +790,7 @@ class Template extends TemplateContent {
return obj;
}

if (!mapEntry.behavior.render) {
if (!mapEntry.template.behavior.isRenderable()) {
debug(
"Template not written %o from %o (via permalink.behavior).",
page.outputPath,
Expand All @@ -800,7 +799,7 @@ class Template extends TemplateContent {
return;
}

if (!mapEntry.behavior.write) {
if (!mapEntry.template.behavior.isWriteable()) {
debug(
"Template not written %o from %o (via permalink: false, permalink.build: false, or a permalink object without a build property).",
page.outputPath,
Expand Down Expand Up @@ -926,17 +925,12 @@ class Template extends TemplateContent {
// Important reminder: This is where the template data is first generated via TemplateMap
let data = dataOverride || (await this.getData());

let rawPermalinkValue = data[this.config.keys.permalink];
let link = this._getRawPermalinkInstance(rawPermalinkValue);

let behavior = link.getBehavior(this.outputFormat);
let entries = [];
// does not return outputPath or url, we don’t want to render permalinks yet
entries.push({
template: this,
inputPath: this.inputPath,
data,
behavior,
});
return entries;
}
Expand Down
33 changes: 33 additions & 0 deletions src/TemplateBehavior.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class TemplateBehavior {
constructor() {
this.render = true;
this.write = true;
this.outputFormat = null;
}

isRenderable() {
return this.render || this.isRenderForced();
}

setOutputFormat(format) {
this.outputFormat = format;
}

isRenderForced() {
return this.outputFormat === "json" || this.outputFormat === "ndjson";
}

isWriteable() {
return this.write;
}

isIncludedInCollections() {
return this.isRenderable();
}

setFromPermalink(templatePermalink) {
this.render = templatePermalink._isRendered;
this.write = templatePermalink._writeToFileSystem;
}
}
module.exports = TemplateBehavior;
54 changes: 26 additions & 28 deletions src/TemplateMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class TemplateMap {

if (
!entry.data.eleventyExcludeFromCollections &&
entry.behavior.includeInCollections
entry.template.behavior.isIncludedInCollections()
) {
// collections.all
graph.addDependency(tagPrefix + "all", entry.inputPath);
Expand Down Expand Up @@ -177,7 +177,7 @@ class TemplateMap {

if (
!entry.data.eleventyExcludeFromCollections &&
entry.behavior.includeInCollections
entry.template.behavior.isIncludedInCollections()
) {
// collections.all
graph.addDependency(tagPrefix + "all", entry.inputPath);
Expand Down Expand Up @@ -221,7 +221,7 @@ class TemplateMap {

if (
!entry.data.eleventyExcludeFromCollections &&
entry.behavior.includeInCollections
entry.template.behavior.isIncludedInCollections()
) {
// collections.all
graph.addDependency(tagPrefix + "all", entry.inputPath);
Expand Down Expand Up @@ -253,7 +253,7 @@ class TemplateMap {

if (
!entry.data.eleventyExcludeFromCollections &&
entry.behavior.includeInCollections
entry.template.behavior.isIncludedInCollections()
) {
// collections.all
graph.addDependency(tagPrefix + "all", entry.inputPath);
Expand Down Expand Up @@ -297,33 +297,29 @@ class TemplateMap {
} else {
// is a template entry
let map = this.getMapEntryForInputPath(depEntry);
if (!map.behavior.read) {
map._pages = [];
} else {
map._pages = await map.template.getTemplates(map.data, map.behavior);
map._pages = await map.template.getTemplates(map.data);

let counter = 0;
for (let page of map._pages) {
// Copy outputPath to map entry
if (!map.outputPath) {
map.outputPath = page.outputPath;
}
let counter = 0;
for (let page of map._pages) {
// Copy outputPath to map entry
if (!map.outputPath) {
map.outputPath = page.outputPath;
}

if (
counter === 0 ||
(map.data.pagination &&
map.data.pagination.addAllPagesToCollections)
) {
if (
counter === 0 ||
(map.data.pagination &&
map.data.pagination.addAllPagesToCollections)
!map.data.eleventyExcludeFromCollections &&
map.template.behavior.isIncludedInCollections()
) {
if (
!map.data.eleventyExcludeFromCollections &&
map.behavior.includeInCollections
) {
// TODO do we need .template in collection entries?
this.collection.add(page);
}
// TODO do we need .template in collection entries?
this.collection.add(page);
}
counter++;
}
counter++;
}
}
}
Expand All @@ -343,10 +339,12 @@ class TemplateMap {
let delayedDependencyMap = this.getDelayedMappedDependencies();
await this.initDependencyMap(delayedDependencyMap);

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

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

await this.resolveRemainingComputedData();
Expand Down Expand Up @@ -444,7 +442,7 @@ class TemplateMap {
if (!map._pages) {
throw new Error(`Content pages not found for ${map.inputPath}`);
}
if (!map.behavior.render) {
if (!map.template.behavior.isRenderable()) {
continue;
}
try {
Expand Down
22 changes: 0 additions & 22 deletions src/TemplatePermalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class TemplatePermalink {
constructor(link, extraSubdir) {
let isLinkAnObject = isPlainObject(link);

this._isIgnoredTemplate = false;
this._isRendered = true;
this._writeToFileSystem = true;

Expand Down Expand Up @@ -54,10 +53,6 @@ class TemplatePermalink {
} else if (link.behavior === "read") {
this._writeToFileSystem = false;
this._isRendered = false;
} else if (link.behavior === "skip") {
this._writeToFileSystem = false;
this._isRendered = false;
this._isIgnoredTemplate = true;
}
}

Expand Down Expand Up @@ -128,23 +123,6 @@ class TemplatePermalink {
return normalize(uri);
}

getBehavior(outputFormat = "fs") {
let obj = {
read: !this._isIgnoredTemplate,
render: this._isRendered,
write: this._writeToFileSystem,
};

// override render behavior for --json or --ndjson
if (outputFormat !== "fs") {
obj.render = "override";
}

obj.includeInCollections = obj.read && obj.render;

return obj;
}

static _hasDuplicateFolder(dir, base) {
let folders = dir.split("/");
if (!folders[folders.length - 1]) {
Expand Down
Loading

0 comments on commit f061d84

Please sign in to comment.