11import path from 'path'
2- import findPage from '#src/frame/lib/find-page.js'
3- import nonEnterpriseDefaultVersion from '#src/versions/lib/non-enterprise-default-version.js'
4- import removeFPTFromPath from '#src/versions/lib/remove-fpt-from-path.js'
5- import { renderContent } from '#src/content-render/index.js'
6- import { executeWithFallback } from '#src/languages/lib/render-with-fallback.js'
2+ import findPage from '@/frame/lib/find-page'
3+ import nonEnterpriseDefaultVersion from '@/versions/lib/non-enterprise-default-version'
4+ import removeFPTFromPath from '@/versions/lib/remove-fpt-from-path'
5+ import { renderContent } from '@/content-render/index'
6+ import { executeWithFallback } from '@/languages/lib/render-with-fallback'
7+ import { Context , LinkOptions , ProcessedLink } from './types'
78
89// rawLinks is an array of paths: [ '/foo' ]
910// we need to convert it to an array of localized objects: [ { href: '/en/foo', title: 'Foo', intro: 'Description here' } ]
10- export default async (
11- rawLinks ,
12- context ,
13- option = { title : true , intro : true , fullTitle : false } ,
11+ export default async function getLinkData (
12+ rawLinks : string [ ] | string | undefined ,
13+ context : Context ,
14+ options : LinkOptions = { title : true , intro : true , fullTitle : false } ,
1415 maxLinks = Infinity ,
15- ) = > {
16- if ( ! rawLinks ) return
16+ ) : Promise < ProcessedLink [ ] | undefined > {
17+ if ( ! rawLinks ) return undefined
1718
1819 if ( typeof rawLinks === 'string' ) {
19- return await processLink ( rawLinks , context , option )
20+ const processedLink = await processLink ( rawLinks , context , options )
21+ return processedLink ? [ processedLink ] : undefined
2022 }
2123
22- const links = [ ]
24+ const links : ProcessedLink [ ] = [ ]
2325 // Using a for loop here because the async work is not network or
2426 // disk bound. It's CPU bound.
2527 // And if we use a for-loop we can potentially bail early if
2628 // the `maxLinks` is reached. That's instead of computing them all,
2729 // and then slicing the array. So it avoids wasted processing.
2830 for ( const link of rawLinks ) {
29- const processedLink = await processLink ( link , context , option )
31+ const processedLink = await processLink ( link , context , options )
3032 if ( processedLink ) {
3133 links . push ( processedLink )
3234 if ( links . length >= maxLinks ) {
@@ -38,9 +40,13 @@ export default async (
3840 return links
3941}
4042
41- async function processLink ( link , context , option ) {
42- const opts = { textOnly : true }
43- const linkHref = link . href || link
43+ async function processLink (
44+ link : string | { href : string } ,
45+ context : Context ,
46+ options : LinkOptions ,
47+ ) : Promise < ProcessedLink | null > {
48+ const opts : { textOnly : boolean ; preferShort ?: boolean } = { textOnly : true }
49+ const linkHref = typeof link === 'string' ? link : link . href
4450 // Parse the link in case it includes Liquid conditionals
4551 const linkPath = linkHref . includes ( '{' )
4652 ? await executeWithFallback (
@@ -55,29 +61,32 @@ async function processLink(link, context, option) {
5561 if ( ! linkPath ) return null
5662
5763 const version =
58- context . currentVersion === 'homepage' ? nonEnterpriseDefaultVersion : context . currentVersion
59- const href = removeFPTFromPath ( path . join ( '/' , context . currentLanguage , version , linkPath ) )
64+ ( context . currentVersion === 'homepage'
65+ ? nonEnterpriseDefaultVersion
66+ : context . currentVersion ) || 'free-pro-team@latest'
67+ const currentLanguage = context . currentLanguage || 'en'
68+ const href = removeFPTFromPath ( path . join ( '/' , currentLanguage , version , linkPath ) )
6069
61- const linkedPage = findPage ( href , context . pages , context . redirects )
70+ const linkedPage = findPage ( href , context . pages || { } , context . redirects || { } )
6271 if ( ! linkedPage ) {
6372 // This can happen when the link depends on Liquid conditionals,
6473 // like...
6574 // - '{% ifversion ghes %}/admin/foo/bar{% endifversion %}'
6675 return null
6776 }
6877
69- const result = { href, page : linkedPage }
78+ const result : ProcessedLink = { href, page : linkedPage }
7079
71- if ( option . title ) {
80+ if ( options . title ) {
7281 result . title = await linkedPage . renderTitle ( context , opts )
7382 }
7483
75- if ( option . fullTitle ) {
84+ if ( options . fullTitle ) {
7685 opts . preferShort = false
7786 result . fullTitle = await linkedPage . renderTitle ( context , opts )
7887 }
7988
80- if ( option . intro ) {
89+ if ( options . intro ) {
8190 result . intro = await linkedPage . renderProp ( 'intro' , context , opts )
8291 }
8392 return result
0 commit comments