Skip to content

Commit

Permalink
Get the best possible target space for content refs (#2580)
Browse files Browse the repository at this point in the history
  • Loading branch information
taranvohra authored Nov 26, 2024
1 parent d9a18ec commit 2c945c6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
36 changes: 36 additions & 0 deletions packages/gitbook/e2e/pages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,42 @@ const testCases: TestsCase[] = [
},
],
},
{
name: 'Shared space navigation (first site)',
baseUrl: 'https://gitbook-open-e2e-sites.gitbook.io/shared-space-uno/',
tests: [
{
name: 'Navigation to shared space',
url: '',
run: async (page) => {
const sharedSpaceLink = page.locator('a.underline');
await sharedSpaceLink.click();
expect(page.locator('h1')).toHaveText('shared');
const url = page.url();
expect(url.includes('shared-space-uno')).toBeTruthy(); // same uno site
expect(url.endsWith('/shared')).toBeTruthy(); // correct page
},
},
],
},
{
name: 'Shared space navigation (second site)',
baseUrl: 'https://gitbook-open-e2e-sites.gitbook.io/shared-space-dos/',
tests: [
{
name: 'Navigation to shared space',
url: '',
run: async (page) => {
const sharedSpaceLink = page.locator('a.underline');
await sharedSpaceLink.click();
expect(page.locator('h1')).toHaveText('shared');
const url = page.url();
expect(url.includes('shared-space-dos')).toBeTruthy(); // same dos site
expect(url.endsWith('/shared')).toBeTruthy(); // correct page
},
},
],
},
{
name: 'Site Redirects',
baseUrl: 'https://gitbook-open-e2e-sites.gitbook.io/gitbook-doc/',
Expand Down
5 changes: 4 additions & 1 deletion packages/gitbook/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,10 @@ export const getPublishedContentSite = cache({

export type SectionsList = { list: SiteSection[]; section: SiteSection; index: number };

function parseSpacesFromSiteSpaces(siteSpaces: SiteSpace[]) {
/**
* Parse the site spaces into a list of spaces with their title and urls.
*/
export function parseSpacesFromSiteSpaces(siteSpaces: SiteSpace[]) {
const spaces: Record<string, Space> = {};
siteSpaces.forEach((siteSpace) => {
spaces[siteSpace.space.id] = {
Expand Down
63 changes: 55 additions & 8 deletions packages/gitbook/src/lib/references.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
ContentRef,
ContentRefSpace,
Revision,
RevisionFile,
RevisionPageDocument,
RevisionReusableContent,
SiteSpace,
Space,
} from '@gitbook/api';
import assertNever from 'assert-never';
Expand All @@ -16,12 +18,14 @@ import {
SpaceContentPointer,
getCollection,
getDocument,
getPublishedContentSite,
getReusableContent,
getRevisionFile,
getSpace,
getSpaceContentData,
getUserById,
ignoreAPIError,
parseSpacesFromSiteSpaces,
} from './api';
import { getBlockById, getBlockTitle } from './document';
import { gitbookAppHref, pageHref, PageHrefContext } from './links';
Expand Down Expand Up @@ -205,12 +209,7 @@ export async function resolveContentRef(
const targetSpace =
contentRef.space === space.id
? space
: await ignoreAPIError(
getSpace(
contentRef.space,
siteContext?.siteShareKey ? siteContext.siteShareKey : undefined,
),
);
: await getBestTargetSpace(contentRef.space, siteContext);

if (!targetSpace) {
return {
Expand Down Expand Up @@ -287,6 +286,50 @@ export async function resolveContentRef(
}
}

/**
* This function is used to get the best possible target space while resolving a content ref.
* It will try to return the space in the site context if it exists to avoid cross-site links.
*/
async function getBestTargetSpace(
spaceId: string,
siteContext: SiteContentPointer | null,
): Promise<Space | undefined> {
const [fetchedSpace, publishedContentSite] = await Promise.all([
ignoreAPIError(
getSpace(spaceId, siteContext?.siteShareKey ? siteContext.siteShareKey : undefined),
),
siteContext
? ignoreAPIError(
getPublishedContentSite({
organizationId: siteContext.organizationId,
siteId: siteContext.siteId,
siteShareKey: siteContext.siteShareKey,
}),
)
: null,
]);

// In the context of sites, we try to find our target space in the site structure.
// because the url of this space will be in the same site.
if (publishedContentSite) {
const siteSpaces =
publishedContentSite.structure.type === 'siteSpaces'
? publishedContentSite.structure.structure
: publishedContentSite.structure.structure.reduce<SiteSpace[]>((acc, section) => {
acc.push(...section.siteSpaces);
return acc;
}, []);
const spaces = parseSpacesFromSiteSpaces(siteSpaces);
const foundSpace = spaces.find((space) => space.id === spaceId);
if (foundSpace) {
return foundSpace;
}
}

// Else we try return the fetched space from the API.
return fetchedSpace ?? undefined;
}

async function resolveContentRefInSpace(
spaceId: string,
siteContext: SiteContentPointer | null,
Expand All @@ -296,12 +339,16 @@ async function resolveContentRefInSpace(
spaceId,
};

const result = await ignoreAPIError(getSpaceContentData(pointer, siteContext?.siteShareKey));
const [result, bestTargetSpace] = await Promise.all([
ignoreAPIError(getSpaceContentData(pointer, siteContext?.siteShareKey)),
getBestTargetSpace(spaceId, siteContext),
]);
if (!result) {
return null;
}

const { space, pages } = result;
const { pages } = result;
const space = bestTargetSpace ?? result.space;

// Base URL to use to prepend to relative URLs.
let baseUrl = space.urls.published ?? space.urls.app;
Expand Down

0 comments on commit 2c945c6

Please sign in to comment.