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) {