forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-map-topic-content.js
31 lines (26 loc) · 1.38 KB
/
get-map-topic-content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const { get, last } = require('lodash')
const { getLanguageCode } = require('../lib/patterns')
// get the childArticles set on map topics in lib/site-tree.js
module.exports = function getMapTopicContent (productId, siteTree, currentLanguage, currentVersion, currentPath) {
const maptopicPath = currentPath
const categoryPath = currentPath.replace(new RegExp(`/${last(currentPath.split('/'))}$`), '')
const siteTreePath = getSiteTreePath(currentVersion, productId, categoryPath, maptopicPath)
let childArticles = get(siteTree[currentLanguage], siteTreePath)
// try falling back to English if needed
if (!childArticles && currentLanguage !== 'en') {
const englishCategoryPath = categoryPath.replace(getLanguageCode, '/en')
const englishMaptopicPath = maptopicPath.replace(getLanguageCode, '/en')
const englishSiteTreePath = getSiteTreePath(currentVersion, productId, englishCategoryPath, englishMaptopicPath)
childArticles = get(siteTree.en, englishSiteTreePath)
}
if (!childArticles) {
console.error(`can't find child articles for map topic ${currentPath}`)
return ''
}
return childArticles
.map(article => `{% link_with_intro /${article.href} %}`)
.join('\n\n')
}
function getSiteTreePath (version, productId, categoryPath, maptopicPath) {
return [version, 'products', productId, 'categories', categoryPath, 'maptopics', maptopicPath, 'childArticles']
}