forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-link-data.js
52 lines (41 loc) · 1.65 KB
/
get-link-data.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
const path = require('path')
const findPage = require('./find-page')
const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
const removeFPTFromPath = require('./remove-fpt-from-path')
const renderContent = require('./render-content')
// rawLinks is an array of paths: [ '/foo' ]
// we need to convert it to an array of localized objects: [ { href: '/en/foo', title: 'Foo', intro: 'Description here' } ]
module.exports = async (rawLinks, context, option = { title: true, intro: true }) => {
if (!rawLinks) return
if (typeof rawLinks === 'string') {
return await processLink(rawLinks, context, option)
}
const links = []
for (const link of rawLinks) {
const linkObj = await processLink(link, context, option)
if (!linkObj) {
continue
} else {
links.push(linkObj)
}
}
return links
}
const processLink = async (link, context, option) => {
const opts = { textOnly: true, encodeEntities: true }
// Parse the link in case it includes Liquid conditionals
const linkPath = await renderContent((link.href || link), context, opts)
if (!linkPath) return null
const version = context.currentVersion === 'homepage' ? nonEnterpriseDefaultVersion : context.currentVersion
const href = removeFPTFromPath(path.join('/', context.currentLanguage, version, linkPath))
const linkedPage = findPage(href, context.pages, context.redirects)
if (!linkedPage) return null
const result = { href, page: linkedPage }
if (option.title) {
result.title = await linkedPage.renderTitle(context, opts)
}
if (option.intro) {
result.intro = await linkedPage.renderProp('intro', context, opts)
}
return result
}