diff --git a/packages/gitbook/src/lib/pages.test.ts b/packages/gitbook/src/lib/pages.test.ts index 18c9a94b5..114d39658 100644 --- a/packages/gitbook/src/lib/pages.test.ts +++ b/packages/gitbook/src/lib/pages.test.ts @@ -35,6 +35,19 @@ describe('extractPagePath', () => { it('returns empty string for root URL', () => { expect(extractPagePath('https://docs.example.com/api/', baseURL)).toBe(''); }); + + it('extracts path when base URL is at the root of the domain', () => { + expect( + extractPagePath( + 'https://docs.example.com/merchant-account/user-management/sso', + 'https://docs.example.com/' + ) + ).toBe('merchant-account/user-management/sso'); + }); + + it('returns empty string when URL equals root base URL', () => { + expect(extractPagePath('https://docs.example.com/', 'https://docs.example.com/')).toBe(''); + }); }); describe('resolveFirstDocument', () => { diff --git a/packages/gitbook/src/lib/pages.ts b/packages/gitbook/src/lib/pages.ts index 08ab395b8..9678bc84f 100644 --- a/packages/gitbook/src/lib/pages.ts +++ b/packages/gitbook/src/lib/pages.ts @@ -413,11 +413,12 @@ export function extractPagePath(url: string, baseURL: string): string | undefine const urlPath = getURLPathname(url); const basePath = getURLPathname(baseURL); - if (!urlPath || !basePath) { + if (urlPath === undefined || basePath === undefined) { return undefined; } - if (urlPath.startsWith(`${basePath}/`) || urlPath === basePath) { + // When basePath is empty, the site is at the root of the domain, so any path matches + if (basePath === '' || urlPath.startsWith(`${basePath}/`) || urlPath === basePath) { return removeLeadingSlash(urlPath.slice(basePath.length)); } }