11'use server' ;
22
33import { resolvePageId } from '@/lib/pages' ;
4- import { findSiteSpaceById } from '@/lib/sites' ;
4+ import { findSiteSpaceById , getSiteStructureSections } from '@/lib/sites' ;
55import { filterOutNullable } from '@/lib/typescript' ;
66import { getV1BaseContext } from '@/lib/v1' ;
77import type {
@@ -10,6 +10,8 @@ import type {
1010 SearchAIRecommendedQuestionStream ,
1111 SearchPageResult ,
1212 SearchSpaceResult ,
13+ SiteSection ,
14+ SiteSectionGroup ,
1315 Space ,
1416} from '@gitbook/api' ;
1517import type { GitBookBaseContext , GitBookSiteContext } from '@v2/lib/context' ;
@@ -19,6 +21,7 @@ import type * as React from 'react';
1921
2022import { joinPathWithBaseURL } from '@/lib/paths' ;
2123import { isV2 } from '@/lib/v2' ;
24+ import type { IconName } from '@gitbook/icons' ;
2225import { throwIfDataError } from '@v2/lib/data' ;
2326import { getSiteURLDataFromMiddleware } from '@v2/lib/middleware' ;
2427import { DocumentView } from '../DocumentView' ;
@@ -46,8 +49,7 @@ export interface ComputedPageResult {
4649 pageId : string ;
4750 spaceId : string ;
4851
49- /** When part of a multi-spaces search, the title of the space */
50- spaceTitle ?: string ;
52+ breadcrumbs ?: Array < { icon ?: IconName ; label : string } > ;
5153}
5254
5355export interface AskAnswerSource {
@@ -258,7 +260,16 @@ async function searchSiteContent(
258260 return (
259261 await Promise . all (
260262 searchResults . map ( async ( spaceItem ) => {
263+ const sections = getSiteStructureSections ( structure ) . flatMap ( ( item ) =>
264+ item . object === 'site-section-group' ? [ item , ...item . sections ] : item
265+ ) ;
261266 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 ;
262273
263274 return Promise . all (
264275 spaceItem . pages . map ( ( pageItem ) =>
@@ -267,6 +278,8 @@ async function searchSiteContent(
267278 spaceItem,
268279 space : siteSpace ?. space ,
269280 spaceURL : siteSpace ?. urls . published ,
281+ siteSection : siteSection ?? undefined ,
282+ siteSectionGroup : ( siteSectionGroup as SiteSectionGroup ) ?? undefined ,
270283 } )
271284 )
272285 ) ;
@@ -348,9 +361,11 @@ async function transformSitePageResult(
348361 spaceItem : SearchSpaceResult ;
349362 space ?: Space ;
350363 spaceURL ?: string ;
364+ siteSection ?: SiteSection ;
365+ siteSectionGroup ?: SiteSectionGroup ;
351366 }
352367) : Promise < OrderedComputedResult [ ] > {
353- const { pageItem, spaceItem, space, spaceURL } = args ;
368+ const { pageItem, spaceItem, space, spaceURL, siteSection , siteSectionGroup } = args ;
354369 const { linker } = context ;
355370
356371 const page : ComputedPageResult = {
@@ -360,12 +375,26 @@ async function transformSitePageResult(
360375 href : spaceURL
361376 ? linker . toLinkForContent ( joinPathWithBaseURL ( spaceURL , pageItem . path ) )
362377 : linker . toPathInSpace ( pageItem . path ) ,
363- spaceTitle : space ?. title ,
364378 pageId : pageItem . id ,
365379 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 ) ,
366395 } ;
367396
368- const sections = await Promise . all (
397+ const pageSections = await Promise . all (
369398 pageItem . sections ?. map < Promise < ComputedSectionResult > > ( async ( section ) => ( {
370399 type : 'section' ,
371400 id : `${ page . id } /${ section . id } ` ,
@@ -379,5 +408,5 @@ async function transformSitePageResult(
379408 } ) ) ?? [ ]
380409 ) ;
381410
382- return [ page , ...sections ] ;
411+ return [ page , ...pageSections ] ;
383412}
0 commit comments