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

breadcrumbs.js 3.0 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  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. module.exports = async (req, res, next) => {
  6. if (!req.context.page) return next()
  7. if (req.context.page.hidden) return next()
  8. req.context.breadcrumbs = {}
  9. // Return an empty object on the landing page
  10. if (req.context.page.relativePath === 'index.md') return next()
  11. const rawPath = getPathWithoutLanguage(req.path)
  12. const pathParts = rawPath.split('/')
  13. // drop first '/'
  14. pathParts.shift()
  15. const productPath = path.posix.join('/', req.context.currentProduct)
  16. const product = req.context.siteTree[req.language][req.context.currentVersion].products[req.context.currentProduct]
  17. if (!product) {
  18. return next()
  19. }
  20. req.context.breadcrumbs.product = {
  21. href: removeFPTFromPath(path.posix.join('/', req.context.currentLanguage, req.context.currentVersion, productPath)),
  22. title: product.title
  23. }
  24. // if this is not FPT, drop the version segment so pathParts now starts with /product
  25. // if this is FPT, there is no version segment so pathParts already starts with /product
  26. if (req.context.currentVersion !== nonEnterpriseDefaultVersion) {
  27. pathParts.shift()
  28. }
  29. if (!pathParts[1]) return next()
  30. // get category path
  31. // e.g., `getting-started-with-github` in /free-pro-team@latest/github/getting-started-with-github
  32. // or /enterprise-server@2.21/github/getting-started-with-github
  33. const categoryPath = removeFPTFromPath(path.posix.join('/', req.context.currentLanguage, req.context.currentVersion, productPath, pathParts[1]))
  34. const category = product.categories[categoryPath]
  35. if (!category) return next()
  36. req.context.breadcrumbs.category = {
  37. href: categoryPath,
  38. title: category.shortTitle || category.title
  39. }
  40. if (!pathParts[2]) return next()
  41. // get maptopic path
  42. // e.g., /github/getting-started-with-github/learning-about-github
  43. let maptopic
  44. if (req.context.page.mapTopic) {
  45. const maptopicPath = req.path
  46. maptopic = category.maptopics[maptopicPath]
  47. if (!maptopic) return next()
  48. req.context.breadcrumbs.maptopic = {
  49. href: maptopicPath,
  50. title: maptopic.shortTitle || maptopic.title
  51. }
  52. } else {
  53. const articlePath = req.path
  54. // find parent maptopic if one exists
  55. // some categories don't have maptopics, e.g. site-policy
  56. if (category.maptopics) {
  57. maptopic = Object.values(category.maptopics).find(maptopic => maptopic.articles[articlePath])
  58. if (maptopic) {
  59. req.context.breadcrumbs.maptopic = {
  60. href: maptopic.href,
  61. title: maptopic.shortTitle || maptopic.title
  62. }
  63. }
  64. }
  65. const articlePage = req.context.page
  66. const articleTitle = await articlePage.renderProp('shortTitle', req.context, { textOnly: true, encodeEntities: true })
  67. req.context.breadcrumbs.article = {
  68. href: articlePath,
  69. title: articleTitle
  70. }
  71. }
  72. return next()
  73. }
Tip!

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

Comments

Loading...