diff --git a/.changeset/silver-ideas-relax.md b/.changeset/silver-ideas-relax.md new file mode 100644 index 0000000000..68d65ec9e9 --- /dev/null +++ b/.changeset/silver-ideas-relax.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Fix an issue where content refs to spaces outside of site was resolving with share links diff --git a/packages/gitbook/e2e/internal.spec.ts b/packages/gitbook/e2e/internal.spec.ts index 0008efa9ee..e3e1aa0763 100644 --- a/packages/gitbook/e2e/internal.spec.ts +++ b/packages/gitbook/e2e/internal.spec.ts @@ -1318,6 +1318,46 @@ const testCases: TestsCase[] = [ }, ], }, + { + name: 'Navigation of links on a share links site', + contentBaseURL: 'https://gitbook-open-e2e-sites.gitbook.io/api-multi-versions-share-links/', + tests: [ + { + name: 'Link in same site has the share link token preserved', + url: '8tNo6MeXg7CkFMzSSz81/link-in-same-site', + run: async (page) => { + const linkToDifferentPage = page.getByRole('link', { name: 'Other Page' }); + await linkToDifferentPage.click(); + await page.waitForURL((url) => + url.pathname.includes( + '/api-multi-versions-share-links/8tNo6MeXg7CkFMzSSz81/3.0/other-page' + ) + ); + await expect( + page.getByRole('heading', { level: 1, name: 'Other Page' }) + ).toBeVisible(); + }, + screenshot: false, + }, + { + name: 'Link to different site should not have the share link token preserved', + url: '8tNo6MeXg7CkFMzSSz81/link-in-different-site', + run: async (page) => { + const linkToDifferentSite = page.getByRole('link', { name: 'Basics' }); + await linkToDifferentSite.click(); + await page.waitForURL( + (url) => + url.toString() === + 'https://gitbook-open-e2e-sites.gitbook.io/sections/sections-4/basics/editor' + ); + await expect( + page.getByRole('heading', { level: 1, name: 'Editor' }) + ).toBeVisible(); + }, + screenshot: false, + }, + ], + }, { name: 'Visitor Auth - Space', contentBaseURL: 'https://gitbook.gitbook.io/gbo-va-space/', diff --git a/packages/gitbook/src/lib/references.tsx b/packages/gitbook/src/lib/references.tsx index f5245b6897..f89708afcb 100644 --- a/packages/gitbook/src/lib/references.tsx +++ b/packages/gitbook/src/lib/references.tsx @@ -362,7 +362,22 @@ async function resolveContentRefInSpace( contentRef: ContentRef, options: ResolveContentRefOptions = {} ) { - const ctx = await createContextForSpace(spaceId, context); + const ctx = await createContextForSpace(spaceId, { + ...context, + shareKey: (() => { + // If the space is found in the current site, we use the current share key to generate links. + if ('site' in context) { + return findSiteSpaceBy( + context.structure, + (siteSpace) => siteSpace.space.id === spaceId + ) + ? context.shareKey + : undefined; + } + + return context.space.id === spaceId ? context.shareKey : undefined; + })(), + }); if (!ctx) { return null; @@ -381,7 +396,6 @@ async function resolveContentRefInSpace( context.structure, (siteSpace) => siteSpace.space.id === spaceId ); - return ( foundSiteSpace?.siteSpace.title ?? foundSiteSpace?.siteSection?.title ??