|
| 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 | +} |
0 commit comments