|
| 1 | +const path = require('path') |
| 2 | +const { getPathWithoutLanguage } = require('../lib/path-utils') |
| 3 | + |
| 4 | +// Early Access content doesn't conform to the same structure as other products, so we |
| 5 | +// can't derive breadcrumbs from the siteTree in the same way. Hence, this separate middleware. |
| 6 | +module.exports = async (req, res, next) => { |
| 7 | + if (!req.context.page) return next() |
| 8 | + if (!req.context.page.hidden) return next() |
| 9 | + |
| 10 | + req.context.breadcrumbs = {} |
| 11 | + |
| 12 | + const earlyAccessProduct = req.context.siteTree[req.language][req.context.currentVersion].products[req.context.currentProduct] |
| 13 | + |
| 14 | + req.context.breadcrumbs.earlyAccessProduct = { |
| 15 | + href: '', // no link for the EA product breadcrumb |
| 16 | + title: earlyAccessProduct.title |
| 17 | + } |
| 18 | + |
| 19 | + const rawPath = getPathWithoutLanguage(req.path) |
| 20 | + const pathParts = rawPath.split('/') |
| 21 | + |
| 22 | + // drop first '/' |
| 23 | + pathParts.shift() |
| 24 | + |
| 25 | + // drop the version and early-accesss segments so pathParts now starts with /product |
| 26 | + pathParts.shift() |
| 27 | + pathParts.shift() |
| 28 | + |
| 29 | + // Early Access products do not require an index.md, so look for a matching public product |
| 30 | + const product = req.context.siteTree[req.language][req.context.currentVersion].products[pathParts[0]] |
| 31 | + |
| 32 | + if (!product) return next() |
| 33 | + |
| 34 | + req.context.breadcrumbs.product = { |
| 35 | + href: '', // no link for the EA product breadcrumbs |
| 36 | + title: product.title |
| 37 | + } |
| 38 | + |
| 39 | + if (!pathParts[1]) return next() |
| 40 | + |
| 41 | + // get Early Access category path |
| 42 | + // e.g., `enforcing-best-practices-with-github-policies` in /free-pro-team@latest/early-access/github/enforcing-best-practices-with-github-policies |
| 43 | + const categoryPath = path.posix.join('/', req.context.currentVersion, 'early-access', pathParts[0], pathParts[1]) |
| 44 | + const category = req.context.pages[path.posix.join('/en', categoryPath)] |
| 45 | + |
| 46 | + if (!category) return next() |
| 47 | + |
| 48 | + req.context.breadcrumbs.category = { |
| 49 | + href: categoryPath, // do include a link for EA category breadcrumbs |
| 50 | + title: category.shortTitle || category.title |
| 51 | + } |
| 52 | + |
| 53 | + if (!pathParts[2]) return next() |
| 54 | + |
| 55 | + // for Early Access purposes, we don't need to differentiate between map topics and articles breadcrumbs |
| 56 | + const mapTopicOrArticlePath = path.posix.join(categoryPath, pathParts[2]) |
| 57 | + const mapTopicOrArticle = req.context.pages[path.posix.join('/en', mapTopicOrArticlePath)] |
| 58 | + |
| 59 | + if (!mapTopicOrArticle) return next() |
| 60 | + |
| 61 | + const mapTopicOrArticleTitle = await mapTopicOrArticle.renderProp('shortTitle', req.context, { textOnly: true, encodeEntities: true }) |
| 62 | + |
| 63 | + req.context.breadcrumbs.article = { |
| 64 | + href: mapTopicOrArticlePath, // do include a link for EA map topic/article breadcrumbs |
| 65 | + title: mapTopicOrArticleTitle |
| 66 | + } |
| 67 | + |
| 68 | + return next() |
| 69 | +} |
0 commit comments