From b34c7a0afeaf79a8bf5ce8382bffbcaef97e6fbc Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Tue, 10 Dec 2024 16:44:37 -0600 Subject: [PATCH] Fixes #3581 --- src/Plugins/InputPathToUrl.js | 31 ++++++++++++++++++++++--------- test/InputPathToUrlPluginTest.js | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/Plugins/InputPathToUrl.js b/src/Plugins/InputPathToUrl.js index ddc60ed70..19c619400 100644 --- a/src/Plugins/InputPathToUrl.js +++ b/src/Plugins/InputPathToUrl.js @@ -45,6 +45,10 @@ function normalizeInputPath(targetInputPath, inputDir, sourceInputPath, contentM } function parseFilePath(filepath) { + if (filepath.startsWith("#") || filepath.startsWith("?")) { + return [filepath, ""]; + } + try { /* u: URL { href: 'file:///tmpl.njk#anchor', @@ -95,8 +99,15 @@ function FilterPlugin(eleventyConfig) { let inputDir = eleventyConfig.directories.input; let suffix = ""; [suffix, targetFilePath] = parseFilePath(targetFilePath); - // @ts-ignore - targetFilePath = normalizeInputPath(targetFilePath, inputDir, this.page.inputPath, contentMap); + if (targetFilePath) { + // @ts-ignore + targetFilePath = normalizeInputPath( + targetFilePath, + inputDir, + this.page.inputPath, + contentMap, + ); + } let urls = contentMap[targetFilePath]; if (!urls || urls.length === 0) { @@ -134,13 +145,15 @@ function TransformPlugin(eleventyConfig, defaultOptions = {}) { let suffix = ""; [suffix, targetFilepathOrUrl] = parseFilePath(targetFilepathOrUrl); - targetFilepathOrUrl = normalizeInputPath( - targetFilepathOrUrl, - inputDir, - // @ts-ignore - this.page.inputPath, - contentMap, - ); + if (targetFilepathOrUrl) { + targetFilepathOrUrl = normalizeInputPath( + targetFilepathOrUrl, + inputDir, + // @ts-ignore + this.page.inputPath, + contentMap, + ); + } let urls = contentMap[targetFilepathOrUrl]; if (!targetFilepathOrUrl || !urls || urls.length === 0) { diff --git a/test/InputPathToUrlPluginTest.js b/test/InputPathToUrlPluginTest.js index 0dc665142..b42237910 100644 --- a/test/InputPathToUrlPluginTest.js +++ b/test/InputPathToUrlPluginTest.js @@ -172,3 +172,21 @@ test("Issue #3417 Using the transform with relative path (no dot slash)", async `Target` ); }); + +test("Issue #3581 #build-cost-🧰", async (t) => { + let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/_site", { + configPath: false, + config: function (eleventyConfig) { + eleventyConfig.addPlugin(TransformPlugin); + + eleventyConfig.addTemplate("source/test.njk", `Target`) + }, + }); + + let results = await elev.toJSON(); + + t.is( + getContentFor(results, "/source/test/index.html"), + `Target` + ); +});