Skip to content

Commit

Permalink
Caused some regressions in the refactor of #3276 (oops!)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jun 10, 2024
1 parent f1a7dfb commit bdf2f34
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 36 deletions.
10 changes: 1 addition & 9 deletions src/Filters/Url.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { TemplatePath } from "@11ty/eleventy-utils";

function isValidUrl(url) {
try {
new URL(url);
return true;
} catch (e) {
// invalid url OR local path
return false;
}
}
import isValidUrl from "../Util/ValidUrl.js";

// Note: This filter is used in the Eleventy Navigation plugin in versions prior to 0.3.4
export default function (url, pathPrefix) {
Expand Down
10 changes: 1 addition & 9 deletions src/Plugins/HtmlBasePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ import { DeepCopy } from "@11ty/eleventy-utils";
import urlFilter from "../Filters/Url.js";
import PathPrefixer from "../Util/PathPrefixer.js";
import { HtmlTransformer } from "../Util/HtmlTransformer.js";

function isValidUrl(url) {
try {
new URL(url);
return true;
} catch (e) {
return false;
}
}
import isValidUrl from "../Util/ValidUrl.js";

function addPathPrefixToUrl(url, pathPrefix, base) {
let u;
Expand Down
59 changes: 41 additions & 18 deletions src/Plugins/InputPathToUrl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TemplatePath } from "@11ty/eleventy-utils";
import isValidUrl from "../Util/ValidUrl.js";

function normalizeInputPath(inputPath, inputDir, contentMap) {
// inputDir is optional at the beginning of the developer supplied-path
Expand All @@ -22,19 +23,31 @@ function normalizeInputPath(inputPath, inputDir, contentMap) {
return inputPath;
}

function parseFilePathForHashSupport(filepath) {
function parseFilePath(filepath) {
try {
/* u: URL {
href: 'file:///tmpl.njk#anchor',
origin: 'null',
protocol: 'file:',
username: '',
password: '',
host: '',
hostname: '',
port: '',
pathname: '/tmpl.njk',
search: '',
searchParams: URLSearchParams {},
hash: '#anchor'
} */

let u = new URL(filepath, "file:");
return {
return [
// hash includes # sign
hash: u.hash,
pathname: u.pathname,
};
u.search + u.hash,
u.pathname,
];
} catch (e) {
return {
hash: "",
pathname: filepath,
};
return ["", filepath];
}
}

Expand All @@ -49,16 +62,21 @@ function FilterPlugin(eleventyConfig) {
throw new Error("Internal error: contentMap not available for `inputPathToUrl` filter.");
}

if (isValidUrl(filepath)) {
return filepath;
}

let inputDir = eleventyConfig.directories.input;
let { hash, pathname } = parseFilePathForHashSupport(filepath);
pathname = normalizeInputPath(pathname, inputDir, contentMap);
let suffix = "";
[suffix, filepath] = parseFilePath(filepath);
filepath = normalizeInputPath(filepath, inputDir, contentMap);

let urls = contentMap[pathname];
let urls = contentMap[filepath];
if (!urls || urls.length === 0) {
throw new Error("`inputPathToUrl` filter could not find a matching target for " + filepath);
}

return `${urls[0]}${hash}`;
return `${urls[0]}${suffix}`;
});
}

Expand All @@ -79,18 +97,23 @@ function TransformPlugin(eleventyConfig, defaultOptions = {}) {
if (!contentMap) {
throw new Error("Internal error: contentMap not available for the `pathToUrl` Transform.");
}
if (isValidUrl(filepathOrUrl)) {
return filepathOrUrl;
}

let inputDir = eleventyConfig.directories.input;

let { hash, pathname } = parseFilePathForHashSupport(filepathOrUrl);
pathname = normalizeInputPath(pathname, inputDir, contentMap);
let suffix = "";
[suffix, filepathOrUrl] = parseFilePath(filepathOrUrl);
filepathOrUrl = normalizeInputPath(filepathOrUrl, inputDir, contentMap);

let urls = contentMap[pathname];
let urls = contentMap[filepathOrUrl];
if (!urls || urls.length === 0) {
// fallback, transforms don’t error on missing paths (though the pathToUrl filter does)
return `${pathname}${hash}`;
return `${filepathOrUrl}${suffix}`;
}

return `${urls[0]}${hash}`;
return `${urls[0]}${suffix}`;
});
}

Expand Down
9 changes: 9 additions & 0 deletions src/Util/ValidUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function isValidUrl(url) {
try {
new URL(url);
return true;
} catch (e) {
// invalid url OR local path
return false;
}
}

0 comments on commit bdf2f34

Please sign in to comment.