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.1 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
97
98
99
100
101
  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 function breadcrumbs (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. if (!req.context.siteTree[req.language][req.context.currentVersion].products) {
  17. return next()
  18. }
  19. const product = req.context.siteTree[req.language][req.context.currentVersion].products[req.context.currentProduct]
  20. if (!product) {
  21. return next()
  22. }
  23. req.context.breadcrumbs.product = {
  24. href: removeFPTFromPath(path.posix.join('/', req.context.currentLanguage, req.context.currentVersion, productPath)),
  25. title: product.title
  26. }
  27. // if this is not FPT, drop the version segment so pathParts now starts with /product
  28. // if this is FPT, there is no version segment so pathParts already starts with /product
  29. if (req.context.currentVersion !== nonEnterpriseDefaultVersion) {
  30. pathParts.shift()
  31. }
  32. if (!pathParts[1]) return next()
  33. // get category path
  34. // e.g., `getting-started-with-github` in /free-pro-team@latest/github/getting-started-with-github
  35. // or /enterprise-server@2.21/github/getting-started-with-github
  36. const categoryPath = removeFPTFromPath(path.posix.join('/', req.context.currentLanguage, req.context.currentVersion, productPath, pathParts[1]))
  37. const category = product.categories[categoryPath]
  38. if (!category) return next()
  39. req.context.breadcrumbs.category = {
  40. href: categoryPath,
  41. title: category.shortTitle || category.title
  42. }
  43. if (!pathParts[2]) return next()
  44. // get maptopic path
  45. // e.g., /github/getting-started-with-github/learning-about-github
  46. let maptopic
  47. if (req.context.page.mapTopic) {
  48. const maptopicPath = req.path
  49. maptopic = category.maptopics[maptopicPath]
  50. if (!maptopic) return next()
  51. req.context.breadcrumbs.maptopic = {
  52. href: maptopicPath,
  53. title: maptopic.shortTitle || maptopic.title
  54. }
  55. } else {
  56. const articlePath = req.path
  57. // find parent maptopic if one exists
  58. // some categories don't have maptopics, e.g. site-policy
  59. if (category.maptopics) {
  60. maptopic = Object.values(category.maptopics).find(maptopic => maptopic.articles[articlePath])
  61. if (maptopic) {
  62. req.context.breadcrumbs.maptopic = {
  63. href: maptopic.href,
  64. title: maptopic.shortTitle || maptopic.title
  65. }
  66. }
  67. }
  68. const articlePage = req.context.page
  69. const articleTitle = await articlePage.renderProp('shortTitle', req.context, { textOnly: true, encodeEntities: true })
  70. req.context.breadcrumbs.article = {
  71. href: articlePath,
  72. title: articleTitle
  73. }
  74. }
  75. return next()
  76. }
Tip!

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

Comments

Loading...