Skip to content

Commit 802cb67

Browse files
committed
move the learning tracks processing into a separate module and return featured link in addition to learning tracks
1 parent 0d8af5b commit 802cb67

2 files changed

Lines changed: 44 additions & 16 deletions

File tree

lib/page.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const pathUtils = require('./path-utils')
1111
const Permalink = require('./permalink')
1212
const languages = require('./languages')
1313
const renderContent = require('./render-content')
14+
const processLearningTracks = require('./process-learning-tracks')
1415
const { renderReact } = require('./react/engine')
1516
const { productMap } = require('./all-products')
1617
const slash = require('slash')
@@ -207,23 +208,10 @@ class Page {
207208
this.permissions = await renderContent(this.rawPermissions, context)
208209
}
209210

211+
// Learning tracks may contain Liquid and need to have versioning processed.
210212
if (this.learningTracks) {
211-
const learningTracks = []
212-
for await (const rawTrackName of this.rawLearningTracks) {
213-
// Track names in frontmatter may include Liquid conditionals
214-
const renderedTrackName = await renderContent(rawTrackName, context, { textOnly: true, encodeEntities: true })
215-
if (!renderedTrackName) continue
216-
217-
const track = context.site.data['learning-tracks'][context.currentProduct][renderedTrackName]
218-
if (!track) continue
219-
learningTracks.push({
220-
trackName: renderedTrackName,
221-
title: await renderContent(track.title, context, { textOnly: true, encodeEntities: true }),
222-
description: await renderContent(track.description, context, { textOnly: true, encodeEntities: true }),
223-
guides: await getLinkData(track.guides, context)
224-
})
225-
}
226-
213+
const { featuredTrack, learningTracks } = await processLearningTracks(this.rawLearningTracks, context)
214+
this.featuredTrack = featuredTrack
227215
this.learningTracks = learningTracks
228216
}
229217

lib/process-learning-tracks.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const renderContent = require('./render-content')
2+
const getLinkData = require('./get-link-data')
3+
4+
const renderOpts = { textOnly: true, encodeEntities: true }
5+
6+
module.exports = async function processLearningTracks (rawLearningTracks, context) {
7+
const learningTracks = []
8+
9+
let featuredTrack
10+
11+
for await (const rawTrackName of rawLearningTracks) {
12+
// Track names in frontmatter may include Liquid conditionals
13+
const renderedTrackName = await renderContent(rawTrackName, context, renderOpts)
14+
if (!renderedTrackName) continue
15+
16+
const track = context.site.data['learning-tracks'][context.currentProduct][renderedTrackName]
17+
if (!track) continue
18+
19+
const learningTrack = {
20+
trackName: renderedTrackName,
21+
title: await renderContent(track.title, context, renderOpts),
22+
description: await renderContent(track.description, context, renderOpts),
23+
// getLinkData respects versioning and only returns guides available in the current version;
24+
// if no guides are available, the learningTrack.guides property will be an empty array.
25+
guides: await getLinkData(track.guides, context)
26+
}
27+
28+
if (track.featured_track) {
29+
// Set the featured track, which is not included in the array of other learning tracks.
30+
featuredTrack = learningTrack
31+
} else {
32+
// Only add the track to the array of tracks if there are guides in this version.
33+
if (learningTrack.guides.length) {
34+
learningTracks.push(learningTrack)
35+
}
36+
}
37+
}
38+
39+
return { featuredTrack, learningTracks }
40+
}

0 commit comments

Comments
 (0)