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.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
  1. module.exports = async function breadcrumbs (req, res, next) {
  2. if (!req.context.page) return next()
  3. if (!req.context.page.relativePath.startsWith('early-access')) 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 earlyAccessProduct = req.context.siteTree[req.language][req.context.currentVersion].childPages.find(childPage => childPage.page.relativePath === 'early-access/index.md')
  10. if (!earlyAccessProduct) return next()
  11. // Create initial landing page breadcrumb
  12. req.context.breadcrumbs.push({
  13. documentType: earlyAccessProduct.page.documentType,
  14. href: '',
  15. title: earlyAccessProduct.page.title
  16. })
  17. // If this is the Early Access landing page, return now
  18. if (req.context.currentPath === earlyAccessProduct.href) {
  19. return next()
  20. }
  21. // Otherwise, create breadcrumbs
  22. await createBreadcrumb(
  23. earlyAccessProduct.childPages,
  24. req.context
  25. )
  26. return next()
  27. }
  28. async function createBreadcrumb (pageArray, context) {
  29. // Find each page in the siteTree's array of child pages that starts with the requested path.
  30. const childPage = pageArray.find(page => context.currentPath.startsWith(page.href))
  31. // Gray out product breadcrumb links and `Articles` categories
  32. const hideHref = childPage.page.documentType === 'product' ||
  33. (childPage.page.documentType === 'category' && childPage.page.relativePath.endsWith('/articles/index.md'))
  34. context.breadcrumbs.push({
  35. documentType: childPage.page.documentType,
  36. href: hideHref ? '' : childPage.href,
  37. title: await childPage.page.renderTitle(context, { textOnly: true, encodeEntities: true })
  38. })
  39. // Recursively loop through the siteTree and create each breadcrumb, until we reach the
  40. // point where the current siteTree page is the same as the requested page. Then stop.
  41. if (childPage.childPages && context.currentPath !== childPage.href) {
  42. createBreadcrumb(childPage.childPages, context)
  43. }
  44. }
Tip!

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

Comments

Loading...