11import type { GitBookAnyContext } from '@/lib/context' ;
22import type { DocumentBlock , JSONDocument } from '@gitbook/api' ;
33
4+ import { getDataOrNull } from './data' ;
45import { getNodeText } from './document' ;
56import { resolveOpenAPIOperationBlock } from './openapi/resolveOpenAPIOperationBlock' ;
67import { resolveOpenAPISchemasBlock } from './openapi/resolveOpenAPISchemasBlock' ;
8+ import { resolveContentRef } from './references' ;
79
810export interface DocumentSection {
911 id : string ;
@@ -28,10 +30,11 @@ export async function getDocumentSections(
2830 */
2931async function getSectionsFromNodes (
3032 nodes : DocumentBlock [ ] ,
31- context : GitBookAnyContext
33+ context : GitBookAnyContext ,
34+ initialDepth = 0
3235) : Promise < DocumentSection [ ] > {
3336 const sections : DocumentSection [ ] = [ ] ;
34- let depth = 0 ;
37+ let depth = initialDepth ;
3538
3639 for ( const block of nodes ) {
3740 switch ( block . type ) {
@@ -64,7 +67,9 @@ async function getSectionsFromNodes(
6467 }
6568 case 'stepper' : {
6669 const stepNodes = await Promise . all (
67- block . nodes . map ( async ( step ) => getSectionsFromNodes ( step . nodes , context ) )
70+ block . nodes . map ( async ( step ) =>
71+ getSectionsFromNodes ( step . nodes , context , depth )
72+ )
6873 ) ;
6974 for ( const stepSections of stepNodes ) {
7075 sections . push ( ...stepSections ) ;
@@ -116,6 +121,39 @@ async function getSectionsFromNodes(
116121 }
117122 continue ;
118123 }
124+ case 'reusable-content' : {
125+ const dataFetcher = block . meta ?. token
126+ ? context . dataFetcher . withToken ( { apiToken : block . meta . token } )
127+ : context . dataFetcher ;
128+
129+ const resolved = await resolveContentRef ( block . data . ref , {
130+ ...context ,
131+ dataFetcher,
132+ } ) ;
133+ if ( ! resolved ) {
134+ continue ;
135+ }
136+ const { reusableContent } = resolved ;
137+ if ( ! reusableContent ) {
138+ continue ;
139+ }
140+ const document = await getDataOrNull (
141+ dataFetcher . getRevisionReusableContentDocument ( {
142+ spaceId : reusableContent . context . space . id ,
143+ revisionId : reusableContent . context . revisionId ,
144+ reusableContentId : reusableContent . revisionReusableContent . id ,
145+ } )
146+ ) ;
147+ if ( ! document ) {
148+ continue ;
149+ }
150+ const reusableContentSections = await getSectionsFromNodes (
151+ document . nodes ,
152+ reusableContent . context ,
153+ depth
154+ ) ;
155+ sections . push ( ...reusableContentSections ) ;
156+ }
119157 }
120158 }
121159
0 commit comments