Skip to content

Commit 2932077

Browse files
conico974Nicolas Dorseuil
and
Nicolas Dorseuil
authored
Remove trailing slash from linker (#3268)
Co-authored-by: Nicolas Dorseuil <[email protected]>
1 parent 957afd9 commit 2932077

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

.changeset/wise-gifts-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook-v2": patch
3+
---
4+
5+
remove trailing slash from linker

packages/gitbook-v2/src/lib/links.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const siteGitBookIO = createLinker({
1919
siteBasePath: '/sitename/',
2020
});
2121

22-
describe('toPathInContent', () => {
22+
describe('toPathInSpace', () => {
2323
it('should return the correct path', () => {
2424
expect(root.toPathInSpace('some/path')).toBe('/some/path');
2525
expect(variantInSection.toPathInSpace('some/path')).toBe('/section/variant/some/path');
@@ -29,13 +29,33 @@ describe('toPathInContent', () => {
2929
expect(root.toPathInSpace('/some/path')).toBe('/some/path');
3030
expect(variantInSection.toPathInSpace('/some/path')).toBe('/section/variant/some/path');
3131
});
32+
33+
it('should remove the trailing slash', () => {
34+
expect(root.toPathInSpace('some/path/')).toBe('/some/path');
35+
expect(variantInSection.toPathInSpace('some/path/')).toBe('/section/variant/some/path');
36+
});
37+
38+
it('should not add a trailing slash', () => {
39+
expect(root.toPathInSpace('')).toBe('');
40+
expect(variantInSection.toPathInSpace('')).toBe('/section/variant');
41+
});
3242
});
3343

3444
describe('toPathInSite', () => {
3545
it('should return the correct path', () => {
3646
expect(root.toPathInSite('some/path')).toBe('/some/path');
3747
expect(siteGitBookIO.toPathInSite('some/path')).toBe('/sitename/some/path');
3848
});
49+
50+
it('should remove the trailing slash', () => {
51+
expect(root.toPathInSite('some/path/')).toBe('/some/path');
52+
expect(siteGitBookIO.toPathInSite('some/path/')).toBe('/sitename/some/path');
53+
});
54+
55+
it('should not add a trailing slash', () => {
56+
expect(root.toPathInSite('')).toBe('');
57+
expect(siteGitBookIO.toPathInSite('')).toBe('/sitename');
58+
});
3959
});
4060

4161
describe('toRelativePathInSite', () => {

packages/gitbook-v2/src/lib/links.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,9 @@ export function createLinker(
128128
function joinPaths(prefix: string, path: string): string {
129129
const prefixPath = prefix.endsWith('/') ? prefix : `${prefix}/`;
130130
const suffixPath = path.startsWith('/') ? path.slice(1) : path;
131-
return prefixPath + suffixPath;
131+
return removeTrailingSlash(prefixPath + suffixPath);
132+
}
133+
134+
function removeTrailingSlash(path: string): string {
135+
return path.endsWith('/') ? path.slice(0, -1) : path;
132136
}

0 commit comments

Comments
 (0)