diff --git a/packages/gitbook-v2/src/lib/links.test.ts b/packages/gitbook-v2/src/lib/links.test.ts index f73ec5ee8..30e6f06c7 100644 --- a/packages/gitbook-v2/src/lib/links.test.ts +++ b/packages/gitbook-v2/src/lib/links.test.ts @@ -35,8 +35,9 @@ describe('toPathInSpace', () => { expect(variantInSection.toPathInSpace('some/path/')).toBe('/section/variant/some/path'); }); - it('should not add a trailing slash', () => { - expect(root.toPathInSpace('')).toBe(''); + it('should not add an unnecessary trailing slash', () => { + // The index page should not be an empty path + expect(root.toPathInSpace('')).toBe('/'); expect(variantInSection.toPathInSpace('')).toBe('/section/variant'); }); }); @@ -52,8 +53,9 @@ describe('toPathInSite', () => { expect(siteGitBookIO.toPathInSite('some/path/')).toBe('/sitename/some/path'); }); - it('should not add a trailing slash', () => { - expect(root.toPathInSite('')).toBe(''); + it('should not add an unnecessary trailing slash', () => { + // The index page should not be an empty path + expect(root.toPathInSite('')).toBe('/'); expect(siteGitBookIO.toPathInSite('')).toBe('/sitename'); }); }); diff --git a/packages/gitbook-v2/src/lib/links.ts b/packages/gitbook-v2/src/lib/links.ts index a64bda541..bb2978505 100644 --- a/packages/gitbook-v2/src/lib/links.ts +++ b/packages/gitbook-v2/src/lib/links.ts @@ -128,7 +128,8 @@ export function createLinker( function joinPaths(prefix: string, path: string): string { const prefixPath = prefix.endsWith('/') ? prefix : `${prefix}/`; const suffixPath = path.startsWith('/') ? path.slice(1) : path; - return removeTrailingSlash(prefixPath + suffixPath); + const pathWithoutTrailingSlash = removeTrailingSlash(prefixPath + suffixPath); + return pathWithoutTrailingSlash === '' ? '/' : pathWithoutTrailingSlash; } function removeTrailingSlash(path: string): string {