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.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
45
46
47
48
49
  1. const { getPathWithoutLanguage, getPathWithoutVersion } = require('../lib/path-utils')
  2. const getLinkData = require('../lib/get-link-data')
  3. module.exports = async (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 track = req.context.site.data['learning-tracks'][req.context.currentProduct][trackName]
  12. if (!track) return noTrack()
  13. const currentLearningTrack = { trackName }
  14. const guidePath = getPathWithoutLanguage(getPathWithoutVersion(req.path))
  15. const guideIndex = track.guides.findIndex((path) => path === guidePath)
  16. if (guideIndex < 0) return noTrack()
  17. if (guideIndex > 0) {
  18. const prevGuidePath = track.guides[guideIndex - 1]
  19. const result = await getLinkData(prevGuidePath, req.context, { title: true, intro: false })
  20. if (!result) return noTrack()
  21. const href = result.href
  22. const title = result.title
  23. currentLearningTrack.prevGuide = { href, title }
  24. }
  25. if (guideIndex < track.guides.length - 1) {
  26. const nextGuidePath = track.guides[guideIndex + 1]
  27. const result = await getLinkData(nextGuidePath, req.context, { title: true, intro: false })
  28. if (!result) return noTrack()
  29. const href = result.href
  30. const title = result.title
  31. currentLearningTrack.nextGuide = { href, title }
  32. }
  33. req.context.currentLearningTrack = currentLearningTrack
  34. return next()
  35. }
Tip!

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

Comments

Loading...