1
0
mirror of synced 2025-12-22 11:26:57 -05:00

remove overloaded helper functions and move code directly where it is used

This commit is contained in:
Sarah Schneider
2021-01-07 13:14:44 -05:00
parent 355f074c89
commit 33f93e39c7
19 changed files with 158 additions and 266 deletions

View File

@@ -1,10 +1,11 @@
const path = require('path')
const findPageInVersion = require('./find-page-in-version')
const products = Object.values(require('../lib/all-products'))
const { getVersionedPathWithLanguage, getPathWithLanguage } = require('./path-utils')
const products = Object.values(require('./all-products'))
const languageCodes = Object.keys(require('./languages'))
const addTitlesToTree = require('./site-tree-titles')
const allVersions = Object.keys(require('./all-versions'))
const { getVersionStringFromPath } = require('./path-utils')
const getApplicableVersions = require('./get-applicable-versions')
const removeFPTFromPath = require('./remove-fpt-from-path')
// This module builds a localized tree of every page on the site
// It includes single-source pages that have different variants
@@ -35,16 +36,20 @@ module.exports = async function buildSiteTree (pageMap, site, redirects) {
return
}
// we don't want versioned product links because these links already have a default version in them
product.href = getPathWithLanguage(item.href, languageCode)
product.href = path.join('/', languageCode, item.href)
// find the product TOC page and get TOC items
const page = findPageInVersion(item.href, pageMap, redirects, languageCode, version)
// find the product TOC page so we have access to the TOC items
const page = pageMap[item.href] || pageMap[redirects[item.href]]
// skip if page can't be found in this version
if (!page) return
if (!getApplicableVersions(page.versions).includes(version)) return
product.categories = buildCategoriesTree(page.tocItems, product.href, pageMap, redirects, version, languageCode)
// item.hrefs have a default version via lib/all-products, so update to the current version
const versionFromPath = getVersionStringFromPath(item.href)
const versionedProductHref = removeFPTFromPath(path.join('/', languageCode, item.href.replace(versionFromPath, version)))
product.categories = buildCategoriesTree(page.tocItems, versionedProductHref, pageMap, redirects, version)
productTree[item.id] = product
return null
@@ -59,24 +64,21 @@ module.exports = async function buildSiteTree (pageMap, site, redirects) {
return siteTree
}
function buildCategoriesTree (tocItems, productHref, pageMap, redirects, version, languageCode) {
function buildCategoriesTree (tocItems, versionedProductHref, pageMap, redirects, version) {
const categoryTree = {}
// for every category in a product TOC...
tocItems.forEach(item => {
const category = {}
const categoryHref = path.join(productHref, item.href)
// we DO want versioned category links
const versionedCategoryHref = getVersionedPathWithLanguage(categoryHref, version, languageCode)
category.href = versionedCategoryHref
category.href = path.join(versionedProductHref, item.href)
// find the category TOC page and get its TOC items
const page = findPageInVersion(versionedCategoryHref, pageMap, redirects, languageCode, version)
const page = pageMap[category.href] || pageMap[redirects[category.href]]
// skip if page can't be found in this version
if (!page) return
if (!getApplicableVersions(page.versions).includes(version)) return
category.title = page.shortTitle || page.title
@@ -92,19 +94,19 @@ function buildCategoriesTree (tocItems, productHref, pageMap, redirects, version
// if TOC contains maptopics, build a maptopics tree
// otherwise build an articles tree
if (hasMaptopics) {
category.maptopics = buildMaptopicsTree(page.tocItems, versionedCategoryHref, pageMap, redirects, version, languageCode)
category.maptopics = buildMaptopicsTree(page.tocItems, category.href, pageMap, redirects, version)
} else {
category.articles = buildArticlesTree(page.tocItems, versionedCategoryHref, pageMap, redirects, version, languageCode)
category.articles = buildArticlesTree(page.tocItems, category.href, pageMap, redirects, version)
}
}
categoryTree[versionedCategoryHref] = category
categoryTree[category.href] = category
})
return categoryTree
}
function buildMaptopicsTree (tocItems, versionedCategoryHref, pageMap, redirects, version, languageCode) {
function buildMaptopicsTree (tocItems, versionedCategoryHref, pageMap, redirects, version) {
const maptopicTree = {}
// for every maptopic in a category TOC...
@@ -113,14 +115,14 @@ function buildMaptopicsTree (tocItems, versionedCategoryHref, pageMap, redirects
.forEach(item => {
const maptopic = {}
const versionedMaptopicHref = path.join(versionedCategoryHref, item.href)
maptopic.href = versionedMaptopicHref
maptopic.href = path.join(versionedCategoryHref, item.href)
// find the category TOC page and get its TOC items
const page = findPageInVersion(versionedMaptopicHref, pageMap, redirects, languageCode, version)
const page = pageMap[maptopic.href] || pageMap[redirects[maptopic.href]]
// skip if page can't be found in this version
if (!page) return
if (!getApplicableVersions(page.versions).includes(version)) return
// if this is not a maptopic, return early
if (!page.mapTopic) return
@@ -130,15 +132,15 @@ function buildMaptopicsTree (tocItems, versionedCategoryHref, pageMap, redirects
maptopic.hidden = page.hidden
// make the child articles accessible to the page object for maptopic rendering
maptopic.childArticles = getChildArticles(tocItems, item.href)
maptopic.articles = buildArticlesTree(maptopic.childArticles, versionedCategoryHref, pageMap, redirects, version, languageCode)
maptopic.articles = buildArticlesTree(maptopic.childArticles, versionedCategoryHref, pageMap, redirects, version)
maptopicTree[versionedMaptopicHref] = maptopic
maptopicTree[maptopic.href] = maptopic
})
return maptopicTree
}
function buildArticlesTree (tocItems, versionedCategoryHref, pageMap, redirects, version, languageCode) {
function buildArticlesTree (tocItems, versionedCategoryHref, pageMap, redirects, version) {
const articleTree = {}
// REST categories may not have TOC items
@@ -148,18 +150,18 @@ function buildArticlesTree (tocItems, versionedCategoryHref, pageMap, redirects,
tocItems.forEach(item => {
const article = {}
const versionedArticleHref = path.join(versionedCategoryHref, item.href)
article.href = versionedArticleHref
article.href = path.join(versionedCategoryHref, item.href)
// find the category TOC page and get its TOC items
const page = findPageInVersion(versionedArticleHref, pageMap, redirects, languageCode, version)
const page = pageMap[article.href] || pageMap[redirects[article.href]]
// skip if page can't be found in this version
if (!page) return
if (!getApplicableVersions(page.versions).includes(version)) return
article.title = page.shortTitle || page.title
articleTree[versionedArticleHref] = article
articleTree[article.href] = article
})
return articleTree