mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-26 10:13:46 +00:00
Check space redirects before page ID URLs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -354,6 +354,17 @@ describe('resolveMissingPagePath', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps a space redirect from a page ID path over the page with that ID', async () => {
|
||||
const context = createRedirectContext({ spaceRedirectPageId: page.id });
|
||||
const idPage = { ...page, id: 'wwCLFQyPl9cctUGxdLZm' };
|
||||
context.revision.pages = [page, idPage];
|
||||
|
||||
expect(await resolveMissingPagePath(context, 'pages/wwCLFQyPl9cctUGxdLZm')).toEqual({
|
||||
type: 'page',
|
||||
page: { page, ancestors: [] },
|
||||
});
|
||||
});
|
||||
|
||||
it('returns undefined when nothing matches', async () => {
|
||||
const context = createRedirectContext({});
|
||||
|
||||
|
||||
@@ -171,15 +171,6 @@ export async function resolveMissingPagePath(
|
||||
}
|
||||
}
|
||||
|
||||
// Content-ref page links (`/pages/<pageId>`) can reach visitors; IDs are case-sensitive, so match the raw pathname.
|
||||
const contentRef = resolveStringContentRef(redirectPathname);
|
||||
if (contentRef?.kind === 'page' && !contentRef.space) {
|
||||
const page = resolvePageId(revision.pages, contentRef.page);
|
||||
if (page) {
|
||||
return { type: 'page', page };
|
||||
}
|
||||
}
|
||||
|
||||
// If page still can't be found, we try with the API, in case we have a redirect at space level.
|
||||
// We use the raw pathname to handle special/malformed redirects setup by users in the GitSync.
|
||||
// The page rendering will take care of redirecting to a normalized pathname.
|
||||
@@ -190,8 +181,21 @@ export async function resolveMissingPagePath(
|
||||
path: rawPathname,
|
||||
})
|
||||
);
|
||||
const page = resolved ? resolvePageId(revision.pages, resolved.id) : undefined;
|
||||
return page ? { type: 'page', page } : undefined;
|
||||
const redirectedPage = resolved ? resolvePageId(revision.pages, resolved.id) : undefined;
|
||||
if (redirectedPage) {
|
||||
return { type: 'page', page: redirectedPage };
|
||||
}
|
||||
|
||||
// Content-ref page links (`/pages/<pageId>`) can reach visitors; IDs are case-sensitive, so match the raw pathname.
|
||||
const contentRef = resolveStringContentRef(redirectPathname);
|
||||
if (contentRef?.kind === 'page' && !contentRef.space) {
|
||||
const page = resolvePageId(revision.pages, contentRef.page);
|
||||
if (page) {
|
||||
return { type: 'page', page };
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user