forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearning-track.js
52 lines (37 loc) · 1.63 KB
/
learning-track.js
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
import { getPathWithoutLanguage, getPathWithoutVersion } from '../lib/path-utils.js'
import getLinkData from '../lib/get-link-data.js'
export default async function learningTrack(req, res, next) {
const noTrack = () => {
req.context.currentLearningTrack = {}
return next()
}
if (!req.context.page) return next()
const trackName = req.query.learn
if (!trackName) return noTrack()
const tracksPerProduct = req.context.site.data['learning-tracks'][req.context.currentProduct]
if (!tracksPerProduct) return noTrack()
const track = req.context.site.data['learning-tracks'][req.context.currentProduct][trackName]
if (!track) return noTrack()
const currentLearningTrack = { trackName }
const guidePath = getPathWithoutLanguage(getPathWithoutVersion(req.pagePath))
const guideIndex = track.guides.findIndex((path) => path === guidePath)
if (guideIndex < 0) return noTrack()
if (guideIndex > 0) {
const prevGuidePath = track.guides[guideIndex - 1]
const result = await getLinkData(prevGuidePath, req.context, { title: true, intro: false })
if (!result) return noTrack()
const href = result.href
const title = result.title
currentLearningTrack.prevGuide = { href, title }
}
if (guideIndex < track.guides.length - 1) {
const nextGuidePath = track.guides[guideIndex + 1]
const result = await getLinkData(nextGuidePath, req.context, { title: true, intro: false })
if (!result) return noTrack()
const href = result.href
const title = result.title
currentLearningTrack.nextGuide = { href, title }
}
req.context.currentLearningTrack = currentLearningTrack
return next()
}