Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/tomarkdownpath-trailing-slash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@dualmark/core": patch
---

Fix `toMarkdownPath` doubling the extension on a `.md` path with a trailing slash.

The `.md` idempotency check ran before trailing slashes were stripped, so `toMarkdownPath("/blog/post.md/")` returned `/blog/post.md.md` (and `toMarkdownUrl` produced the same doubled path), which would 404. Trailing slashes are now stripped first, so a markdown path with a trailing slash maps back to itself. All other cases (root to `/index.md`, trailing-slash stripping, deep nesting) are unchanged.
5 changes: 4 additions & 1 deletion packages/core/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
* "/blog/x.md" → "/blog/x.md" (idempotent)
*/
export function toMarkdownPath(pathname: string): string {
if (pathname.endsWith(".md")) return pathname;
// Strip trailing slashes before the `.md` check, otherwise a markdown path
// with a trailing slash (e.g. "/blog/post.md/") defeats the idempotency
// guard and gets a doubled extension ("/blog/post.md.md").
Comment on lines +21 to +23
const trimmed = pathname.replace(/\/+$/, "");
if (trimmed === "") return "/index.md";
if (trimmed.endsWith(".md")) return trimmed;
return trimmed + ".md";
}

Expand Down
5 changes: 5 additions & 0 deletions packages/core/test/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ describe("toMarkdownPath", () => {
expect(toMarkdownPath("/index.md")).toBe("/index.md");
});

it("does not double the extension for a .md path with a trailing slash", () => {
expect(toMarkdownPath("/a.md/")).toBe("/a.md");
expect(toMarkdownPath("/blog/post.md/")).toBe("/blog/post.md");
});

it("handles deep nesting", () => {
expect(toMarkdownPath("/a/b/c/d/e")).toBe("/a/b/c/d/e.md");
});
Expand Down