Skip to content

Commit 5ada99b

Browse files
bug/issue 1475 page contents overriding layouts content when no outlet tag is being used (#1527)
1 parent f992744 commit 5ada99b

5 files changed

Lines changed: 76 additions & 10 deletions

File tree

packages/cli/src/lib/layout-utils.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,20 @@ async function mergeContentIntoLayout(
214214
outletType === "content"
215215
? /<content-outlet><\/content-outlet>/
216216
: /<page-outlet><\/page-outlet>/;
217+
// we need to make sure that if parent layouts don't have an "outlet" tag
218+
// then we _do not_ favor the child contents in that case
219+
// this can happen in the case of context plugins in which pages may _only_ be used for loading a layout
220+
// https://github.com/ProjectEvergreen/greenwood/pull/1527
217221
const finalBody =
218222
parentBody && parentBody.match(outletRegex)
219223
? parentBody.replace(outletRegex, childBody ?? childContents)
220-
: childRoot.querySelector("html") && childBody
221-
? childBody
222-
: !childRoot.querySelector("html")
223-
? childContents
224-
: "";
224+
: parentContents && outletType === "content"
225+
? parentBody
226+
: childRoot.querySelector("html") && childBody
227+
? childBody
228+
: !childRoot.querySelector("html")
229+
? childContents
230+
: "";
225231

226232
mergedContents = `<!DOCTYPE html>
227233
${mergedHtml}
@@ -271,7 +277,7 @@ async function getPageLayout(pageContents, compilation, matchingRoute, ssrLayout
271277
// has a custom layout from markdown frontmatter or context plugin
272278
layoutContents =
273279
customPluginPageLayouts.length > 0
274-
? await fs.readFile(new URL(`./${layout}.html`, customPluginPageLayouts[0]), "utf-8")
280+
? await fs.readFile(customPluginPageLayouts[0], "utf-8")
275281
: await fs.readFile(new URL(`./${layout}.html`, userLayoutsDir), "utf-8");
276282
} else if (customPluginDefaultPageLayouts.length > 0 || (!is404Page && hasPageLayout)) {
277283
// has a dynamic default page layout from context plugin

packages/cli/test/cases/build.plugins.context/build.plugins.context.spec.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* pages/
1717
* slides/
1818
* index.md
19+
* about-me.md
1920
* index.md
2021
*/
2122
import chai from "chai";
@@ -65,7 +66,7 @@ describe("Build Greenwood With: ", function () {
6566

6667
runSmokeTest(["public", "index"], LABEL);
6768

68-
describe("Custom Default App and Page Layout", function () {
69+
describe("App and Custom Index Page Layouts for the home page", function () {
6970
let dom;
7071

7172
before(async function () {
@@ -80,6 +81,43 @@ describe("Build Greenwood With: ", function () {
8081
);
8182
});
8283

84+
it("should have expected <body> content from the index layout", function () {
85+
const customElement = dom.window.document.querySelectorAll("body presenter-mode");
86+
87+
expect(customElement.length).to.equal(1);
88+
});
89+
90+
it("should have expected <head> content from the index layout", function () {
91+
const scriptTags = dom.window.document.querySelectorAll("head script");
92+
const tags = Array.from(scriptTags).filter((tag) => tag.getAttribute("type") === "module");
93+
94+
expect(tags.length).to.equal(1);
95+
});
96+
97+
it("should NOT have any content from the index page since index layout has no <content-outlet></content-outlet> tag", function () {
98+
const h3 = dom.window.document.querySelectorAll("body h3");
99+
const h4 = dom.window.document.querySelectorAll("body h3");
100+
101+
expect(h3.length).to.equal(0);
102+
expect(h4.length).to.equal(0);
103+
});
104+
});
105+
106+
describe("Default App and Page Layouts for the about page", function () {
107+
let dom;
108+
109+
before(async function () {
110+
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, "about-me/index.html"));
111+
});
112+
113+
it("should have expected text from from a mock package layout/app.html in node_modules/", function () {
114+
const pageLayoutHeading = dom.window.document.querySelectorAll("body h1")[0];
115+
116+
expect(pageLayoutHeading.textContent).to.be.equal(
117+
"This is a custom app layout from the custom layouts directory.",
118+
);
119+
});
120+
83121
it("should have expected text from from a mock package layout/page.html in node_modules/", function () {
84122
const pageLayoutHeading = dom.window.document.querySelectorAll("body h2")[0];
85123

@@ -90,10 +128,10 @@ describe("Build Greenwood With: ", function () {
90128

91129
it("should have expected text from user workspace pages/index.md", function () {
92130
const pageHeadingPrimary = dom.window.document.querySelectorAll("body h3")[0];
93-
const pageHeadingSecondary = dom.window.document.querySelectorAll("body h4")[0];
131+
const pageHeadingSecondary = dom.window.document.querySelectorAll("body p")[0];
94132

95-
expect(pageHeadingPrimary.textContent).to.be.equal("Context Plugin Theme Pack Test");
96-
expect(pageHeadingSecondary.textContent).to.be.equal("From user workspace pages/index.md");
133+
expect(pageHeadingPrimary.textContent).to.be.equal("About Me");
134+
expect(pageHeadingSecondary.textContent).to.be.equal("Hello from me!");
97135
});
98136
});
99137

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<script type="module">
5+
document.addEventListener("slide-selected", (event) => {
6+
document.querySelector("slide-viewer").setAttribute("slide", JSON.stringify(event.detail));
7+
});
8+
</script>
9+
</head>
10+
<body>
11+
<main>
12+
<presenter-mode></presenter-mode>
13+
</main>
14+
</body>
15+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### About Me
2+
3+
Hello from me!
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
layout: index
3+
---
4+
15
### Context Plugin Theme Pack Test
26

37
#### From user workspace pages/index.md

0 commit comments

Comments
 (0)