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

learning-track.js 1.6 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
  1. const { getPathWithoutLanguage, getPathWithoutVersion } = require('../lib/path-utils')
  2. const getLinkData = require('../lib/get-link-data')
  3. module.exports = async function learningTrack (req, res, next) {
  4. const noTrack = () => {
  5. req.context.currentLearningTrack = {}
  6. return next()
  7. }
  8. if (!req.context.page) return next()
  9. const trackName = req.query.learn
  10. if (!trackName) return noTrack()
  11. const tracksPerProduct = req.context.site.data['learning-tracks'][req.context.currentProduct]
  12. if (!tracksPerProduct) return noTrack()
  13. const track = req.context.site.data['learning-tracks'][req.context.currentProduct][trackName]
  14. if (!track) return noTrack()
  15. const currentLearningTrack = { trackName }
  16. const guidePath = getPathWithoutLanguage(getPathWithoutVersion(req.pagePath))
  17. const guideIndex = track.guides.findIndex((path) => path === guidePath)
  18. if (guideIndex < 0) return noTrack()
  19. if (guideIndex > 0) {
  20. const prevGuidePath = track.guides[guideIndex - 1]
  21. const result = await getLinkData(prevGuidePath, req.context, { title: true, intro: false })
  22. if (!result) return noTrack()
  23. const href = result.href
  24. const title = result.title
  25. currentLearningTrack.prevGuide = { href, title }
  26. }
  27. if (guideIndex < track.guides.length - 1) {
  28. const nextGuidePath = track.guides[guideIndex + 1]
  29. const result = await getLinkData(nextGuidePath, req.context, { title: true, intro: false })
  30. if (!result) return noTrack()
  31. const href = result.href
  32. const title = result.title
  33. currentLearningTrack.nextGuide = { href, title }
  34. }
  35. req.context.currentLearningTrack = currentLearningTrack
  36. return next()
  37. }
Tip!

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

Comments

Loading...