Skip to content

Commit

Permalink
Merge branch 'input_paths_with_anchors' of github.com:mendhak/elevent…
Browse files Browse the repository at this point in the history
…y into mendhak-input_paths_with_anchors
  • Loading branch information
zachleat committed Jun 10, 2024
2 parents d4c5f85 + a7a0221 commit 899fbd4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Plugins/InputPathToUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ function normalizeInputPath(inputPath, inputDir, contentMap) {
return inputPath;
}

function splitFilePath(filepath) {
let hash = "";
let splitUrl = filepath.split("#");
if (splitUrl.length > 1) {
hash = "#" + splitUrl[1];
filepath = splitUrl[0];
}
return [hash, filepath];
}

function FilterPlugin(eleventyConfig) {
let contentMap;
eleventyConfig.on("eleventy.contentMap", function ({ inputPathToUrl }) {
Expand All @@ -34,14 +44,16 @@ function FilterPlugin(eleventyConfig) {
}

let inputDir = eleventyConfig.directories.input;
let hash = "";
[hash, filepath] = splitFilePath(filepath);
filepath = normalizeInputPath(filepath, inputDir, contentMap);

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];
return `${urls[0]}${hash}`;
});
}

Expand All @@ -63,15 +75,18 @@ function TransformPlugin(eleventyConfig, defaultOptions = {}) {
throw new Error("Internal error: contentMap not available for the `pathToUrl` Transform.");
}
let inputDir = eleventyConfig.directories.input;

let hash = "";
[hash, filepathOrUrl] = splitFilePath(filepathOrUrl);
filepathOrUrl = normalizeInputPath(filepathOrUrl, inputDir, contentMap);

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

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

Expand Down
2 changes: 2 additions & 0 deletions test/InputPathToUrlPluginTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const OUTPUT_HTML_STD = `<!doctype html>
<body>
<a href="/">Home</a>
<a href="/tmpl/">Test</a>
<a href="/tmpl/#anchor">Anchor</a>
</body>
</html>`;

Expand All @@ -34,6 +35,7 @@ const OUTPUT_HTML_BASE = `<!doctype html>
<body>
<a href="/gh-pages/">Home</a>
<a href="/gh-pages/tmpl/">Test</a>
<a href="/gh-pages/tmpl/#anchor">Anchor</a>
</body>
</html>`;

Expand Down
1 change: 1 addition & 0 deletions test/stubs-pathtourl/filter.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
<body>
<a href="/">Home</a>
<a href="{{ 'tmpl.njk' | inputPathToUrl }}">Test</a>
<a href="{{ 'tmpl.njk#anchor' | inputPathToUrl }}">Anchor</a>
</body>
</html>
1 change: 1 addition & 0 deletions test/stubs-pathtourl/transform.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
<body>
<a href="/">Home</a>
<a href="tmpl.njk">Test</a>
<a href="tmpl.njk#anchor">Anchor</a>
</body>
</html>

0 comments on commit 899fbd4

Please sign in to comment.