@@ -48,18 +48,46 @@ export function listAllSiteSpaces(siteStructure: SiteStructure) {
48
48
/**
49
49
* Find a site space by its spaceId in a site structure.
50
50
*/
51
- export function findSiteSpaceById ( siteStructure : SiteStructure , spaceId : string ) : SiteSpace | null {
51
+ export function findSiteSpaceBy (
52
+ siteStructure : SiteStructure ,
53
+ predicate : ( siteSpace : SiteSpace ) => boolean
54
+ ) : {
55
+ siteSpace : SiteSpace ;
56
+ siteSection : SiteSection | null ;
57
+ siteSectionGroup : SiteSectionGroup | null ;
58
+ } | null {
52
59
if ( siteStructure . type === 'siteSpaces' ) {
53
- return siteStructure . structure . find ( ( siteSpace ) => siteSpace . space . id === spaceId ) ?? null ;
60
+ const siteSpace = siteStructure . structure . find ( predicate ) ?? null ;
61
+ if ( siteSpace ) {
62
+ return {
63
+ siteSpace,
64
+ siteSection : null ,
65
+ siteSectionGroup : null ,
66
+ } ;
67
+ }
68
+
69
+ return null ;
54
70
}
55
71
56
- for ( const section of siteStructure . structure ) {
57
- const siteSpace =
58
- section . object === 'site-section'
59
- ? findSiteSpaceByIdInSiteSpaces ( section . siteSpaces , spaceId )
60
- : findSiteSpaceByIdInSections ( section . sections , spaceId ) ;
61
- if ( siteSpace ) {
62
- return siteSpace ;
72
+ for ( const sectionOrGroup of siteStructure . structure ) {
73
+ if ( sectionOrGroup . object === 'site-section' ) {
74
+ const siteSpace = findSiteSpaceByIdInSiteSpaces ( sectionOrGroup . siteSpaces , predicate ) ;
75
+ if ( siteSpace ) {
76
+ return {
77
+ siteSpace,
78
+ siteSection : sectionOrGroup ,
79
+ siteSectionGroup : null ,
80
+ } ;
81
+ }
82
+ } else {
83
+ const found = findSiteSpaceByIdInSections ( sectionOrGroup . sections , predicate ) ;
84
+ if ( found ) {
85
+ return {
86
+ siteSpace : found . siteSpace ,
87
+ siteSection : found . siteSection ,
88
+ siteSectionGroup : sectionOrGroup ,
89
+ } ;
90
+ }
63
91
}
64
92
}
65
93
@@ -94,25 +122,37 @@ export function getSiteSpaceURL(context: GitBookSiteContext, siteSpace: SiteSpac
94
122
95
123
/**
96
124
* Get the path of a site space in the current site.
97
- * This doesn't return the most optimized path, as it doesn't take into account which one is the default one.
98
125
*/
99
126
export function getFallbackSiteSpacePath ( context : GitBookSiteContext , siteSpace : SiteSpace ) {
100
- const { sections } = context ;
101
- return sections ?. current ? joinPath ( sections . current . path , siteSpace . path ) : siteSpace . path ;
127
+ const found = findSiteSpaceBy ( context . structure , ( entry ) => entry . id === siteSpace . id ) ;
128
+ // don't include the path for the default site space
129
+ const siteSpacePath = siteSpace . default ? '' : siteSpace . path ;
130
+
131
+ // for non-default site sections, include the section path.
132
+ if ( found ?. siteSection && ! found ?. siteSection . default ) {
133
+ return joinPath ( found . siteSection . path , siteSpacePath ) ;
134
+ }
135
+
136
+ return siteSpacePath ;
102
137
}
103
138
104
- function findSiteSpaceByIdInSections ( sections : SiteSection [ ] , spaceId : string ) : SiteSpace | null {
105
- for ( const section of sections ) {
106
- const siteSpace =
107
- section . siteSpaces . find ( ( siteSpace ) => siteSpace . space . id === spaceId ) ?? null ;
139
+ function findSiteSpaceByIdInSections (
140
+ sections : SiteSection [ ] ,
141
+ predicate : ( siteSpace : SiteSpace ) => boolean
142
+ ) : { siteSpace : SiteSpace ; siteSection : SiteSection } | null {
143
+ for ( const siteSection of sections ) {
144
+ const siteSpace = siteSection . siteSpaces . find ( predicate ) ?? null ;
108
145
if ( siteSpace ) {
109
- return siteSpace ;
146
+ return { siteSpace, siteSection } ;
110
147
}
111
148
}
112
149
113
150
return null ;
114
151
}
115
152
116
- function findSiteSpaceByIdInSiteSpaces ( siteSpaces : SiteSpace [ ] , spaceId : string ) : SiteSpace | null {
117
- return siteSpaces . find ( ( siteSpace ) => siteSpace . space . id === spaceId ) ?? null ;
153
+ function findSiteSpaceByIdInSiteSpaces (
154
+ siteSpaces : SiteSpace [ ] ,
155
+ predicate : ( siteSpace : SiteSpace ) => boolean
156
+ ) : SiteSpace | null {
157
+ return siteSpaces . find ( predicate ) ?? null ;
118
158
}
0 commit comments