Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow inputPathToUrl to handle hashes (#anchor) #3276

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>