@@ -9,7 +9,7 @@ export function registerFilters(eleventyConfig: UserConfig): void {
9
9
eleventyConfig . addFilter ( 'toSimpleDate' , toSimpleDate ) ;
10
10
eleventyConfig . addFilter ( 'regexReplace' , regexReplace ) ;
11
11
eleventyConfig . addFilter ( 'toISOString' , toISOString ) ;
12
- eleventyConfig . addFilter ( 'activeNavEntryIndexArray ' , activeNavEntryIndexArray ) ;
12
+ eleventyConfig . addFilter ( 'activeNavForPage ' , activeNavForPage ) ;
13
13
eleventyConfig . addFilter ( 'arrayToSentenceString' , arrayToSentenceString ) ;
14
14
eleventyConfig . addFilter ( 'underscoreBreaker' , underscoreBreaker ) ;
15
15
eleventyConfig . addFilter ( 'throwError' , function ( error : any ) {
@@ -60,38 +60,49 @@ function toISOString(input: string | Date): string {
60
60
}
61
61
}
62
62
63
- function activeNavEntryIndexArray ( navEntryTree : any , pageUrlPath : string = '' ) : number [ ] | null {
64
- const activeEntryIndexes = _getActiveNavEntries ( navEntryTree , pageUrlPath ) ;
65
- return activeEntryIndexes . length === 0 ? null : activeEntryIndexes ;
66
- }
63
+ function activeNavForPage ( pageUrlPath : string , activeNav : any ) {
64
+ // Split the path for this page, dropping everything before the path.
65
+ // Example: dart.dev/tools/pub/package-layout ->
66
+ // [tools, pub, package-layout]
67
+ const parts = pageUrlPath . toLowerCase ( ) . split ( '/' ) . slice ( 1 ) ;
68
+ let currentPathPairs = activeNav ;
69
+ let lastAllowedBackupActive = [ ] ;
70
+
71
+ parts . forEach ( part => {
72
+ // If the current entry has active data,
73
+ // allow its active data to be a backup if a later path isn't found.
74
+ const currentEntryActiveData = currentPathPairs [ 'active' ] ;
75
+ if ( currentEntryActiveData ) {
76
+ lastAllowedBackupActive = currentEntryActiveData ;
77
+ }
67
78
68
- function _getActiveNavEntries ( navEntryTree : any , pageUrlPath = '' ) : number [ ] {
69
- // TODO(parlough): Simplify once standardizing with the Flutter site.
70
- for ( let i = 0 ; i < navEntryTree . length ; i ++ ) {
71
- const entry = navEntryTree [ i ] ;
72
-
73
- if ( entry . children ) {
74
- const descendantIndexes = _getActiveNavEntries (
75
- entry . children ,
76
- pageUrlPath ,
77
- ) ;
78
- if ( descendantIndexes . length > 0 ) {
79
- return [ i + 1 , ...descendantIndexes ] ;
80
- }
79
+ const paths = currentPathPairs [ 'paths' ] ;
80
+
81
+ // If the current entry has children paths, explore those next.
82
+ if ( paths ) {
83
+ currentPathPairs = paths ;
81
84
}
82
85
83
- if ( entry . permalink ) {
84
- const isMatch = entry [ 'match-page-url-exactly' ]
85
- ? pageUrlPath === entry . permalink
86
- : pageUrlPath . includes ( entry . permalink ) ;
86
+ // Get the data for the next part.
87
+ const nextPair = currentPathPairs [ part ] ;
87
88
88
- if ( isMatch ) {
89
- return [ i + 1 ] ;
90
- }
89
+ // If the next part of the path doesn't have data,
90
+ // use the active data for the current backup.
91
+ if ( nextPair === undefined || nextPair === null ) {
92
+ return lastAllowedBackupActive ;
91
93
}
94
+
95
+ currentPathPairs = nextPair ;
96
+ } ) ;
97
+
98
+ // If the last path part has active data, use that,
99
+ // otherwise fall back to the backup active data.
100
+ let activeEntries = currentPathPairs [ 'active' ] ;
101
+ if ( activeEntries === undefined || activeEntries === null ) {
102
+ activeEntries = lastAllowedBackupActive ;
92
103
}
93
104
94
- return [ ] ;
105
+ return activeEntries ;
95
106
}
96
107
97
108
function arrayToSentenceString ( list : string [ ] , joiner : string = 'and' ) : string {
0 commit comments