Skip to content

Commit 8fe9c9a

Browse files
authored
Fix an issue where same-site references across spaces created invalid links. (#3407)
1 parent 216ba7a commit 8fe9c9a

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,27 @@ describe('linkerWithOtherSpaceBasePath', () => {
114114
'/section/variant/some/path'
115115
);
116116
});
117+
118+
it('should return a new linker that resolves links relative to a new spaceBasePath in the current site', () => {
119+
const otherSpaceBasePathLinker = linkerWithOtherSpaceBasePath(root, {
120+
spaceBasePath: '/section/variant',
121+
});
122+
expect(otherSpaceBasePathLinker.toPathInSpace('some/path')).toBe(
123+
'/section/variant/some/path'
124+
);
125+
});
126+
127+
it('should use a basepath relative to the site', () => {
128+
const otherSpaceBasePathLinker = linkerWithOtherSpaceBasePath(siteGitBookIO, {
129+
spaceBasePath: 'a/b',
130+
});
131+
expect(otherSpaceBasePathLinker.toPathInSpace('some/path')).toBe('/sitename/a/b/some/path');
132+
});
133+
134+
it('should use a basepath relative to the site (with trailing slash)', () => {
135+
const otherSpaceBasePathLinker = linkerWithOtherSpaceBasePath(siteGitBookIO, {
136+
spaceBasePath: '/a/b',
137+
});
138+
expect(otherSpaceBasePathLinker.toPathInSpace('some/path')).toBe('/sitename/a/b/some/path');
139+
});
117140
});

packages/gitbook/src/lib/links.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,22 @@ export function linkerWithAbsoluteURLs(linker: GitBookLinker): GitBookLinker {
142142
*/
143143
export function linkerWithOtherSpaceBasePath(
144144
linker: GitBookLinker,
145-
{ spaceBasePath }: { spaceBasePath: string }
145+
{
146+
spaceBasePath,
147+
}: {
148+
/**
149+
* The base path of the space. It should be relative to the root of the site.
150+
*/
151+
spaceBasePath: string;
152+
}
146153
): GitBookLinker {
147154
const newLinker: GitBookLinker = {
148155
...linker,
149156
toPathInSpace(relativePath: string): string {
150-
return joinPaths(spaceBasePath, relativePath);
157+
return linker.toPathInSite(joinPaths(spaceBasePath, relativePath));
151158
},
159+
// implementation matches the base linker toPathForPage, but decouples from using `this` to
160+
// ensure we always use the updates `toPathInSpace` method.
152161
toPathForPage({ pages, page, anchor }) {
153162
return newLinker.toPathInSpace(getPagePath(pages, page)) + (anchor ? `#${anchor}` : '');
154163
},

0 commit comments

Comments
 (0)