From 8ca77c85d5d115ed57009fac24e7cdae013ced0d Mon Sep 17 00:00:00 2001 From: Nicolas Dorseuil Date: Mon, 5 Oct 2026 14:36:17 +0200 Subject: [PATCH] Add support for root destination markdown routing in toMarkdownDestination --- packages/gitbook/src/routes/markdownPage.test.ts | 5 +++++ packages/gitbook/src/routes/markdownPage.ts | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/gitbook/src/routes/markdownPage.test.ts b/packages/gitbook/src/routes/markdownPage.test.ts index 2445f95f8..1be218f1f 100644 --- a/packages/gitbook/src/routes/markdownPage.test.ts +++ b/packages/gitbook/src/routes/markdownPage.test.ts @@ -81,6 +81,11 @@ describe('toMarkdownDestination', () => { expect(toMarkdownDestination('/docs/new-page/?a=1#b')).toBe('/docs/new-page.md?a=1#b'); }); + it('points the site root to its markdown route', () => { + expect(toMarkdownDestination('/')).toBe('/.md'); + expect(toMarkdownDestination('/?a=1#b')).toBe('/.md?a=1#b'); + }); + it('leaves markdown paths and external URLs untouched', () => { expect(toMarkdownDestination('/docs/new-page.md')).toBe('/docs/new-page.md'); expect(toMarkdownDestination('https://example.com/page')).toBe('https://example.com/page'); diff --git a/packages/gitbook/src/routes/markdownPage.ts b/packages/gitbook/src/routes/markdownPage.ts index 660313713..611a3f90e 100644 --- a/packages/gitbook/src/routes/markdownPage.ts +++ b/packages/gitbook/src/routes/markdownPage.ts @@ -99,11 +99,12 @@ export function toMarkdownDestination(destination: string): string { const url = new URL(destination, 'https://gitbook.invalid'); const pathname = url.pathname.replace(/\/+$/, ''); - if (!pathname || pathname.endsWith('.md')) { + if (pathname.endsWith('.md')) { return destination; } - return `${pathname}.md${url.search}${url.hash}`; + // A root destination trims to an empty pathname; its markdown route is `/.md`. + return `${pathname || '/'}.md${url.search}${url.hash}`; } function markdownRedirect(location: string, permanent: boolean) {