Compare commits

...

4 Commits

Author SHA1 Message Date
Taran Vohra 1e1d75c55c Merge branch 'main' into taran/sections-in-resuable-content 2025-02-24 21:18:35 +05:30
Taran Vohra 818166b16c Merge branch 'main' into taran/sections-in-resuable-content 2025-02-24 19:47:20 +05:30
taranvohra 02342749ca changeset 2025-02-24 19:39:20 +05:30
taranvohra 303432ef9d Resolve sections in reusable content block 2025-02-24 19:38:55 +05:30
3 changed files with 36 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'gitbook': patch
---
Resolve sections in reusable content blocks
@@ -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 ? <ScrollSectionsList sections={sections} /> : null;
}
+28 -1
View File
@@ -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 { resolveOpenAPIBlock } 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<ResolvedContentRef | null>,
): Promise<DocumentSection[]> {
@@ -52,6 +54,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;