Skip to content

Commit f992744

Browse files
bug/issue 1475 reconcile page level custom title and layout <title> merging when using active content (#1526)
1 parent a0c35ae commit f992744

File tree

8 files changed

+51
-13
lines changed

8 files changed

+51
-13
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,16 @@ async function mergeContentIntoLayout(
111111
: parentTitle.rawText;
112112
title = text.replace(activeFrontmatterTitleKey, matchingRoute.title || matchingRoute.label);
113113
} else {
114-
title = matchingRoute.title
115-
? matchingRoute.title
116-
: childTitle && childTitle.rawText
117-
? childTitle.rawText
118-
: parentTitle && parentTitle.rawText
119-
? parentTitle.rawText
120-
: "";
114+
// we favor frontmatter title for the page
115+
// otherwise we defer to page layouts and then ultimately the app layout
116+
title =
117+
outletType === "content" && matchingRoute?.title
118+
? matchingRoute.title
119+
: childTitle && childTitle.rawText
120+
? childTitle.rawText
121+
: parentTitle && parentTitle.rawText
122+
? parentTitle.rawText
123+
: "";
121124
}
122125

123126
const mergedHtml =

packages/cli/test/cases/build.config.active-frontmatter/build.config.active-frontmatter.spec.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { JSDOM } from "jsdom";
2727
import path from "node:path";
2828
import chai from "chai";
2929
import { getOutputTeardownFiles } from "../../../../../test/utils.js";
30+
import { runSmokeTest } from "../../../../../test/smoke-test.js";
3031
import { Runner } from "gallinago";
3132
import { fileURLToPath } from "node:url";
3233

@@ -51,6 +52,8 @@ describe("Build Greenwood With: ", function () {
5152
runner.runCommand(cliPath, "build");
5253
});
5354

55+
runSmokeTest(["public", "index"], LABEL);
56+
5457
describe("Default Greenwood frontmatter should be interpolated in the correct places for the home page", function () {
5558
let dom;
5659

@@ -83,7 +86,7 @@ describe("Build Greenwood With: ", function () {
8386
});
8487
});
8588

86-
describe("Simple frontmatter should be interpolated in the correct places", function () {
89+
describe("Simple active frontmatter should be interpolated in the correct places for the first blog post page", function () {
8790
let dom;
8891

8992
before(async function () {
@@ -95,7 +98,7 @@ describe("Build Greenwood With: ", function () {
9598
it("should have the correct value for the <title> tag in the <head> for the first post page", function () {
9699
const title = dom.window.document.querySelector("head title").textContent;
97100

98-
expect(title).to.be.equal("My First Post");
101+
expect(title).to.be.equal("My Blog - My First Post");
99102
});
100103

101104
it("should have the correct value for author <meta> tag in the <head>", function () {
@@ -119,7 +122,7 @@ describe("Build Greenwood With: ", function () {
119122
});
120123
});
121124

122-
describe("Rich frontmatter should be interpolated in the correct places", function () {
125+
describe("Rich active frontmatter should be interpolated in the correct places for the second blog post page", function () {
123126
let dom;
124127

125128
before(async function () {
@@ -131,7 +134,7 @@ describe("Build Greenwood With: ", function () {
131134
it("should have the correct value for the <title> tag in the <head> for second post page", function () {
132135
const title = dom.window.document.querySelector("head title").textContent;
133136

134-
expect(title).to.be.equal("My Second Post");
137+
expect(title).to.be.equal("My Blog - My Second Post");
135138
});
136139

137140
it("should have the correct songs frontmatter data in the page output", function () {
@@ -148,6 +151,26 @@ describe("Build Greenwood With: ", function () {
148151
});
149152
});
150153
});
154+
155+
describe("Page level custom title for the about page", function () {
156+
let dom;
157+
158+
before(async function () {
159+
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, "./about/index.html"));
160+
});
161+
162+
it("should have the correct value for the <title> tag in the <head>", function () {
163+
const title = dom.window.document.querySelector("head title").textContent;
164+
165+
expect(title).to.be.equal("My Custom About Page Title");
166+
});
167+
168+
it("should have the correct content for the page", function () {
169+
const heading = dom.window.document.querySelector("body h1").textContent;
170+
171+
expect(heading).to.be.equal("This is the about page");
172+
});
173+
});
151174
});
152175

153176
after(function () {

packages/cli/test/cases/build.config.active-frontmatter/src/layouts/blog.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html lang="en" prefix="og:http://ogp.me/ns#">
33
<head>
44
<meta name="author" content="${globalThis.page.data.author}" />
5+
<title>My Blog - ${globalThis.page.title}</title>
56
</head>
67

78
<body>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: My Custom About Page Title
3+
---
4+
5+
<html>
6+
<body>
7+
<h1>This is the about page</h1>
8+
</body>
9+
</html>

packages/cli/test/cases/build.default.title/build.default.title.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe("Build Greenwood With: ", function () {
6666
});
6767
});
6868

69-
describe("Custom Front-Matter Title", function () {
69+
describe("Custom Frontmatter Title", function () {
7070
let dom;
7171

7272
before(async function () {

www/layouts/app.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html>
22
<html lang="en" prefix="og:http://ogp.me/ns#">
33
<head>
4-
<title>Greenwood - ${globalThis.page.title}</title>
4+
<title>Greenwood</title>
55
<meta charset="utf-8" />
66
<meta
77
name="description"

www/layouts/blog.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!doctype html>
22
<html lang="en" prefix="og:http://ogp.me/ns#">
33
<head>
4+
<title>Greenwood - ${globalThis.page.title}</title>
45
<meta property="og:title" content="Greenwood - ${globalThis.page.title}" />
56
<link rel="stylesheet" href="/node_modules/prismjs/themes/prism-tomorrow.css" />
67
<link rel="stylesheet" href="../styles/page.css" />

www/layouts/docs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!doctype html>
22
<html lang="en" prefix="og:http://ogp.me/ns#">
33
<head>
4+
<title>Greenwood - ${globalThis.page.title}</title>
45
<meta property="og:title" content="Greenwood - ${globalThis.page.title}" />
56
<link rel="stylesheet" href="/node_modules/prismjs/themes/prism-tomorrow.css" />
67
<link rel="stylesheet" href="../styles/page.css" />

0 commit comments

Comments
 (0)