1
1
'use server' ;
2
2
3
3
import { resolvePageId } from '@/lib/pages' ;
4
- import { findSiteSpaceById } from '@/lib/sites' ;
4
+ import { findSiteSpaceById , getSiteStructureSections } from '@/lib/sites' ;
5
5
import { filterOutNullable } from '@/lib/typescript' ;
6
6
import { getV1BaseContext } from '@/lib/v1' ;
7
7
import type {
@@ -10,6 +10,8 @@ import type {
10
10
SearchAIRecommendedQuestionStream ,
11
11
SearchPageResult ,
12
12
SearchSpaceResult ,
13
+ SiteSection ,
14
+ SiteSectionGroup ,
13
15
Space ,
14
16
} from '@gitbook/api' ;
15
17
import type { GitBookBaseContext , GitBookSiteContext } from '@v2/lib/context' ;
@@ -19,6 +21,7 @@ import type * as React from 'react';
19
21
20
22
import { joinPathWithBaseURL } from '@/lib/paths' ;
21
23
import { isV2 } from '@/lib/v2' ;
24
+ import type { IconName } from '@gitbook/icons' ;
22
25
import { throwIfDataError } from '@v2/lib/data' ;
23
26
import { getSiteURLDataFromMiddleware } from '@v2/lib/middleware' ;
24
27
import { DocumentView } from '../DocumentView' ;
@@ -46,8 +49,7 @@ export interface ComputedPageResult {
46
49
pageId : string ;
47
50
spaceId : string ;
48
51
49
- /** When part of a multi-spaces search, the title of the space */
50
- spaceTitle ?: string ;
52
+ breadcrumbs ?: Array < { icon ?: IconName ; label : string } > ;
51
53
}
52
54
53
55
export interface AskAnswerSource {
@@ -258,7 +260,16 @@ async function searchSiteContent(
258
260
return (
259
261
await Promise . all (
260
262
searchResults . map ( async ( spaceItem ) => {
263
+ const sections = getSiteStructureSections ( structure ) . flatMap ( ( item ) =>
264
+ item . object === 'site-section-group' ? [ item , ...item . sections ] : item
265
+ ) ;
261
266
const siteSpace = findSiteSpaceById ( structure , spaceItem . id ) ;
267
+ const siteSection = sections . find (
268
+ ( section ) => section . id === siteSpace ?. section
269
+ ) as SiteSection ;
270
+ const siteSectionGroup = siteSection ?. sectionGroup
271
+ ? sections . find ( ( sectionGroup ) => sectionGroup . id === siteSection . sectionGroup )
272
+ : null ;
262
273
263
274
return Promise . all (
264
275
spaceItem . pages . map ( ( pageItem ) =>
@@ -267,6 +278,8 @@ async function searchSiteContent(
267
278
spaceItem,
268
279
space : siteSpace ?. space ,
269
280
spaceURL : siteSpace ?. urls . published ,
281
+ siteSection : siteSection ?? undefined ,
282
+ siteSectionGroup : ( siteSectionGroup as SiteSectionGroup ) ?? undefined ,
270
283
} )
271
284
)
272
285
) ;
@@ -348,9 +361,11 @@ async function transformSitePageResult(
348
361
spaceItem : SearchSpaceResult ;
349
362
space ?: Space ;
350
363
spaceURL ?: string ;
364
+ siteSection ?: SiteSection ;
365
+ siteSectionGroup ?: SiteSectionGroup ;
351
366
}
352
367
) : Promise < OrderedComputedResult [ ] > {
353
- const { pageItem, spaceItem, space, spaceURL } = args ;
368
+ const { pageItem, spaceItem, space, spaceURL, siteSection , siteSectionGroup } = args ;
354
369
const { linker } = context ;
355
370
356
371
const page : ComputedPageResult = {
@@ -360,12 +375,26 @@ async function transformSitePageResult(
360
375
href : spaceURL
361
376
? linker . toLinkForContent ( joinPathWithBaseURL ( spaceURL , pageItem . path ) )
362
377
: linker . toPathInSpace ( pageItem . path ) ,
363
- spaceTitle : space ?. title ,
364
378
pageId : pageItem . id ,
365
379
spaceId : spaceItem . id ,
380
+ breadcrumbs : [
381
+ siteSectionGroup && {
382
+ icon : siteSectionGroup ?. icon as IconName ,
383
+ label : siteSectionGroup . title ,
384
+ } ,
385
+ siteSection && {
386
+ icon : siteSection ?. icon as IconName ,
387
+ label : siteSection . title ,
388
+ } ,
389
+ ( ! siteSection || siteSection ?. siteSpaces . length > 1 ) && space
390
+ ? {
391
+ label : space ?. title ,
392
+ }
393
+ : undefined ,
394
+ ] . filter ( ( item ) => item !== undefined ) ,
366
395
} ;
367
396
368
- const sections = await Promise . all (
397
+ const pageSections = await Promise . all (
369
398
pageItem . sections ?. map < Promise < ComputedSectionResult > > ( async ( section ) => ( {
370
399
type : 'section' ,
371
400
id : `${ page . id } /${ section . id } ` ,
@@ -379,5 +408,5 @@ async function transformSitePageResult(
379
408
} ) ) ?? [ ]
380
409
) ;
381
410
382
- return [ page , ...sections ] ;
411
+ return [ page , ...pageSections ] ;
383
412
}
0 commit comments