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,28 @@ 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
+ const { siteSpace : currentSiteSpace } = context ;
76
+
77
+ if ( section . siteSpaces . length === 1 ) {
78
+ return true ;
79
+ }
80
+
81
+ return section . siteSpaces . some ( ( siteSpace ) =>
82
+ areSiteSpacesEquivalent ( siteSpace , currentSiteSpace )
83
+ ) ;
84
+ }
85
+
60
86
/**
61
87
* Find the best default site space to navigate to for a givent section:
62
88
* 1. If we are on the default, continue on the default.
@@ -70,12 +96,19 @@ function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
70
96
return getSectionURL ( context , section ) ;
71
97
}
72
98
73
- const bestMatch = section . siteSpaces . find (
74
- ( siteSpace ) => siteSpace . path === currentSiteSpace . path
99
+ const bestMatch = section . siteSpaces . find ( ( siteSpace ) =>
100
+ areSiteSpacesEquivalent ( siteSpace , currentSiteSpace )
75
101
) ;
76
102
if ( bestMatch ) {
77
103
return getSiteSpaceURL ( context , bestMatch ) ;
78
104
}
79
105
80
106
return getSectionURL ( context , section ) ;
81
107
}
108
+
109
+ /**
110
+ * Test if 2 site spaces are equivalent.
111
+ */
112
+ function areSiteSpacesEquivalent ( siteSpace1 : SiteSpace , siteSpace2 : SiteSpace ) {
113
+ return siteSpace1 . path === siteSpace2 . path ;
114
+ }
0 commit comments