| | 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 { Context, LinkOptions, ProcessedLink } from './types' |
| |
|
| | |
| | |
| | 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[] = [] |
| | |
| | |
| | |
| | |
| | |
| | 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 | { href: string }, |
| | context: Context, |
| | options: LinkOptions, |
| | ): Promise<ProcessedLink | null> { |
| | const opts: { textOnly: boolean; preferShort?: boolean } = { textOnly: true } |
| | const linkHref = typeof link === 'string' ? link : link.href |
| | |
| | const linkPath = linkHref.includes('{') |
| | ? await executeWithFallback( |
| | context, |
| | () => renderContent(linkHref, context, opts), |
| | () => '', |
| | ) |
| | : linkHref |
| | |
| | |
| | |
| | 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) { |
| | |
| | |
| | |
| | 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 |
| | } |
| |
|