From 2932077bf90ca92eadbfd8b9c832c5fd6f018e0b Mon Sep 17 00:00:00 2001 From: conico974 Date: Tue, 27 May 2025 21:13:55 +0200 Subject: [PATCH] Remove trailing slash from linker (#3268) Co-authored-by: Nicolas Dorseuil --- .changeset/wise-gifts-smash.md | 5 +++++ packages/gitbook-v2/src/lib/links.test.ts | 22 +++++++++++++++++++++- packages/gitbook-v2/src/lib/links.ts | 6 +++++- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/wise-gifts-smash.md diff --git a/.changeset/wise-gifts-smash.md b/.changeset/wise-gifts-smash.md new file mode 100644 index 000000000..32a85eecb --- /dev/null +++ b/.changeset/wise-gifts-smash.md @@ -0,0 +1,5 @@ +--- +"gitbook-v2": patch +--- + +remove trailing slash from linker diff --git a/packages/gitbook-v2/src/lib/links.test.ts b/packages/gitbook-v2/src/lib/links.test.ts index 26a2464f5..f73ec5ee8 100644 --- a/packages/gitbook-v2/src/lib/links.test.ts +++ b/packages/gitbook-v2/src/lib/links.test.ts @@ -19,7 +19,7 @@ const siteGitBookIO = createLinker({ siteBasePath: '/sitename/', }); -describe('toPathInContent', () => { +describe('toPathInSpace', () => { it('should return the correct path', () => { expect(root.toPathInSpace('some/path')).toBe('/some/path'); expect(variantInSection.toPathInSpace('some/path')).toBe('/section/variant/some/path'); @@ -29,6 +29,16 @@ describe('toPathInContent', () => { expect(root.toPathInSpace('/some/path')).toBe('/some/path'); expect(variantInSection.toPathInSpace('/some/path')).toBe('/section/variant/some/path'); }); + + it('should remove the trailing slash', () => { + expect(root.toPathInSpace('some/path/')).toBe('/some/path'); + expect(variantInSection.toPathInSpace('some/path/')).toBe('/section/variant/some/path'); + }); + + it('should not add a trailing slash', () => { + expect(root.toPathInSpace('')).toBe(''); + expect(variantInSection.toPathInSpace('')).toBe('/section/variant'); + }); }); describe('toPathInSite', () => { @@ -36,6 +46,16 @@ describe('toPathInSite', () => { expect(root.toPathInSite('some/path')).toBe('/some/path'); expect(siteGitBookIO.toPathInSite('some/path')).toBe('/sitename/some/path'); }); + + it('should remove the trailing slash', () => { + expect(root.toPathInSite('some/path/')).toBe('/some/path'); + expect(siteGitBookIO.toPathInSite('some/path/')).toBe('/sitename/some/path'); + }); + + it('should not add a trailing slash', () => { + expect(root.toPathInSite('')).toBe(''); + expect(siteGitBookIO.toPathInSite('')).toBe('/sitename'); + }); }); describe('toRelativePathInSite', () => { diff --git a/packages/gitbook-v2/src/lib/links.ts b/packages/gitbook-v2/src/lib/links.ts index b84565d1e..a64bda541 100644 --- a/packages/gitbook-v2/src/lib/links.ts +++ b/packages/gitbook-v2/src/lib/links.ts @@ -128,5 +128,9 @@ 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 prefixPath + suffixPath; + return removeTrailingSlash(prefixPath + suffixPath); +} + +function removeTrailingSlash(path: string): string { + return path.endsWith('/') ? path.slice(0, -1) : path; }