Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

early-access-breadcrumbs.js 2.9 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...