@@ -37,35 +37,39 @@ async function loadSiteTree (unversionedTree) {
3737 const rawTree = Object . assign ( { } , ( unversionedTree || await loadUnversionedTree ( ) ) )
3838 const siteTree = { }
3939
40+ // For every language...
4041 await Promise . all ( Object . keys ( languages ) . map ( async ( langCode ) => {
4142 const treePerVersion = { }
42-
43+ // in every version...
4344 await Promise . all ( versions . map ( async ( version ) => {
44- versionPages ( rawTree [ langCode ] )
45+ // "version" the pages.
46+ treePerVersion [ version ] = versionPages ( Object . assign ( { } , rawTree [ langCode ] ) , version )
47+ } ) )
4548
46- // This step can't be asynchronous because the order of child pages matters.
47- function versionPages ( item ) {
48- // Add a versioned href as a convenience for use in layouts.
49- item . href = item . page . permalinks
50- . find ( pl => pl . pageVersion === version || ( pl . pageVersion === 'homepage' && version === nonEnterpriseDefaultVersion ) )
51- . href
49+ siteTree [ langCode ] = treePerVersion
50+ } ) )
5251
53- if ( ! item . childPages ) return item
52+ return siteTree
53+ }
5454
55- // Drop child pages that do not apply to the current version.
56- item . childPages = item . childPages . filter ( childPage => childPage . page . applicableVersions . includes ( version ) )
55+ // This step can't be asynchronous because the order of child pages matters.
56+ function versionPages ( obj , version ) {
57+ // Add a versioned href as a convenience for use in layouts.
58+ obj . href = obj . page . permalinks
59+ . find ( pl => pl . pageVersion === version || ( pl . pageVersion === 'homepage' && version === nonEnterpriseDefaultVersion ) )
60+ . href
5761
58- // Run on the child pages recursively.
59- item . childPages . forEach ( childPage => versionPages ( childPage ) )
60- }
62+ if ( ! obj . childPages ) return obj
6163
62- treePerVersion [ version ] = rawTree [ langCode ]
63- } ) )
64+ const versionedChildPages = obj . childPages
65+ // Drop child pages that do not apply to the current version.
66+ . filter ( childPage => childPage . page . applicableVersions . includes ( version ) )
67+ // Version the child pages recursively.
68+ . map ( childPage => versionPages ( Object . assign ( { } , childPage ) , version ) )
6469
65- siteTree [ langCode ] = treePerVersion
66- } ) )
70+ obj . childPages = [ ...versionedChildPages ]
6771
68- return siteTree
72+ return obj
6973}
7074
7175// Derive a flat array of Page objects in all languages.
0 commit comments