1
0
mirror of synced 2025-12-22 11:26:57 -05:00
Files
docs/middleware/contextualizers/current-product-tree.js
Kevin Heis 8a56437c93 Pretty format (#20352)
* Update prettier flow to include JS

* Run prettier

* ...run prettier
2021-07-14 14:35:01 -07:00

33 lines
1.1 KiB
JavaScript

import path from 'path'
import findPageInSiteTree from '../../lib/find-page-in-site-tree.js'
import removeFPTFromPath from '../../lib/remove-fpt-from-path.js'
// This module adds currentProductTree to the context object for use in layouts.
export default function currentProductTree(req, res, next) {
if (!req.context.page) return next()
if (req.context.page.documentType === 'homepage') return next()
// We need this so we can fall back to English if localized pages are out of sync.
req.context.currentEnglishTree = req.context.siteTree.en[req.context.currentVersion]
const currentRootTree =
req.context.siteTree[req.context.currentLanguage][req.context.currentVersion]
const currentProductPath = removeFPTFromPath(
path.posix.join(
'/',
req.context.currentLanguage,
req.context.currentVersion,
req.context.currentProduct
)
)
const currentProductTree = findPageInSiteTree(
currentRootTree,
req.context.currentEnglishTree,
currentProductPath
)
req.context.currentProductTree = currentProductTree
return next()
}