forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatured-links.js
51 lines (45 loc) · 1.62 KB
/
featured-links.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
import getLinkData from '../lib/get-link-data.js'
import renderContent from '../lib/render-content/index.js'
// this middleware adds properties to the context object
export default async function featuredLinks(req, res, next) {
if (!req.context.page) return next()
if (
!(
req.context.page.relativePath.endsWith('index.md') ||
req.context.page.layout === 'product-landing'
)
)
return next()
if (!req.context.page.featuredLinks) return next()
req.context.featuredLinks = {}
for (const key in req.context.page.featuredLinks) {
if (key === 'videos') {
// Videos are external URLs so don't run through getLinkData, they're
// objects with title and href properties.
// When the title contains Liquid versioning tags, it will be either
// the provided string title or an empty title. When the title is empty,
// it indicates the video is not versioned for the current version
req.context.featuredLinks[key] = []
for (let i = 0; i < req.context.page.featuredLinks[key].length; i++) {
const title = await renderContent(
req.context.page.featuredLinks[key][i].title,
req.context,
{
textOnly: true,
}
)
const item = { title, href: req.context.page.featuredLinks[key][i].href }
if (item.title) {
req.context.featuredLinks[key].push(item)
}
}
} else {
req.context.featuredLinks[key] = await getLinkData(
req.context.page.featuredLinks[key],
req.context,
{ title: true, intro: true, fullTitle: true }
)
}
}
return next()
}