Skip to content

Commit

Permalink
Fixes #3581
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 10, 2024
1 parent 117e7e6 commit b34c7a0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/Plugins/InputPathToUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions test/InputPathToUrlPluginTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,21 @@ test("Issue #3417 Using the transform with relative path (no dot slash)", async
`<a href="/source/target/">Target</a>`
);
});

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", `<a href="#built-cost-🧰">Target</a>`)
},
});

let results = await elev.toJSON();

t.is(
getContentFor(results, "/source/test/index.html"),
`<a href="#built-cost-🧰">Target</a>`
);
});

0 comments on commit b34c7a0

Please sign in to comment.