1
0
mirror of synced 2025-12-21 19:06:49 -05:00
Files
docs/lib/get-map-topic-content.js
Sarah Schneider ee7b67bbb5 lint
2020-12-17 12:10:21 -05:00

32 lines
1.4 KiB
JavaScript

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']
}