forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearning-track.js
111 lines (84 loc) · 3.81 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { getPathWithoutLanguage, getPathWithoutVersion } from '../lib/path-utils.js'
import getLinkData from '../lib/get-link-data.js'
import renderContent from '../lib/render-content/renderContent.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()
let trackProduct = req.context.currentProduct
let tracksPerProduct = req.context.site.data['learning-tracks'][trackProduct]
// If there are no learning tracks for the current product, try and fall
// back to the learning track product set as a URL parameter. This handles
// the case where a learning track has guide paths for a different product
// than the current learning track product.
if (!tracksPerProduct) {
trackProduct = req.query.learnProduct
tracksPerProduct = req.context.site.data['learning-tracks'][trackProduct]
}
if (!tracksPerProduct) return noTrack()
const track = req.context.site.data['learning-tracks'][trackProduct][trackName]
if (!track) return noTrack()
const currentLearningTrack = { trackName, trackProduct }
const guidePath = getPathWithoutLanguage(getPathWithoutVersion(req.pagePath))
// The raw track.guides will return all guide paths, need to use getLinkData
// so we only get guides available in the current version
const trackGuides = await getLinkData(track.guides, req.context)
const trackGuidePaths = trackGuides.map((guide) => {
return getPathWithoutLanguage(getPathWithoutVersion(guide.href))
})
let guideIndex = trackGuidePaths.findIndex((path) => path === guidePath)
// The learning track path may use Liquid version conditionals, handle the
// case where the requested path is a learning track path but won't match
// because of a Liquid conditional.
if (guideIndex < 0) {
guideIndex = await indexOfLearningTrackGuide(trackGuidePaths, guidePath, req.context)
}
// Also check if the learning track path is now a redirect to the requested
// page, we still want to render the learning track banner in that case.
// Also handles Liquid conditionals in the track path.
if (guideIndex < 0) {
for (const redirect of req.context.page.redirect_from || []) {
if (guideIndex >= 0) break
guideIndex = await indexOfLearningTrackGuide(trackGuidePaths, redirect, req.context)
}
}
if (guideIndex < 0) return noTrack()
if (guideIndex > 0) {
const prevGuidePath = trackGuidePaths[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 < trackGuidePaths.length - 1) {
const nextGuidePath = trackGuidePaths[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()
}
// Find the index of a learning track guide path in an array of guide paths,
// return -1 if not found.
async function indexOfLearningTrackGuide(trackGuidePaths, guidePath, context) {
let guideIndex = -1
const renderOpts = { textOnly: true, encodeEntities: true }
for (let i = 0; i < trackGuidePaths.length; i++) {
// Learning track URLs may have Liquid conditionals.
const renderedGuidePath = await renderContent(trackGuidePaths[i], context, renderOpts)
if (!renderedGuidePath) continue
if (renderedGuidePath === guidePath) {
guideIndex = i
break
}
}
return guideIndex
}