MCPcopy
hub / github.com/github/docs / getRedirect

Function getRedirect

lib/get-redirect.js:27–181  ·  view source on GitHub ↗
(uri, context)

Source from the content-addressed store, hash-verified

25
26// Return the new URI if there is one, otherwise return undefined.
27export default function getRedirect(uri, context) {
28 const { redirects, userLanguage } = context
29
30 const [language, withoutLanguage] = splitPathByLanguage(uri, userLanguage)
31
32 let destination
33
34 // `redirects` is sourced from more than one thing. The primary use
35 // case is gathering up the `redirect_from` frontmatter key.
36 // But we also have `developer.json` which contains legacy redirects.
37 // For example, the `developer.json` will have entries such
38 // `/enterprise/v4/enum/auditlogorderfield` which clearly is using
39 // the old formatting of the version. So to leverage the redirects
40 // from `developer.json` we'll look at it right away.
41 if (withoutLanguage in redirects) {
42 // But only inject the language if it's NOT an external redirect
43 if (redirects[withoutLanguage].includes('://')) {
44 return redirects[withoutLanguage]
45 }
46 return getPathWithLanguage(redirects[withoutLanguage], language)
47 }
48
49 let basicCorrection
50
51 if (withoutLanguage.startsWith(nonEnterpriseDefaultVersionPrefix)) {
52 // E.g. '/free-pro-team@latest/foo/bar' or '/free-pro-team@latest'
53 basicCorrection =
54 `/${language}` + withoutLanguage.replace(nonEnterpriseDefaultVersionPrefix, '')
55 } else if (withoutLanguage.replace('/', '') in allVersions && !languagePrefixRegex.test(uri)) {
56 // E.g. just '/github-ae@latest' or '/enterprise-cloud@latest'
57 basicCorrection = `/${language}` + withoutLanguage
58 return basicCorrection
59 }
60
61 if (
62 withoutLanguage === '/enterprise-server' ||
63 withoutLanguage.startsWith('/enterprise-server/')
64 ) {
65 // E.g. '/enterprise-server' or '/enterprise-server/3.0/foo'
66 basicCorrection =
67 `/${language}` + withoutLanguage.replace('/enterprise-server', `/enterprise-server@${latest}`)
68 // If it's now just the version, without anything after, exit here
69 if (withoutLanguage === '/enterprise-server') {
70 return basicCorrection
71 }
72 } else if (withoutLanguage.startsWith('/enterprise-server@latest')) {
73 // E.g. '/enterprise-server@latest' or '/enterprise-server@latest/3.3/foo'
74 basicCorrection =
75 `/${language}` +
76 withoutLanguage.replace('/enterprise-server@latest', `/enterprise-server@${latest}`)
77 // If it was *just* '/enterprise-server@latest' all that's needed is
78 // the language but with 'latest' replaced with the value of `latest`
79 if (withoutLanguage === '/enterprise-server@latest') {
80 return basicCorrection
81 }
82 } else if (
83 withoutLanguage.startsWith('/enterprise/') &&
84 supportedAndRecentlyDeprecated.includes(withoutLanguage.split('/')[2])

Callers 5

findPageFunction · 0.70
handleRedirectsFunction · 0.50
findPageFunction · 0.50
get-redirect.jsFile · 0.50

Calls 3

getPathWithLanguageFunction · 0.90
splitPathByLanguageFunction · 0.85
tryReplacementsFunction · 0.85

Tested by

no test coverage detected