diff --git a/packages/gitbook/src/components/SitePage/plan.test.ts b/packages/gitbook/src/components/SitePage/plan.test.ts index 691d3ffa0..00736c4d9 100644 --- a/packages/gitbook/src/components/SitePage/plan.test.ts +++ b/packages/gitbook/src/components/SitePage/plan.test.ts @@ -74,7 +74,8 @@ function setup(options: { } ); const loadIcons = mock(async () => ({})); - return { context, dataFetcher, imageResizer, resolveRef, loadIcons }; + const loadGitMetadata = mock(async () => undefined); + return { context, dataFetcher, imageResizer, resolveRef, loadIcons, loadGitMetadata }; } const doc = (nodes: unknown[]) => ({ object: 'document', nodes }) as unknown as JSONDocument; @@ -98,6 +99,71 @@ describe('runSitePagePlan', () => { await done; }); + it('starts the Git metadata of the page for the Edit on Git action', async () => { + const s = setup({}); + await runSitePagePlan(s.context, page, s); + expect(s.loadGitMetadata).toHaveBeenCalledWith(s.context, page); + }); + + it('resolves cross-space links of the layout and the table of contents', async () => { + const ref = (space: string, page: string) => ({ kind: 'page', space, page }); + const s = setup({ + customization: { + header: { + primaryLink: ref('space-2', 'primary'), + links: [ + { + title: 'Docs', + to: ref('space-2', 'top'), + links: [{ title: 'API', to: ref('space-2', 'nested') }], + }, + { title: 'Here', to: { kind: 'page', page: 'same-space' } }, + ], + }, + footer: { groups: [{ title: 'More', links: [{ to: ref('space-2', 'footer') }] }] }, + announcement: { + enabled: true, + message: 'Hi', + link: { to: ref('space-2', 'announce') }, + }, + }, + }); + (s.context as unknown as { revision: unknown }).revision = { + pages: [ + { + type: 'group', + pages: [{ type: 'link', target: ref('space-2', 'toc') }], + }, + ], + }; + await runSitePagePlan(s.context, page, s); + const resolved = s.resolveRef.mock.calls.map(([d, r]) => [ + d, + (r as { page?: string }).page, + ]); + for (const name of ['primary', 'top', 'nested', 'footer', 'announce', 'toc']) { + expect(resolved).toContainEqual([null, name]); + } + expect(resolved).not.toContainEqual([null, 'same-space']); + }); + + it('skips hidden table of contents links and a disabled announcement', async () => { + const ref = (page: string) => ({ kind: 'page', space: 'space-2', page }); + const s = setup({ + customization: { + announcement: { enabled: false, message: 'Hi', link: { to: ref('announce') } }, + }, + }); + (s.context as unknown as { revision: unknown }).revision = { + pages: [ + { type: 'link', hidden: true, target: ref('hidden-link') }, + { type: 'group', hidden: true, pages: [{ type: 'link', target: ref('inside') }] }, + ], + }; + await runSitePagePlan(s.context, page, s); + expect(s.resolveRef).not.toHaveBeenCalled(); + }); + it('starts embeds from the document with the page space', async () => { const s = setup({ document: doc([ diff --git a/packages/gitbook/src/components/SitePage/plan.ts b/packages/gitbook/src/components/SitePage/plan.ts index 8e278fc9d..d683a01ab 100644 --- a/packages/gitbook/src/components/SitePage/plan.ts +++ b/packages/gitbook/src/components/SitePage/plan.ts @@ -10,6 +10,7 @@ import { isDocumentTableImageRecord, isStringArray, } from '@/components/DocumentView/Table/utils'; +import { getPageGitMetadata } from '@/components/PageActions/pageActionsData'; import type { GitBookSiteContext } from '@/lib/context'; import { getDataOrNull, getPageDocument } from '@/lib/data'; import { getSimplifiedContentType } from '@/lib/files'; @@ -34,6 +35,7 @@ type Deps = { options: ResolveContentRefOptions ) => Promise; loadIcons: (context: GitBookSiteContext, document: JSONDocument) => Promise; + loadGitMetadata: (context: GitBookSiteContext, page: RevisionPageDocument) => Promise; }; const defaultDeps: Deps = { @@ -48,6 +50,7 @@ const defaultDeps: Deps = { document, }) ), + loadGitMetadata: getPageGitMetadata, }; type Node = { object?: string; type?: string; data?: Record; nodes?: unknown }; @@ -117,6 +120,9 @@ export function runSitePagePlan( const ids = { spaceId: context.space.id, revisionId: context.revisionId, pageId: page.id }; + // The Edit on Git action reloads the page with its Git metadata, which the revision tree omits. + start(() => deps.loadGitMetadata(context, page)); + start(async () => { const links = await getDataOrNull(context.dataFetcher.listRevisionPageMetaLinks(ids)); for (const ref of [links?.canonical, ...(links?.alternates ?? [])]) { @@ -190,7 +196,7 @@ export function runSitePagePlan( }); }); - const planRef = (document: JSONDocument, ref: ContentRef, anchorText: boolean) => { + const planRef = (document: JSONDocument | null, ref: ContentRef, anchorText: boolean) => { if (needsRequest(ref, anchorText, context)) { resolveRef(document, ref, anchorText); } @@ -273,7 +279,28 @@ export function runSitePagePlan( } }; - const planSiteImages = () => { + const planSite = () => { + // Links the layout resolves: header, announcement, footer, and link pages of the table of contents. + const { header: headerLinks, footer: footerLinks, announcement } = context.customization; + const layoutRefs: (ContentRef | null | undefined)[] = [ + headerLinks.primaryLink, + // Same condition as `Announcement`, which renders nothing otherwise. + announcement?.enabled && announcement.message ? announcement.link?.to : undefined, + ...(headerLinks.links ?? []).flatMap((link) => [ + link.to, + ...(link.links ?? []).map((sub) => sub.to), + ]), + ...(footerLinks.groups ?? []).flatMap((group) => + (group.links ?? []).map((link) => link.to) + ), + ]; + visitPages(context.revision?.pages, (target) => layoutRefs.push(target)); + for (const ref of layoutRefs) { + if (ref) { + planRef(null, ref, false); + } + } + // Logos and the custom icon carry no size, so the layout measures them on every page. const { header, footer, favicon } = context.customization; if (header.logo) { @@ -304,7 +331,7 @@ export function runSitePagePlan( }; // After every helper above is defined, and inside `start` so nothing throws out of the plan. - start(async () => planSiteImages()); + start(async () => planSite()); return settle(pending); } @@ -320,6 +347,28 @@ function needsRequest(ref: ContentRef, anchorText: boolean, context: GitBookSite return ref.kind === 'anchor' && anchorText; } +/** Calls `fn` with the target of every link page, as the table of contents resolves them. */ +function visitPages(pages: unknown, fn: (target: ContentRef) => void) { + if (!Array.isArray(pages)) { + return; + } + for (const page of pages as { + type?: string; + hidden?: boolean; + target?: ContentRef; + pages?: unknown; + }[]) { + // The table of contents skips hidden pages and everything under them. + if (!page || page.hidden) { + continue; + } + if (page.type === 'link' && page.target) { + fn(page.target); + } + visitPages(page.pages, fn); + } +} + function visit(nodes: unknown, fn: (node: Node) => void) { if (!Array.isArray(nodes)) { return;