Skip to content

Commit 30bcd3e

Browse files
committed
Fix test-hrefs to accept href links to /static content
1 parent e6e9a28 commit 30bcd3e

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

scripts/test-hrefs.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ const mapBlogFilePath = path => {
3232
return path;
3333
};
3434

35+
// Static files are located in /public/static/img/somefile.png
36+
// within markdown files they are referenced as /static/img/somefile.png
37+
const mapStaticFilePath = path => {
38+
return path.replace("./public", "");
39+
}
40+
3541
// Creates a lookup table of all available pages within the website
3642
// It will also automatically map urls for dedicated directorys (such as _blogposts)
3743
// to the correct url
@@ -40,9 +46,18 @@ const createPageIndex = files => {
4046
return files.reduce((acc, path) => {
4147
// We need to consider all the different file formats used in pages
4248
// Calculate the website url by stripping .re, .bs.js, .md(x), etc.
43-
let url = mapBlogFilePath(path)
44-
.replace(/^\.\//, "/")
45-
.replace(/\.re|\.bs\.js|\.js|\.md(x)?$/, "");
49+
let url;
50+
if(path.startsWith("./_blogposts")) {
51+
url = mapBlogFilePath(path)
52+
}
53+
else if(path.startsWith("./public/static")) {
54+
url = mapStaticFilePath(path);
55+
}
56+
else {
57+
url = path;
58+
}
59+
60+
url = url.replace(/^\.\//, "/").replace(/\.re|\.bs\.js|\.js|\.md(x)?$/, "");
4661

4762
// For index we need to special case, since it can be referred as '/' as well
4863
if (path.match(/\.\/pages\/index(\.re|\.bs\.js|\.js|\.md(x))?$/)) {
@@ -133,9 +148,16 @@ const testFile = (pageMap, test) => {
133148
let resolved;
134149
if (!path.isAbsolute(url)) {
135150
resolved = path.join("/", path.dirname(filepath), parsed.pathname);
136-
} else {
137-
// e.g. /api/javascript/latest/js needs to be prefixed to actual pages dir
138-
resolved = path.join("/pages", parsed.pathname);
151+
}
152+
else {
153+
if(parsed.pathname.startsWith("/static")) {
154+
console.log("Static");
155+
resolved = path.join(parsed.pathname);
156+
}
157+
else {
158+
// e.g. /api/javascript/latest/js needs to be prefixed to actual pages dir
159+
resolved = path.join("/pages", parsed.pathname);
160+
}
139161
}
140162

141163
// If there's no page stated the relative link
@@ -191,7 +213,11 @@ const main = () => {
191213

192214
// We need to capture all files independently from the test file glob
193215
const pageMapFiles = glob.sync("./{pages,_blogposts}/**/*.{js,mdx}", { cwd });
194-
const pageMap = createPageIndex(pageMapFiles);
216+
const staticFiles = glob.sync("./public/static/**/*.{svg,png,woff2}", { cwd });
217+
218+
const allFiles = pageMapFiles.concat(staticFiles);
219+
220+
const pageMap = createPageIndex(allFiles);
195221

196222
//console.log(pageMap);
197223
//return;

0 commit comments

Comments
 (0)