Skip to content

Commit

Permalink
Fixes #2654
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Nov 19, 2022
1 parent 0c27a80 commit 650e583
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 25 deletions.
13 changes: 10 additions & 3 deletions src/TemplateLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,31 @@ class TemplateLayout extends TemplateContent {
return map;
}

async getLayoutChain() {
let map = await this.getTemplateLayoutMap();
let layoutChain = [];
for (let j = map.length - 1; j >= 0; j--) {
layoutChain.push(map[j].template.inputPath);
}
layoutChain.reverse();
return layoutChain;
}

async getData() {
if (this.dataCache) {
return this.dataCache;
}

let map = await this.getTemplateLayoutMap();
let dataToMerge = [];
let layoutChain = [];
for (let j = map.length - 1; j >= 0; j--) {
layoutChain.push(map[j].template.inputPath);
dataToMerge.push(map[j].frontMatterData);
}

// Deep merge of layout front matter
let data = TemplateData.mergeDeep(this.config, {}, ...dataToMerge);
delete data[this.config.keys.layout];

this.layoutChain = layoutChain.reverse();
this.dataCache = data;
return data;
}
Expand Down
29 changes: 19 additions & 10 deletions src/TemplateMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,10 @@ class TemplateMap {

this.checkForDuplicatePermalinks();

await this.config.events.emitLazy("eleventy.layouts", () =>
this.generateLayoutsMap()
);
await this.config.events.emitLazy("eleventy.layouts", () => {
// returns promise
return this.generateLayoutsMap();
});

await this.config.events.emitLazy("eleventy.serverlessUrlMap", () =>
this.generateServerlessUrlMap(orderedMap)
Expand Down Expand Up @@ -654,19 +655,27 @@ class TemplateMap {
return Promise.all(promises);
}

generateLayoutsMap() {
async generateLayoutsMap() {
let layouts = {};

for (let entry of this.map) {
for (let page of entry._pages) {
let tmpl = page.template;
let layout = page.data[this.config.keys.layout];
if (layout) {
let layoutFilePath = tmpl.getLayout(layout)?.inputPath;
if (!layouts[layoutFilePath]) {
layouts[layoutFilePath] = new Set();
let layoutKey = page.data[this.config.keys.layout];
if (layoutKey) {
let layout = tmpl.getLayout(layoutKey);
let layoutChain = await layout.getLayoutChain();
let priors = [];
for (let filepath of layoutChain) {
if (!layouts[filepath]) {
layouts[filepath] = new Set();
}
layouts[filepath].add(page.inputPath);
for (let prior of priors) {
layouts[filepath].add(prior);
}
priors.push(filepath);
}
layouts[layoutFilePath].add(page.inputPath);
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions src/Util/AsyncEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ class AsyncEventEmitter extends EventEmitter {
return [];
}

return this.emit.call(
this,
type,
...args.map((arg) => {
if (typeof arg === "function") {
return arg();
let argsMap = [];
for (let arg of args) {
if (typeof arg === "function") {
let r = arg();
if (r instanceof Promise) {
r = await r;
}
return arg;
})
);
argsMap.push(r);
} else {
argsMap.push(arg);
}
}

return this.emit.call(this, type, ...argsMap);
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/TemplateLayoutTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test("Get Layout Chain", async (t) => {

await tl.getData();

t.deepEqual(tl.layoutChain, [
t.deepEqual(await tl.getLayoutChain(), [
"./test/stubs/_includes/layouts/layout-inherit-a.njk",
"./test/stubs/_includes/layouts/layout-inherit-b.njk",
"./test/stubs/_includes/layouts/layout-inherit-c.njk",
Expand All @@ -75,7 +75,7 @@ test("Get Front Matter Data", async (t) => {
thirdinherits: "c",
});

t.deepEqual(tl.layoutChain, [
t.deepEqual(await tl.getLayoutChain(), [
"./test/stubs/_includes/layouts/layout-inherit-a.njk",
"./test/stubs/_includes/layouts/layout-inherit-b.njk",
"./test/stubs/_includes/layouts/layout-inherit-c.njk",
Expand Down
9 changes: 9 additions & 0 deletions test/TemplateMapTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,15 @@ test("eleventy.layouts Event", async (t) => {
"./test/stubs-layouts-event/_includes/first.liquid": [
"./test/stubs-layouts-event/page.md",
],
"./test/stubs-layouts-event/_includes/second.liquid": [
"./test/stubs-layouts-event/page.md",
"./test/stubs-layouts-event/_includes/first.liquid",
],
"./test/stubs-layouts-event/_includes/third.liquid": [
"./test/stubs-layouts-event/page.md",
"./test/stubs-layouts-event/_includes/first.liquid",
"./test/stubs-layouts-event/_includes/second.liquid",
],
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/TemplateTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,7 @@ test("Get Layout Chain", async (t) => {

await tmpl.getData();

t.deepEqual(tmpl._layout.layoutChain, [
t.deepEqual(await tmpl._layout.getLayoutChain(), [
"./test/stubs-incremental/layout-chain/_includes/base.njk",
"./test/stubs-incremental/layout-chain/_includes/parent.njk",
]);
Expand Down
3 changes: 3 additions & 0 deletions test/stubs-layouts-event/_includes/first.liquid
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
---
layout: second
---
{{ content }}
4 changes: 4 additions & 0 deletions test/stubs-layouts-event/_includes/second.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
layout: third.liquid
---
{{ content }}
1 change: 1 addition & 0 deletions test/stubs-layouts-event/_includes/third.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ content }}

0 comments on commit 650e583

Please sign in to comment.