11const path = require ( 'path' )
22const findPageInVersion = require ( './find-page-in-version' )
33const products = Object . values ( require ( '../lib/all-products' ) )
4- const { getVersionedPathWithoutLanguage } = require ( './path-utils' )
4+ const { getVersionedPathWithLanguage , getPathWithLanguage } = require ( './path-utils' )
55const languageCodes = Object . keys ( require ( './languages' ) )
66const addTitlesToTree = require ( './site-tree-titles' )
77const allVersions = Object . keys ( require ( './all-versions' ) )
@@ -35,15 +35,16 @@ module.exports = async function buildSiteTree (pageMap, site, redirects) {
3535 return
3636 }
3737
38- product . href = item . href
38+ // we don't want versioned product links because these links already have a default version in them
39+ product . href = getPathWithLanguage ( item . href , languageCode )
3940
4041 // find the product TOC page and get TOC items
4142 const page = findPageInVersion ( item . href , pageMap , redirects , languageCode , version )
4243
4344 // skip if page can't be found in this version
4445 if ( ! page ) return
4546
46- product . categories = buildCategoriesTree ( page . tocItems , item . href , pageMap , redirects , version , languageCode )
47+ product . categories = buildCategoriesTree ( page . tocItems , product . href , pageMap , redirects , version , languageCode )
4748
4849 productTree [ item . id ] = product
4950 return null
@@ -67,11 +68,12 @@ function buildCategoriesTree (tocItems, productHref, pageMap, redirects, version
6768
6869 const categoryHref = path . join ( productHref , item . href )
6970
70- const versionedCategoryHref = getVersionedPathWithoutLanguage ( categoryHref , version )
71+ // we DO want versioned category links
72+ const versionedCategoryHref = getVersionedPathWithLanguage ( categoryHref , version , languageCode )
7173 category . href = versionedCategoryHref
7274
7375 // find the category TOC page and get its TOC items
74- const page = findPageInVersion ( categoryHref , pageMap , redirects , languageCode , version )
76+ const page = findPageInVersion ( versionedCategoryHref , pageMap , redirects , languageCode , version )
7577
7678 // skip if page can't be found in this version
7779 if ( ! page ) return
@@ -90,9 +92,9 @@ function buildCategoriesTree (tocItems, productHref, pageMap, redirects, version
9092 // if TOC contains maptopics, build a maptopics tree
9193 // otherwise build an articles tree
9294 if ( hasMaptopics ) {
93- category . maptopics = buildMaptopicsTree ( page . tocItems , categoryHref , pageMap , redirects , version , languageCode )
95+ category . maptopics = buildMaptopicsTree ( page . tocItems , versionedCategoryHref , pageMap , redirects , version , languageCode )
9496 } else {
95- category . articles = buildArticlesTree ( page . tocItems , categoryHref , pageMap , redirects , version , languageCode )
97+ category . articles = buildArticlesTree ( page . tocItems , versionedCategoryHref , pageMap , redirects , version , languageCode )
9698 }
9799 }
98100
@@ -102,7 +104,7 @@ function buildCategoriesTree (tocItems, productHref, pageMap, redirects, version
102104 return categoryTree
103105}
104106
105- function buildMaptopicsTree ( tocItems , categoryHref , pageMap , redirects , version , languageCode ) {
107+ function buildMaptopicsTree ( tocItems , versionedCategoryHref , pageMap , redirects , version , languageCode ) {
106108 const maptopicTree = { }
107109
108110 // for every maptopic in a category TOC...
@@ -111,38 +113,32 @@ function buildMaptopicsTree (tocItems, categoryHref, pageMap, redirects, version
111113 . forEach ( item => {
112114 const maptopic = { }
113115
114- const maptopicHref = path . join ( categoryHref , item . href )
115-
116- const versionedMaptopicHref = getVersionedPathWithoutLanguage ( maptopicHref , version )
116+ const versionedMaptopicHref = path . join ( versionedCategoryHref , item . href )
117117 maptopic . href = versionedMaptopicHref
118118
119- // we already have access to the child articles via the category TOC items
120- // but we still need the page to get the available versions
121- const page = findPageInVersion ( maptopicHref , pageMap , redirects , languageCode , version )
119+ // find the category TOC page and get its TOC items
120+ const page = findPageInVersion ( versionedMaptopicHref , pageMap , redirects , languageCode , version )
122121
123122 // skip if page can't be found in this version
124123 if ( ! page ) return
125124
126125 // if this is not a maptopic, return early
127126 if ( ! page . mapTopic ) return
128127
129- const childArticles = getChildArticles ( tocItems , item . href )
130-
131128 maptopic . title = page . title
132129 maptopic . shortTitle = page . shortTitle
133130 maptopic . hidden = page . hidden
134-
135131 // make the child articles accessible to the page object for maptopic rendering
136- if ( ! page . childArticles ) page . childArticles = childArticles
132+ maptopic . childArticles = getChildArticles ( tocItems , item . href )
133+ maptopic . articles = buildArticlesTree ( maptopic . childArticles , versionedCategoryHref , pageMap , redirects , version , languageCode )
137134
138- maptopic . articles = buildArticlesTree ( childArticles , categoryHref , pageMap , redirects , version , languageCode )
139135 maptopicTree [ versionedMaptopicHref ] = maptopic
140136 } )
141137
142138 return maptopicTree
143139}
144140
145- function buildArticlesTree ( tocItems , categoryHref , pageMap , redirects , version , languageCode ) {
141+ function buildArticlesTree ( tocItems , versionedCategoryHref , pageMap , redirects , version , languageCode ) {
146142 const articleTree = { }
147143
148144 // REST categories may not have TOC items
@@ -152,12 +148,11 @@ function buildArticlesTree (tocItems, categoryHref, pageMap, redirects, version,
152148 tocItems . forEach ( item => {
153149 const article = { }
154150
155- const articleHref = path . join ( categoryHref , item . href )
156-
157- const versionedArticleHref = getVersionedPathWithoutLanguage ( articleHref , version )
151+ const versionedArticleHref = path . join ( versionedCategoryHref , item . href )
158152 article . href = versionedArticleHref
159153
160- const page = findPageInVersion ( articleHref , pageMap , redirects , languageCode , version )
154+ // find the category TOC page and get its TOC items
155+ const page = findPageInVersion ( versionedArticleHref , pageMap , redirects , languageCode , version )
161156
162157 // skip if page can't be found in this version
163158 if ( ! page ) return
0 commit comments