Add support for root destination markdown routing in toMarkdownDestination

This commit is contained in:
Nicolas Dorseuil
2026-10-05 14:36:17 +02:00
parent 0c92b3a679
commit 8ca77c85d5
2 changed files with 8 additions and 2 deletions
@@ -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');
+3 -2
View File
@@ -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) {