Skip to content

Commit 79592ae

Browse files
mgwalkergeekygirlsarah
authored andcommitted
fix an issue with baseurl processing
1 parent 099c93b commit 79592ae

File tree

7 files changed

+33
-8
lines changed

7 files changed

+33
-8
lines changed

.eleventy.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const markdownItAttrs = require("markdown-it-attrs");
55
const yaml = require("js-yaml");
66
const svgSprite = require("eleventy-plugin-svg-sprite");
77
const { headingLinks } = require("./config/headingLinks");
8+
const baseurl = require("./config/baseurl");
89

910
const HandbookPlugin = require("./config/HandbookPlugin.js");
1011

@@ -59,7 +60,7 @@ module.exports = function (config) {
5960
templateFormats: ["md", "html", "njk"],
6061
markdownTemplateEngine: "liquid",
6162
htmlTemplateEngine: "liquid",
62-
pathPrefix: process.env.BASEURL ?? "/",
63+
pathPrefix: baseurl,
6364

6465
dir: {
6566
input: "pages",

check-links.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const cheerio = require("cheerio");
21
const fs = require("fs/promises");
32
const path = require("path");
3+
const baseurl = require("./config/baseurl");
4+
const cheerio = require("cheerio");
45

56
const SITE_ROOT = path.join(__dirname, "_site");
67

@@ -51,7 +52,7 @@ const getDom = async (file) => {
5152
SITE_ROOT,
5253
// Strip off any URL hashes and remove the base URL portion to get down
5354
// to just the target file.
54-
realFile.replace(/\#.*$/, "").replace(process.env.BASEURL, ""),
55+
realFile.replace(/\#.*$/, "").replace(baseurl, ""),
5556
path.basename(file)
5657
);
5758

@@ -103,7 +104,7 @@ const run = async () => {
103104

104105
// if(/^\/(images|))
105106

106-
pathComponents.push(url.pathname.replace(process.env.BASEURL, ""));
107+
pathComponents.push(url.pathname.replace(baseurl, ""));
107108

108109
// If the link does not include a file path, append index.html
109110
if (!/\.[a-z]+/i.test(url.pathname)) {

config/baseurl.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This bit of logic was being used in several places, and that's silly.
2+
// Especially when it turns out it's a little more complex than I thought it
3+
// was, and resulted in these issues:
4+
//
5+
// https://github.com/18F/handbook/issues/3615
6+
// https://github.com/18F/handbook/issues/3623
7+
// https://github.com/18F/handbook/issues/3630
8+
9+
if (!process.env.BASEURL) {
10+
// If the BASEURL set is falsey, then our actual baseurl is the root.
11+
module.exports = "/";
12+
// If the BASEURL is set but it's a string of just whitespace, we want to use
13+
// the root for that too.
14+
} else if (process.env.BASEURL.trim().length === 0) {
15+
module.exports = "/";
16+
} else {
17+
// But if the BASEURL is set to a real value, then that's our base.
18+
module.exports = process.env.BASEURL;
19+
}

config/shortcodes/download.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require("fs/promises");
22
const path = require("path");
3+
const baseurl = require("../baseurl");
34

45
// Given a path to a file that should be downloadable, copy that path into the
56
// output's assets directory so it'll be available. Return the resulting URL
@@ -17,5 +18,5 @@ module.exports = async (downloadPath) => {
1718

1819
// The resulting URL should include the base URL, if any. This is necessary
1920
// for cloud.gov Pages previews to work correctly.
20-
return `${process.env.BASEURL ?? ""}/assets/downloads/${filename}`;
21+
return `${baseurl}/assets/downloads/${filename}`;
2122
};

config/shortcodes/imageWithClass.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Image = require("@11ty/eleventy-img");
22
const path = require("path");
3+
const baseurl = require("../baseurl");
34

45
// Given an image path, copies the file to the assets/images directory so it is
56
// available on the web. Returns the text for an <img> element that has the
@@ -31,7 +32,7 @@ module.exports = async (imagePath, cssClass, altText) => {
3132
// unexpected, hard-to-debug ways that do not break the build.
3233
const attributes = {
3334
// We need to honor BASEURL to support cloud.gov Pages preview builds.
34-
src: `${process.env.BASEURL ?? ""}/${url}`,
35+
src: `${baseurl}/${url}`,
3536
class: cssClass ?? false,
3637
alt: altText ?? false,
3738
loading: "lazy",

config/shortcodes/link.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const path = require("path");
2+
const baseurl = require("../baseurl");
23

34
// Given text that should be a link, guarantee that it becomes one. If it's
45
// already a link that begins with http/https, leave it alone. Otherwise, add
56
// the site BASEURL to it.
67
module.exports = (link) =>
7-
link.startsWith("http") ? link : path.join(process.env.BASEURL ?? "/", link);
8+
link.startsWith("http") ? link : path.join(baseurl, link);

config/shortcodes/page.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require("path");
2+
const baseurl = require("../baseurl");
23

34
// Given a page reference, turn it into a link on the published site.
4-
module.exports = (link) => path.join(process.env.BASEURL ?? "/", link);
5+
module.exports = (link) => path.join(baseurl, link);

0 commit comments

Comments
 (0)