From 303432ef9d73c79883d21be5ceb85917529848cf Mon Sep 17 00:00:00 2001 From: taranvohra Date: Mon, 24 Feb 2025 19:38:55 +0530 Subject: [PATCH 1/2] Resolve sections in reusable content block --- .../src/components/PageAside/PageAside.tsx | 4 ++- packages/gitbook/src/lib/document-sections.ts | 29 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/gitbook/src/components/PageAside/PageAside.tsx b/packages/gitbook/src/components/PageAside/PageAside.tsx index 0d85ec3ace..0d1d5cb5de 100644 --- a/packages/gitbook/src/components/PageAside/PageAside.tsx +++ b/packages/gitbook/src/components/PageAside/PageAside.tsx @@ -252,7 +252,9 @@ export async function PageAside(props: { async function PageAsideSections(props: { document: JSONDocument; context: ContentRefContext }) { const { document, context } = props; - const sections = await getDocumentSections(document, (ref) => resolveContentRef(ref, context)); + const sections = await getDocumentSections(context.space, document, (ref) => + resolveContentRef(ref, context), + ); return sections.length > 1 ? : null; } diff --git a/packages/gitbook/src/lib/document-sections.ts b/packages/gitbook/src/lib/document-sections.ts index 375b5812d3..1e13beccaa 100644 --- a/packages/gitbook/src/lib/document-sections.ts +++ b/packages/gitbook/src/lib/document-sections.ts @@ -1,5 +1,6 @@ -import { JSONDocument, ContentRef } from '@gitbook/api'; +import { JSONDocument, ContentRef, Space } from '@gitbook/api'; +import { getDocument } from './api'; import { getNodeText } from './document'; import { fetchOpenAPIBlock } from './openapi/fetch'; import { ResolvedContentRef } from './references'; @@ -16,6 +17,7 @@ export interface DocumentSection { * Extract a list of sections from a document. */ export async function getDocumentSections( + space: Space, document: JSONDocument, resolveContentRef: (ref: ContentRef) => Promise, ): Promise { @@ -49,6 +51,31 @@ export async function getDocumentSections( }); } } + + if (block.type === 'reusable-content') { + const resolved = await resolveContentRef(block.data.ref); + const documentId = resolved?.reusableContent?.document; + if (!documentId) { + throw new Error(`Expected a document ID for reusable content block`); + } + + const document = await getDocument(space.id, documentId); + if (!document) { + throw new Error(`Document not found for reusable content block`); + } + + const resuableContentSections = await getDocumentSections( + space, + document, + resolveContentRef, + ); + sections.push( + ...resuableContentSections.map((section) => ({ + ...section, + depth: section.depth + depth, + })), + ); + } } return sections; From 02342749ca7ff606f08a03ae171dc464745d5158 Mon Sep 17 00:00:00 2001 From: taranvohra Date: Mon, 24 Feb 2025 19:39:20 +0530 Subject: [PATCH 2/2] changeset --- .changeset/khaki-ravens-scream.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/khaki-ravens-scream.md diff --git a/.changeset/khaki-ravens-scream.md b/.changeset/khaki-ravens-scream.md new file mode 100644 index 0000000000..cfc952c50c --- /dev/null +++ b/.changeset/khaki-ravens-scream.md @@ -0,0 +1,5 @@ +--- +'gitbook': patch +--- + +Resolve sections in reusable content blocks