diff --git a/src/Filters/Url.js b/src/Filters/Url.js index c89ac3010..87fc1e2e3 100644 --- a/src/Filters/Url.js +++ b/src/Filters/Url.js @@ -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) { diff --git a/src/Plugins/HtmlBasePlugin.js b/src/Plugins/HtmlBasePlugin.js index 3760d8a48..d1c1aca77 100644 --- a/src/Plugins/HtmlBasePlugin.js +++ b/src/Plugins/HtmlBasePlugin.js @@ -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; diff --git a/src/Plugins/InputPathToUrl.js b/src/Plugins/InputPathToUrl.js index b0eb07a20..ae51cdc4c 100644 --- a/src/Plugins/InputPathToUrl.js +++ b/src/Plugins/InputPathToUrl.js @@ -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 @@ -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]; } } @@ -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}`; }); } @@ -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}`; }); } diff --git a/src/Util/ValidUrl.js b/src/Util/ValidUrl.js new file mode 100644 index 000000000..9d0f59ba1 --- /dev/null +++ b/src/Util/ValidUrl.js @@ -0,0 +1,9 @@ +export default function isValidUrl(url) { + try { + new URL(url); + return true; + } catch (e) { + // invalid url OR local path + return false; + } +}