1
0
mirror of synced 2026-01-06 06:02:35 -05:00
Files
docs/middleware/contextualizers/breadcrumbs.js
2021-04-20 14:04:42 -04:00

39 lines
1.3 KiB
JavaScript

module.exports = async function breadcrumbs (req, res, next) {
if (!req.context.page) return next()
if (req.context.page.hidden) return next()
req.context.breadcrumbs = []
// Return an empty array on the landing page.
if (req.context.page.documentType === 'homepage') {
return next()
}
const currentSiteTree = req.context.siteTree[req.context.currentLanguage][req.context.currentVersion]
await createBreadcrumb(
// Array of child pages on the root, i.e., the product level.
currentSiteTree.childPages,
req.context
)
return next()
}
async function createBreadcrumb (pageArray, context) {
// Find each page in the siteTree's array of child pages that starts with the requested path.
const childPage = pageArray.find(page => context.currentPath.startsWith(page.href))
context.breadcrumbs.push({
documentType: childPage.page.documentType,
href: childPage.href,
title: childPage.renderedShortTitle || childPage.renderedFullTitle
})
// Recursively loop through the siteTree and create each breadcrumb, until we reach the
// point where the current siteTree page is the same as the requested page. Then stop.
if (childPage.childPages && context.currentPath !== childPage.href) {
createBreadcrumb(childPage.childPages, context)
}
}