11import { getSectionURL , getSiteSpaceURL } from '@/lib/sites' ;
2- import type { SiteSection , SiteSectionGroup } from '@gitbook/api' ;
2+ import type { SiteSection , SiteSectionGroup , SiteSpace } from '@gitbook/api' ;
33import type { GitBookSiteContext , SiteSections } from '@v2/lib/context' ;
44
55export type ClientSiteSections = {
@@ -33,10 +33,14 @@ export function encodeClientSiteSections(context: GitBookSiteContext, sections:
3333 title : item . title ,
3434 icon : item . icon ,
3535 object : item . object ,
36- sections : item . sections . map ( ( section ) => encodeSection ( context , section ) ) ,
36+ sections : item . sections
37+ . filter ( ( section ) => shouldIncludeSection ( context , section ) )
38+ . map ( ( section ) => encodeSection ( context , section ) ) ,
3739 } ) ;
3840 } else {
39- clientSections . push ( encodeSection ( context , item ) ) ;
41+ if ( shouldIncludeSection ( context , item ) ) {
42+ clientSections . push ( encodeSection ( context , item ) ) ;
43+ }
4044 }
4145 }
4246
@@ -57,6 +61,33 @@ function encodeSection(context: GitBookSiteContext, section: SiteSection) {
5761 } ;
5862}
5963
64+ /**
65+ * Test if a section should be included in the list of sections.
66+ */
67+ function shouldIncludeSection ( context : GitBookSiteContext , section : SiteSection ) {
68+ if ( context . site . id !== 'site_JOVzv' ) {
69+ return true ;
70+ }
71+
72+ // Testing for a new mode of navigation where the multi-variants section are hidden
73+ // if they do not include an equivalent of the current site space.
74+
75+ // TODO: replace with a proper flag on the section
76+ const withNavigateOnlyIfEquivalent = section . id === 'sitesc_4jvEm' ;
77+
78+ if ( ! withNavigateOnlyIfEquivalent ) {
79+ return true ;
80+ }
81+
82+ const { siteSpace : currentSiteSpace } = context ;
83+ if ( section . siteSpaces . length === 1 ) {
84+ return true ;
85+ }
86+ return section . siteSpaces . some ( ( siteSpace ) =>
87+ areSiteSpacesEquivalent ( siteSpace , currentSiteSpace )
88+ ) ;
89+ }
90+
6091/**
6192 * Find the best default site space to navigate to for a givent section:
6293 * 1. If we are on the default, continue on the default.
@@ -70,12 +101,19 @@ function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
70101 return getSectionURL ( context , section ) ;
71102 }
72103
73- const bestMatch = section . siteSpaces . find (
74- ( siteSpace ) => siteSpace . path === currentSiteSpace . path
104+ const bestMatch = section . siteSpaces . find ( ( siteSpace ) =>
105+ areSiteSpacesEquivalent ( siteSpace , currentSiteSpace )
75106 ) ;
76107 if ( bestMatch ) {
77108 return getSiteSpaceURL ( context , bestMatch ) ;
78109 }
79110
80111 return getSectionURL ( context , section ) ;
81112}
113+
114+ /**
115+ * Test if 2 site spaces are equivalent.
116+ */
117+ function areSiteSpacesEquivalent ( siteSpace1 : SiteSpace , siteSpace2 : SiteSpace ) {
118+ return siteSpace1 . path === siteSpace2 . path ;
119+ }
0 commit comments