forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
current-product-tree.js
32 lines (27 loc) · 1.06 KB
/
current-product-tree.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
32
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()
}