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 1.5 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
  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. req.context.breadcrumbs = []
  5. // Return an empty array on the landing page.
  6. if (req.context.page.documentType === 'homepage') {
  7. return next()
  8. }
  9. const currentSiteTree = req.context.siteTree[req.context.currentLanguage][req.context.currentVersion]
  10. await createBreadcrumb(
  11. // Array of child pages on the root, i.e., the product level.
  12. currentSiteTree.childPages,
  13. req.context
  14. )
  15. return next()
  16. }
  17. async function createBreadcrumb (pageArray, context) {
  18. // Find each page in the siteTree's array of child pages that starts with the requested path.
  19. let childPage = pageArray.find(page => context.currentPath.startsWith(page.href))
  20. // Fall back to English if needed
  21. if (!childPage) {
  22. childPage = pageArray.find(page => context.currentPath.startsWith(page.href.replace(`/${context.currentLanguage}`, '/en')))
  23. if (!childPage) return
  24. }
  25. context.breadcrumbs.push({
  26. documentType: childPage.page.documentType,
  27. href: childPage.href,
  28. title: childPage.renderedShortTitle || childPage.renderedFullTitle
  29. })
  30. // Recursively loop through the siteTree and create each breadcrumb, until we reach the
  31. // point where the current siteTree page is the same as the requested page. Then stop.
  32. if (childPage.childPages && context.currentPath !== childPage.href) {
  33. createBreadcrumb(childPage.childPages, context)
  34. }
  35. }
Tip!

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

Comments

Loading...