|
| 1 | +import { languageKeys } from './languages.js' |
| 2 | +import nonEnterpriseDefaultVersion from './non-enterprise-default-version.js' |
| 3 | + |
| 4 | +import { allVersions } from './all-versions.js' |
| 5 | +import { latest, supported } from './enterprise-server-releases.js' |
| 6 | + |
| 7 | +const languagePrefixRegex = new RegExp(`^/(${languageKeys.join('|')})/`) |
| 8 | +const nonEnterpriseDefaultVersionPrefix = `/${nonEnterpriseDefaultVersion}` |
| 9 | + |
| 10 | +// Return the new URI if there is one, otherwise return undefined. |
| 11 | +export default function getRedirect(uri, context) { |
| 12 | + const { redirects, pages } = context |
| 13 | + |
| 14 | + let language = 'en' |
| 15 | + let withoutLanguage = uri |
| 16 | + if (languagePrefixRegex.test(uri)) { |
| 17 | + language = uri.match(languagePrefixRegex)[1] |
| 18 | + withoutLanguage = uri.replace(languagePrefixRegex, '/') |
| 19 | + } |
| 20 | + |
| 21 | + let destination |
| 22 | + |
| 23 | + // `redirects` is sourced from more than one thing. The primary use |
| 24 | + // case is gathering up the `redirect_from` frontmatter key. |
| 25 | + // But we also has `developer.json` which contains legacy redirects. |
| 26 | + // For example, the `developer.json` will have entries such |
| 27 | + // `/enterprise/v4/enum/auditlogorderfield` which clearly is using |
| 28 | + // the old formatting of the version. So to leverage the redirects |
| 29 | + // from `developer.json` we'll look at it right away. |
| 30 | + if (withoutLanguage in redirects) { |
| 31 | + return `/${language}` + redirects[withoutLanguage] |
| 32 | + } |
| 33 | + |
| 34 | + let basicCorrection |
| 35 | + |
| 36 | + if (withoutLanguage.startsWith(nonEnterpriseDefaultVersionPrefix)) { |
| 37 | + // E.g. '/free-pro-team@latest/foo/bar' or '/free-pro-team@latest' |
| 38 | + basicCorrection = |
| 39 | + `/${language}` + withoutLanguage.replace(nonEnterpriseDefaultVersionPrefix, '') |
| 40 | + } else if (withoutLanguage.replace('/', '') in allVersions && !languagePrefixRegex.test(uri)) { |
| 41 | + // E.g. just '/github-ae@latest' or '/enterprise-cloud@latest' |
| 42 | + basicCorrection = `/${language}` + withoutLanguage |
| 43 | + return basicCorrection |
| 44 | + } |
| 45 | + |
| 46 | + if ( |
| 47 | + withoutLanguage === '/enterprise-server' || |
| 48 | + withoutLanguage.startsWith('/enterprise-server/') |
| 49 | + ) { |
| 50 | + // E.g. '/enterprise-server' or '/enterprise-server/3.0/foo' |
| 51 | + basicCorrection = |
| 52 | + `/${language}` + withoutLanguage.replace('/enterprise-server', `/enterprise-server@${latest}`) |
| 53 | + // If it's now just the version, without anything after, exit here |
| 54 | + if (withoutLanguage === '/enterprise-server') { |
| 55 | + return basicCorrection |
| 56 | + } |
| 57 | + // console.log({ basicCorrection }) |
| 58 | + } else if (withoutLanguage.startsWith('/enterprise-server@latest')) { |
| 59 | + // E.g. '/enterprise-server@latest' or '/enterprise-server@latest/3.3/foo' |
| 60 | + basicCorrection = |
| 61 | + `/${language}` + |
| 62 | + withoutLanguage.replace('/enterprise-server@latest', `/enterprise-server@${latest}`) |
| 63 | + // If it was *just* '/enterprise-server@latest' all that's needed is |
| 64 | + // the language but with 'latest' replaced with the value of `latest` |
| 65 | + if (withoutLanguage === '/enterprise-server@latest') { |
| 66 | + return basicCorrection |
| 67 | + } |
| 68 | + } else if ( |
| 69 | + withoutLanguage.startsWith('/enterprise/') && |
| 70 | + supported.includes(withoutLanguage.split('/')[2]) |
| 71 | + ) { |
| 72 | + // E.g. '/enterprise/3.3' or '/enterprise/3.3/foo' |
| 73 | + |
| 74 | + // If the URL is without a language, and no redirect is necessary, |
| 75 | + // but it has as version prefix, the language has to be there |
| 76 | + // otherwise it will never be found in `req.context.pages` |
| 77 | + const version = withoutLanguage.split('/')[2] |
| 78 | + if (withoutLanguage === `/enterprise/${version}`) { |
| 79 | + // E.g. `/enterprise/3.0` |
| 80 | + basicCorrection = |
| 81 | + `/${language}` + |
| 82 | + withoutLanguage.replace(`/enterprise/${version}`, `/enterprise-server@${version}`) |
| 83 | + return basicCorrection |
| 84 | + } else { |
| 85 | + basicCorrection = |
| 86 | + `/${language}` + |
| 87 | + withoutLanguage.replace(`/enterprise/${version}/`, `/enterprise-server@${version}/`) |
| 88 | + } |
| 89 | + } else if (withoutLanguage === '/enterprise') { |
| 90 | + // E.g. `/enterprise` exactly |
| 91 | + basicCorrection = `/${language}/enterprise-server@${latest}` |
| 92 | + return basicCorrection |
| 93 | + } else if ( |
| 94 | + withoutLanguage.startsWith('/enterprise/') && |
| 95 | + !supported.includes(withoutLanguage.split('/')[2]) |
| 96 | + ) { |
| 97 | + // E.g. '/en/enterprise/user/github/foo' |
| 98 | + // If the URL is without a language, and no redirect is necessary, |
| 99 | + // but it has as version prefix, the language has to be there |
| 100 | + // otherwise it will never be found in `req.context.pages` |
| 101 | + basicCorrection = |
| 102 | + `/${language}` + |
| 103 | + withoutLanguage |
| 104 | + .replace(`/enterprise/`, `/enterprise-server@${latest}/`) |
| 105 | + .replace('/user/', '/') |
| 106 | + } else if (withoutLanguage.startsWith('/insights')) { |
| 107 | + // E.g. '/insights/foo' |
| 108 | + basicCorrection = uri.replace('/insights', `${language}/enterprise-server@${latest}/insights`) |
| 109 | + } |
| 110 | + |
| 111 | + if (basicCorrection) { |
| 112 | + return ( |
| 113 | + getRedirect(basicCorrection, { |
| 114 | + redirects, |
| 115 | + pages, |
| 116 | + }) || basicCorrection |
| 117 | + ) |
| 118 | + } |
| 119 | + |
| 120 | + if (withoutLanguage.startsWith('/admin/')) { |
| 121 | + const prefix = `/enterprise-server@${latest}` |
| 122 | + let suffix = withoutLanguage |
| 123 | + if (suffix.startsWith('/admin/guides/')) { |
| 124 | + suffix = suffix.replace('/admin/guides/', '/admin/') |
| 125 | + } |
| 126 | + const newURL = prefix + suffix |
| 127 | + destination = redirects[newURL] || newURL |
| 128 | + } else if ( |
| 129 | + withoutLanguage.split('/')[1].includes('@') && |
| 130 | + withoutLanguage.split('/')[1] in allVersions |
| 131 | + ) { |
| 132 | + // E.g. '/enterprise-server@latest' or '/github-ae@latest' or '/enterprise-server@3.3' |
| 133 | + const majorVersion = withoutLanguage.split('/')[1].split('@')[0] |
| 134 | + const split = withoutLanguage.split('/') |
| 135 | + const version = split[1].split('@')[1] |
| 136 | + let prefix |
| 137 | + let suffix |
| 138 | + |
| 139 | + if (supported.includes(version) || version === 'latest') { |
| 140 | + prefix = `/${majorVersion}@${version}` |
| 141 | + suffix = '/' + split.slice(2).join('/') |
| 142 | + |
| 143 | + if ( |
| 144 | + suffix.includes('/user') || |
| 145 | + suffix.startsWith('/admin/guide') || |
| 146 | + suffix.startsWith('/articles/user') |
| 147 | + ) { |
| 148 | + suffix = tryReplacements(prefix, suffix, context) || suffix |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + const newURL = prefix + suffix |
| 153 | + if (newURL !== withoutLanguage) { |
| 154 | + // At least the prefix changed! |
| 155 | + destination = redirects[newURL] || newURL |
| 156 | + } else { |
| 157 | + destination = redirects[newURL] |
| 158 | + } |
| 159 | + } else if (withoutLanguage.startsWith('/desktop/guides/')) { |
| 160 | + // E.g. /desktop/guides/contributing-and-collaborat |
| 161 | + const newURL = withoutLanguage.replace('/desktop/guides/', '/desktop/') |
| 162 | + destination = redirects[newURL] || newURL |
| 163 | + } else { |
| 164 | + destination = redirects[withoutLanguage] |
| 165 | + } |
| 166 | + |
| 167 | + if (destination !== undefined) { |
| 168 | + // There's hope! Now we just need to attach the correct language |
| 169 | + // to the destination URL. |
| 170 | + return `/${language}${destination}` |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// Over time, we've developed multiple ambiguous patterns of URLs |
| 175 | +// You can't simply assume that all `/admin/guides` should become |
| 176 | +// `/admin` for example. |
| 177 | +// This function tries different string replacement on the suffix |
| 178 | +// (the pathname after the language and version part) until it |
| 179 | +// finds one string replacement that yields either a page or a redirect. |
| 180 | +function tryReplacements(prefix, suffix, { pages, redirects }) { |
| 181 | + const test = (suffix) => { |
| 182 | + const candidateAsRedirect = prefix + suffix |
| 183 | + const candidateAsURL = '/en' + candidateAsRedirect |
| 184 | + return candidateAsRedirect in redirects || candidateAsURL in pages |
| 185 | + } |
| 186 | + |
| 187 | + let attempt = suffix.replace('/user', '/github') |
| 188 | + if (test(attempt)) return attempt |
| 189 | + |
| 190 | + attempt = suffix.replace('/user', '') |
| 191 | + if (test(attempt)) return attempt |
| 192 | + |
| 193 | + attempt = suffix.replace('/admin/guides', '/admin') |
| 194 | + if (test(attempt)) return attempt |
| 195 | + |
| 196 | + attempt = suffix.replace('/admin/guides/user', '/admin/github') |
| 197 | + if (test(attempt)) return attempt |
| 198 | + |
| 199 | + attempt = suffix.replace('/admin/guides', '/admin').replace('/user', '/github') |
| 200 | + if (test(attempt)) return attempt |
| 201 | +} |
0 commit comments