@@ -6,25 +6,30 @@ module.exports = ({
66 } ,
77} ) => {
88 let navGroups = site . keys . navGroups
9+
910 if ( ! navGroups ) return [ ]
1011 if ( navGroups . _compiled ) return navGroups
12+
1113 const components = site . components
1214 const componentNames = Object . keys ( components )
1315 const claimed = [ ]
14- navGroups = JSON . parse ( navGroups ) . reduce ( ( accum , navGroup ) => {
15- const componentNamesInGroup = ( navGroup . components || [ ] ) . reduce ( ( matched , componentName ) => {
16- if ( ~ componentName . indexOf ( '*' ) ) {
17- const rx = new RegExp ( `^${ componentName . replace ( / [ * ] / g, '.*?' ) } $` )
18- return matched . concat ( componentNames . filter ( ( candidate ) => rx . test ( candidate ) ) )
19- } else if ( componentName in components ) {
20- return matched . concat ( componentName )
21- }
22- return matched
23- } , [ ] )
16+
17+ navGroups = JSON . parse ( navGroups ) . map ( ( navGroup ) => {
18+ const componentNamesInGroup =
19+ ( navGroup . components || [ ] ) . flatMap (
20+ ( componentName ) => componentNames . filter ( globbify ( componentName ) ) )
21+
2422 claimed . push ( ...componentNamesInGroup )
25- return accum . concat ( compileNavGroup ( navGroup , componentNamesInGroup , contentCatalog , components ) )
26- } , [ ] )
27- const orphaned = componentNames . filter ( ( it ) => claimed . indexOf ( it ) < 0 )
23+
24+ return compileNavGroup (
25+ navGroup ,
26+ componentNamesInGroup ,
27+ contentCatalog ,
28+ components )
29+ } )
30+
31+ const orphaned = componentNames . filter ( ( it ) => ! claimed . includes ( it ) )
32+
2833 if ( orphaned . length ) {
2934 const homeIdx = orphaned . indexOf ( 'home' )
3035 if ( ~ homeIdx ) {
@@ -41,18 +46,22 @@ module.exports = ({
4146 : navGroups . push ( compileNavGroup ( { title : 'General' } , orphaned , contentCatalog , components ) )
4247 }
4348 }
49+
4450 navGroups . _compiled = true
45- return ( site . keys . navGroups = navGroups )
51+
52+ site . keys . navGroups = navGroups
53+ return navGroups
4654}
4755
4856function compileNavGroup ( navGroup , componentNamesInGroup , contentCatalog , components ) {
49- navGroup . components = componentNamesInGroup
5057 let startPage = navGroup . startPage
5158 if ( startPage ) {
5259 startPage = contentCatalog . resolvePage ( startPage )
5360 if ( startPage ) navGroup . url = startPage . pub . url
5461 delete navGroup . startPage
5562 }
63+
64+ navGroup . components = componentNamesInGroup
5665 if ( componentNamesInGroup . length ) {
5766 navGroup . latestVersions = componentNamesInGroup . reduce ( ( latestVersionMap , it ) => {
5867 latestVersionMap [ it ] = components [ it ] . latest . version
@@ -61,3 +70,8 @@ function compileNavGroup (navGroup, componentNamesInGroup, contentCatalog, compo
6170 }
6271 return navGroup
6372}
73+
74+ function globbify ( componentName ) {
75+ const rx = new RegExp ( `^${ componentName . replace ( / [ * ] / g, '.*?' ) } $` )
76+ return ( it ) => rx . test ( it )
77+ }
0 commit comments