Skip to content

Commit 974b3bb

Browse files
committed
add new middleware contextualizers that are feature flagged
1 parent 0a38c29 commit 974b3bb

4 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = async function breadcrumbs (req, res, next) {
2+
if (!req.context.page) return next()
3+
if (req.context.page.hidden) return next()
4+
5+
req.context.breadcrumbs = []
6+
7+
// Return an empty array on the landing page.
8+
if (req.context.page.documentType === 'homepage') {
9+
return next()
10+
}
11+
12+
const currentSiteTree = req.context.siteTree[req.context.currentLanguage][req.context.currentVersion]
13+
14+
await createBreadcrumb(
15+
// Array of child pages on the root, i.e., the product level.
16+
currentSiteTree.childPages,
17+
req.context
18+
)
19+
20+
return next()
21+
}
22+
23+
async function createBreadcrumb (pageArray, context) {
24+
// Find each page in the siteTree's array of child pages that starts with the requested path.
25+
const childPage = pageArray.find(page => context.currentPath.startsWith(page.href))
26+
27+
context.breadcrumbs.push({
28+
documentType: childPage.page.documentType,
29+
href: childPage.href,
30+
title: await childPage.page.renderTitle(context, { textOnly: true, encodeEntities: true })
31+
})
32+
33+
// Recursively loop through the siteTree and create each breadcrumb, until we reach the
34+
// point where the current siteTree page is the same as the requested page. Then stop.
35+
if (childPage.childPages && context.currentPath !== childPage.href) {
36+
createBreadcrumb(childPage.childPages, context)
37+
}
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function currentProductTree (req, res, next) {
2+
if (!req.context.page) return next()
3+
if (req.context.page.documentType === 'homepage') return next()
4+
5+
const currentSiteTree = req.context.siteTree[req.context.currentLanguage][req.context.currentVersion]
6+
7+
req.context.currentProductTree = currentSiteTree.childPages.find(page => req.context.currentPath.startsWith(page.href))
8+
9+
return next()
10+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module.exports = async function breadcrumbs (req, res, next) {
2+
if (!req.context.page) return next()
3+
if (!req.context.page.hidden) return next()
4+
5+
req.context.breadcrumbs = []
6+
7+
// Return an empty array on the landing page.
8+
if (req.context.page.documentType === 'homepage') {
9+
return next()
10+
}
11+
12+
const earlyAccessProduct = req.context.siteTree[req.language][req.context.currentVersion].childPages.find(childPage => childPage.page.relativePath === 'early-access/index.md')
13+
if (!earlyAccessProduct) return next()
14+
15+
// Create initial landing page breadcrumb
16+
req.context.breadcrumbs.push({
17+
documentType: earlyAccessProduct.page.documentType,
18+
href: '',
19+
title: earlyAccessProduct.page.title
20+
})
21+
22+
// If this is the Early Access landing page, return now
23+
if (req.context.currentPath === earlyAccessProduct.href) {
24+
return next()
25+
}
26+
27+
// Otherwise, create breadcrumbs
28+
await createBreadcrumb(
29+
earlyAccessProduct.childPages,
30+
req.context
31+
)
32+
33+
return next()
34+
}
35+
36+
async function createBreadcrumb (pageArray, context) {
37+
// Find each page in the siteTree's array of child pages that starts with the requested path.
38+
const childPage = pageArray.find(page => context.currentPath.startsWith(page.href))
39+
40+
// Gray out product breadcrumb links and `Articles` categories
41+
const hideHref = childPage.page.documentType === 'product' ||
42+
(childPage.page.documentType === 'category' && childPage.page.relativePath.endsWith('/articles/index.md'))
43+
44+
context.breadcrumbs.push({
45+
documentType: childPage.page.documentType,
46+
href: hideHref ? '' : childPage.href,
47+
title: await childPage.page.renderTitle(context, { textOnly: true, encodeEntities: true })
48+
})
49+
50+
// Recursively loop through the siteTree and create each breadcrumb, until we reach the
51+
// point where the current siteTree page is the same as the requested page. Then stop.
52+
if (childPage.childPages && context.currentPath !== childPage.href) {
53+
createBreadcrumb(childPage.childPages, context)
54+
}
55+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const { sortBy } = require('lodash')
2+
3+
module.exports = async function genericToc (req, res, next) {
4+
if (!req.context.page) return next()
5+
if (req.context.page.hidden) return next()
6+
if (req.context.currentLayoutName !== 'generic-toc') return next()
7+
8+
const currentSiteTree = req.context.siteTree[req.context.currentLanguage][req.context.currentVersion]
9+
10+
// Find the array of child pages that start with the requested path.
11+
const currentPageInSiteTree = findPageInSiteTree(currentSiteTree.childPages, req.context.currentPath)
12+
13+
const unsortedTocItems = await Promise.all(currentPageInSiteTree.childPages.map(async (childPage) => {
14+
// return an empty string if it's a hidden link on a non-hidden page (hidden links on hidden pages are OK)
15+
if (childPage.page.hidden && !req.context.page.hidden) {
16+
return ''
17+
}
18+
19+
const fullPath = childPage.href
20+
const title = await childPage.page.renderTitle(req.context, { textOnly: true, encodeEntities: true })
21+
const intro = await childPage.page.renderProp('intro', req.context, { unwrap: true })
22+
23+
return { fullPath, title, intro }
24+
}))
25+
26+
req.context.tocItems = sortBy(
27+
unsortedTocItems,
28+
// Sort by the ordered array of `children` in the frontmatter.
29+
currentPageInSiteTree.page.children
30+
)
31+
32+
return next()
33+
}
34+
35+
// Recursively loop through the siteTree until we reach the point where the
36+
// current siteTree page is the same as the requested page. Then stop.
37+
function findPageInSiteTree (pageArray, currentPath) {
38+
const childPage = pageArray.find(page => currentPath.startsWith(page.href))
39+
40+
if (childPage.href === currentPath) {
41+
return childPage
42+
}
43+
44+
return findPageInSiteTree(childPage.childPages, currentPath)
45+
}

0 commit comments

Comments
 (0)