Skip to content

Commit 99ef9b5

Browse files
committed
Fixes 11ty#3850
1 parent 4a0f5f2 commit 99ef9b5

2 files changed

Lines changed: 50 additions & 20 deletions

File tree

src/Template.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -534,29 +534,43 @@ class Template extends TemplateContent {
534534
});
535535
}
536536

537+
async #renderComputedUnit(entry, data) {
538+
if (typeof entry === "string") {
539+
return this.renderComputedData(entry, data);
540+
}
541+
542+
if (isPlainObject(entry)) {
543+
for (let key in entry) {
544+
entry[key] = await this.#renderComputedUnit(entry[key], data);
545+
}
546+
}
547+
548+
if (Array.isArray(entry)) {
549+
for (let j = 0, k = entry.length; j < k; j++) {
550+
entry[j] = await this.#renderComputedUnit(entry[j], data);
551+
}
552+
}
553+
554+
return entry;
555+
}
556+
537557
_addComputedEntry(computedData, obj, parentKey, declaredDependencies) {
538558
// this check must come before isPlainObject
539559
if (typeof obj === "function") {
540560
computedData.add(parentKey, obj, declaredDependencies);
541-
} else if (Array.isArray(obj)) {
542-
// Arrays are treated as one entry in the dependency graph now
561+
} else if (Array.isArray(obj) || typeof obj === "string") {
562+
// Arrays are treated as one entry in the dependency graph now, Issue #3728
543563
computedData.addTemplateString(
544564
parentKey,
545565
async function (innerData) {
546-
return Promise.all(
547-
obj.map((entry) => {
548-
if (typeof entry === "string") {
549-
return this.tmpl.renderComputedData(entry, innerData);
550-
}
551-
return entry;
552-
}),
553-
);
566+
return this.tmpl.#renderComputedUnit(obj, innerData);
554567
},
555568
declaredDependencies,
556569
this.getParseForSymbolsFunction(obj),
557570
this,
558571
);
559572
} else if (isPlainObject(obj)) {
573+
// Arrays used to be computed here
560574
for (let key in obj) {
561575
let keys = [];
562576
if (parentKey) {
@@ -565,16 +579,6 @@ class Template extends TemplateContent {
565579
keys.push(key);
566580
this._addComputedEntry(computedData, obj[key], keys.join("."), declaredDependencies);
567581
}
568-
} else if (typeof obj === "string") {
569-
computedData.addTemplateString(
570-
parentKey,
571-
async function (innerData) {
572-
return this.tmpl.renderComputedData(obj, innerData);
573-
},
574-
declaredDependencies,
575-
this.getParseForSymbolsFunction(obj),
576-
this,
577-
);
578582
} else {
579583
// Numbers, booleans, etc
580584
computedData.add(parentKey, obj, declaredDependencies);

test/Issue3850Test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import test from "ava";
2+
import Eleventy from "../src/Eleventy.js";
3+
4+
test("#3850 Computed Data regression part 2", async (t) => {
5+
let elev = new Eleventy("test/noop", false, {
6+
config(eleventyConfig) {
7+
eleventyConfig.addTemplate("index.njk", `---
8+
site:
9+
download_link_mac: "http://example.com/"
10+
eleventyComputed:
11+
downloads:
12+
-
13+
links:
14+
-
15+
url: "{{ site.download_link_mac }}"
16+
---
17+
{{ site.download_link_mac }}:::{{ downloads | dump | safe }}`);
18+
}
19+
});
20+
21+
let results = await elev.toJSON();
22+
results.sort();
23+
24+
t.is(results.length, 1);
25+
t.is(results[0]?.content.trim(), `http://example.com/:::[{"links":[{"url":"http://example.com/"}]}]`)
26+
});

0 commit comments

Comments
 (0)