Skip to content

Commit

Permalink
Fix regression with #hash and ?search links and InputPathToUrl plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jun 11, 2024
1 parent 245db98 commit b60027f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Plugins/InputPathToUrl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "node:path";
import { TemplatePath } from "@11ty/eleventy-utils";
import isValidUrl from "../Util/ValidUrl.js";

Expand Down Expand Up @@ -40,10 +41,15 @@ function parseFilePath(filepath) {
hash: '#anchor'
} */

// URL(`file:#anchor`) gives back a pathname of `/`
if (filepath.startsWith("#") || filepath.startsWith("?")) {
return ["", filepath];
}

// Note that `node:url` -> pathToFileURL creates an absolute path, which we don’t want
let u = new URL(`file:${filepath}`);
return [
// hash includes # sign
// search includes ?, hash includes #
u.search + u.hash,
u.pathname,
];
Expand Down
4 changes: 4 additions & 0 deletions test/InputPathToUrlPluginTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const OUTPUT_HTML_STD = `<!doctype html>
<a href="/">Home</a>
<a href="/tmpl/">Test</a>
<a href="/tmpl/#anchor">Anchor</a>
<a href="#anchor">Anchor</a>
<a href="?search=1">Search</a>
</body>
</html>`;

Expand All @@ -36,6 +38,8 @@ const OUTPUT_HTML_BASE = `<!doctype html>
<a href="/gh-pages/">Home</a>
<a href="/gh-pages/tmpl/">Test</a>
<a href="/gh-pages/tmpl/#anchor">Anchor</a>
<a href="#anchor">Anchor</a>
<a href="?search=1">Search</a>
</body>
</html>`;

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

0 comments on commit b60027f

Please sign in to comment.