-
Notifications
You must be signed in to change notification settings - Fork 67.1k
Expand file tree
/
Copy pathget-link-data.ts
More file actions
106 lines (93 loc) · 3.41 KB
/
get-link-data.ts
File metadata and controls
106 lines (93 loc) · 3.41 KB
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
import path from 'path'
import findPage from '@/frame/lib/find-page'
import nonEnterpriseDefaultVersion from '@/versions/lib/non-enterprise-default-version'
import removeFPTFromPath from '@/versions/lib/remove-fpt-from-path'
import { renderContent } from '@/content-render/index'
import { executeWithFallback } from '@/languages/lib/render-with-fallback'
import type { Context, Page } from '@/types'
export interface LinkOptions {
title?: boolean
intro?: boolean
fullTitle?: boolean
}
export interface ProcessedLink {
href: string
page: Page
title?: string
fullTitle?: string
intro?: string
}
// 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' } ]
export default async function getLinkData(
rawLinks: string[] | string | undefined,
context: Context,
options: LinkOptions = { title: true, intro: true, fullTitle: false },
maxLinks = Infinity,
): Promise<ProcessedLink[] | undefined> {
if (!rawLinks) return undefined
if (typeof rawLinks === 'string') {
const processedLink = await processLink(rawLinks, context, options)
return processedLink ? [processedLink] : undefined
}
const links: ProcessedLink[] = []
// Using a for loop here because the async work is not network or
// disk bound. It's CPU bound.
// And if we use a for-loop we can potentially bail early if
// the `maxLinks` is reached. That's instead of computing them all,
// and then slicing the array. So it avoids wasted processing.
for (const link of rawLinks) {
const processedLink = await processLink(link, context, options)
if (processedLink) {
links.push(processedLink)
if (links.length >= maxLinks) {
break
}
}
}
return links
}
async function processLink(
link: string,
context: Context,
options: LinkOptions,
): Promise<ProcessedLink | null> {
const opts: { textOnly: boolean; preferShort?: boolean } = { textOnly: true }
// Parse the link in case it includes Liquid conditionals
const linkPath = link.includes('{')
? await executeWithFallback(
context,
() => renderContent(link, context, opts),
(enContext: Context) => renderContent(link, enContext, opts),
)
: link
// If the link was `{% ifversion ghes %}/admin/foo/bar{% endifversion %}`
// the `context.currentVersion` was `enterprise-cloud`, the final
// output would become '' (empty string).
if (!linkPath) return null
const version =
(context.currentVersion === 'homepage'
? nonEnterpriseDefaultVersion
: context.currentVersion) || 'free-pro-team@latest'
const currentLanguage = context.currentLanguage || 'en'
const href = removeFPTFromPath(path.join('/', currentLanguage, version, linkPath))
const linkedPage = findPage(href, context.pages || {}, context.redirects || {})
if (!linkedPage) {
// This can happen when the link depends on Liquid conditionals,
// like...
// - '{% ifversion ghes %}/admin/foo/bar{% endifversion %}'
return null
}
const result: ProcessedLink = { href, page: linkedPage }
if (options.title) {
result.title = await linkedPage.renderTitle(context, opts)
}
if (options.fullTitle) {
opts.preferShort = false
result.fullTitle = await linkedPage.renderTitle(context, opts)
}
if (options.intro) {
result.intro = await linkedPage.renderProp('intro', context, opts)
}
return result
}