|
1 | | -const findPage = require('./find-page') |
| 1 | +const { get, last } = require('lodash') |
| 2 | +const { getLanguageCode } = require('../lib/patterns') |
2 | 3 |
|
3 | | -// get the page.childArticles set on english map topics in lib/site-tree.js |
4 | | -module.exports = function getMapTopicContent (page, pageMap, redirects) { |
5 | | - const englishPage = page.languageCode !== 'en' |
6 | | - ? findPage(`/${page.relativePath.replace(/.md$/, '')}`, pageMap, redirects, 'en') |
7 | | - : page |
| 4 | +// get the childArticles set on map topics in lib/site-tree.js |
| 5 | +module.exports = function getMapTopicContent (productId, siteTree, currentLanguage, currentVersion, currentPath) { |
| 6 | + const maptopicPath = currentPath |
| 7 | + const categoryPath = currentPath.replace(new RegExp(`/${last(currentPath.split('/'))}$`), '') |
| 8 | + const siteTreePath = getSiteTreePath(currentVersion, productId, categoryPath, maptopicPath) |
| 9 | + let childArticles = get(siteTree[currentLanguage], siteTreePath) |
8 | 10 |
|
9 | | - if (!englishPage) { |
10 | | - console.error(`cannot find english page: ${page.fullPath}`) |
11 | | - return |
| 11 | + // try falling back to English if needed |
| 12 | + if (!childArticles && currentLanguage !== 'en') { |
| 13 | + const englishCategoryPath = categoryPath.replace(getLanguageCode, '/en') |
| 14 | + const englishMaptopicPath = maptopicPath.replace(getLanguageCode, '/en') |
| 15 | + const englishSiteTreePath = getSiteTreePath(currentVersion, productId, englishCategoryPath, englishMaptopicPath) |
| 16 | + childArticles = get(siteTree.en, englishSiteTreePath) |
12 | 17 | } |
13 | 18 |
|
14 | | - if (!englishPage.childArticles) { |
15 | | - console.error(`error getting child articles on map topic: ${page.fullPath}`) |
16 | | - return |
| 19 | + if (!childArticles) { |
| 20 | + console.error(`can't find child articles for map topic ${currentPath}`) |
| 21 | + return '' |
17 | 22 | } |
18 | 23 |
|
19 | | - return englishPage.childArticles |
| 24 | + return childArticles |
20 | 25 | .map(article => `{% link_with_intro /${article.href} %}`) |
21 | 26 | .join('\n\n') |
22 | 27 | } |
| 28 | + |
| 29 | +function getSiteTreePath (version, productId, categoryPath, maptopicPath) { |
| 30 | + return [version, 'products', productId, 'categories', categoryPath, 'maptopics', maptopicPath, 'childArticles'] |
| 31 | +} |
0 commit comments