1+ const { defaultLanguage } = require ( '../locales' ) ;
12const { iterateEdges, mapToNavigationData } = require ( './createPageUtils' ) ;
23
34function getYamlPageIdentifier ( relativePath ) {
@@ -8,32 +9,76 @@ function getYamlPageIdentifier(relativePath) {
89 : relativePath . replace ( / ( \. [ a - z ] + ) ? \. ( m d x | m d ) / , '' ) ;
910}
1011
11- function createLearnPages ( learnEdges , yamlNavigationData ) {
12- const learnPages = [ ] ;
13- const navigationData = { } ;
12+ function getEdgesToLocaleMap ( edges ) {
13+ const mapEdgesToLocale = new Map ( ) ;
14+ edges . forEach ( edge => {
15+ const { locale } = edge . node . fields ;
16+
17+ if ( ! mapEdgesToLocale . has ( locale ) ) {
18+ mapEdgesToLocale . set ( locale , [ ] ) ;
19+ }
20+
21+ const localeEdges = mapEdgesToLocale . get ( locale ) ;
22+ localeEdges . push ( edge ) ;
23+ mapEdgesToLocale . set ( locale , localeEdges ) ;
24+ } ) ;
25+ return mapEdgesToLocale ;
26+ }
1427
15- const getLearnEdgeByPageId = pageId => edge =>
16- getYamlPageIdentifier ( edge . node . parent . relativePath ) === pageId ;
28+ const getLearnEdgeByPageId = pageId => edge =>
29+ getYamlPageIdentifier ( edge . node . parent . relativePath ) === pageId ;
1730
18- // Handles the Navigation Data only of Learn pages
19- yamlNavigationData . forEach ( ( { section, items } ) => {
20- navigationData [ section ] = [ ] ;
31+ const getIteratedPagesForYaml = ( yamlNavigation , edges ) => {
32+ const iteratedPagesForSection = { } ;
33+ yamlNavigation . forEach ( ( { section, items } ) => {
34+ iteratedPagesForSection [ section ] = [ ] ;
2135
2236 // This adds the items to the navigation section data based on the order defined within the YAML file
2337 // If the page doesn't exist it will be set as null and then removed via Array.filter()
2438 const iteratedPages = iterateEdges (
2539 items
2640 // Iterates the items of the section and retrieve their respective edges
2741 // then we transform them into pages and add to the navigation data
28- . map ( pageId => learnEdges . find ( getLearnEdgeByPageId ( pageId ) ) )
42+ // since learnPages are language independent we will use default edges
43+ . map ( pageId => edges . find ( getLearnEdgeByPageId ( pageId ) ) )
2944 . filter ( edge => edge && edge . node )
3045 ) ;
3146
47+ iteratedPagesForSection [ section ] = iteratedPages ;
48+ } ) ;
49+ return iteratedPagesForSection ;
50+ } ;
51+
52+ function getNavigationData ( yamlNavigation , edges ) {
53+ const navigationData = { } ;
54+ const iteratedPageForYaml = getIteratedPagesForYaml ( yamlNavigation , edges ) ;
55+ Object . entries ( iteratedPageForYaml ) . forEach ( ( [ section , iteratedPages ] ) => {
3256 navigationData [ section ] = iteratedPages . map ( mapToNavigationData ) ;
57+ } ) ;
58+ return navigationData ;
59+ }
3360
34- // Then we push them to the resulting learn pages object
61+ function getLearnPages ( yamlNavigation , edges ) {
62+ const learnPages = [ ] ;
63+ const iteratedPageForYaml = getIteratedPagesForYaml ( yamlNavigation , edges ) ;
64+ Object . values ( iteratedPageForYaml ) . forEach ( iteratedPages => {
3565 learnPages . push ( ...iteratedPages ) ;
3666 } ) ;
67+ return learnPages ;
68+ }
69+
70+ function createLearnPages ( learnEdges , yamlNavigationData ) {
71+ const mapEdgesToLocale = getEdgesToLocaleMap ( learnEdges ) ;
72+ const learnPages = getLearnPages (
73+ yamlNavigationData ,
74+ mapEdgesToLocale . get ( defaultLanguage )
75+ ) ;
76+
77+ const navigationData = { } ;
78+
79+ mapEdgesToLocale . forEach ( ( edges , locale ) => {
80+ navigationData [ locale ] = getNavigationData ( yamlNavigationData , edges ) ;
81+ } ) ;
3782
3883 return { learnPages, navigationData } ;
3984}
0 commit comments