1
1
import { getSectionURL , getSiteSpaceURL } from '@/lib/sites' ;
2
- import type { SiteSection , SiteSectionGroup } from '@gitbook/api' ;
2
+ import type { SiteSection , SiteSectionGroup , SiteSpace } from '@gitbook/api' ;
3
3
import type { GitBookSiteContext , SiteSections } from '@v2/lib/context' ;
4
4
5
5
export type ClientSiteSections = {
@@ -33,10 +33,14 @@ export function encodeClientSiteSections(context: GitBookSiteContext, sections:
33
33
title : item . title ,
34
34
icon : item . icon ,
35
35
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 ) ) ,
37
39
} ) ;
38
40
} else {
39
- clientSections . push ( encodeSection ( context , item ) ) ;
41
+ if ( shouldIncludeSection ( context , item ) ) {
42
+ clientSections . push ( encodeSection ( context , item ) ) ;
43
+ }
40
44
}
41
45
}
42
46
@@ -57,6 +61,33 @@ function encodeSection(context: GitBookSiteContext, section: SiteSection) {
57
61
} ;
58
62
}
59
63
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
+
60
91
/**
61
92
* Find the best default site space to navigate to for a givent section:
62
93
* 1. If we are on the default, continue on the default.
@@ -70,12 +101,19 @@ function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
70
101
return getSectionURL ( context , section ) ;
71
102
}
72
103
73
- const bestMatch = section . siteSpaces . find (
74
- ( siteSpace ) => siteSpace . path === currentSiteSpace . path
104
+ const bestMatch = section . siteSpaces . find ( ( siteSpace ) =>
105
+ areSiteSpacesEquivalent ( siteSpace , currentSiteSpace )
75
106
) ;
76
107
if ( bestMatch ) {
77
108
return getSiteSpaceURL ( context , bestMatch ) ;
78
109
}
79
110
80
111
return getSectionURL ( context , section ) ;
81
112
}
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