@@ -9,25 +9,34 @@ import {
9
9
} from '#src/versions/lib/enterprise-server-releases.js'
10
10
import { getPathWithLanguage , getVersionStringFromPath } from '#src/frame/lib/path-utils.js'
11
11
12
+ import type { Context } from '@/types.js'
13
+
12
14
const languagePrefixRegex = new RegExp ( `^/(${ languageKeys . join ( '|' ) } )/` )
13
15
const nonEnterpriseDefaultVersionPrefix = `/${ nonEnterpriseDefaultVersion } `
14
16
15
17
const supportedAndRecentlyDeprecated = [ ...supported , ...deprecatedWithFunctionalRedirects ]
16
18
17
- export function splitPathByLanguage ( uri , userLanguage ) {
19
+ export function splitPathByLanguage ( uri : string , userLanguage ?: string ) : [ string , string ] {
18
20
let language = userLanguage || 'en'
19
21
let withoutLanguage = uri
20
22
if ( languagePrefixRegex . test ( uri ) ) {
21
- language = uri . match ( languagePrefixRegex ) [ 1 ]
22
- withoutLanguage = uri . replace ( languagePrefixRegex , '/' )
23
+ const match = uri . match ( languagePrefixRegex )
24
+ if ( match ) {
25
+ language = match [ 1 ]
26
+ withoutLanguage = uri . replace ( languagePrefixRegex , '/' )
27
+ }
23
28
}
24
29
return [ language , withoutLanguage ]
25
30
}
26
31
27
32
// Return the new URI if there is one, otherwise return undefined.
28
- export default function getRedirect ( uri , context ) {
33
+ export default function getRedirect ( uri : string , context : Context ) : string | undefined {
29
34
const { redirects, userLanguage } = context
30
35
36
+ if ( ! redirects ) {
37
+ return undefined
38
+ }
39
+
31
40
const [ language , withoutLanguage ] = splitPathByLanguage ( uri , userLanguage )
32
41
33
42
if ( withoutLanguage . startsWith ( '/github-ae@latest' ) ) {
@@ -43,7 +52,7 @@ export default function getRedirect(uri, context) {
43
52
return nonAERedirect
44
53
}
45
54
46
- let destination
55
+ let destination : string | undefined
47
56
48
57
// `redirects` is sourced from more than one thing. The primary use
49
58
// case is gathering up the `redirect_from` frontmatter key.
@@ -60,7 +69,7 @@ export default function getRedirect(uri, context) {
60
69
return getPathWithLanguage ( redirects [ withoutLanguage ] , language )
61
70
}
62
71
63
- let basicCorrection
72
+ let basicCorrection : string | undefined
64
73
65
74
if ( withoutLanguage . startsWith ( nonEnterpriseDefaultVersionPrefix ) ) {
66
75
// E.g. '/free-pro-team@latest/foo/bar' or '/free-pro-team@latest'
@@ -157,8 +166,8 @@ export default function getRedirect(uri, context) {
157
166
const majorVersion = withoutLanguage . split ( '/' ) [ 1 ] . split ( '@' ) [ 0 ]
158
167
const split = withoutLanguage . split ( '/' )
159
168
const version = split [ 1 ] . split ( '@' ) [ 1 ]
160
- let prefix
161
- let suffix
169
+ let prefix : string
170
+ let suffix : string
162
171
163
172
if ( supported . includes ( version ) || version === 'latest' ) {
164
173
prefix = `/${ majorVersion } @${ version } `
@@ -171,6 +180,10 @@ export default function getRedirect(uri, context) {
171
180
) {
172
181
suffix = tryReplacements ( prefix , suffix , context ) || suffix
173
182
}
183
+ } else {
184
+ // If version is not supported, we still need to set these values
185
+ prefix = `/${ majorVersion } @${ version } `
186
+ suffix = '/' + split . slice ( 2 ) . join ( '/' )
174
187
}
175
188
176
189
const newURL = prefix + suffix
@@ -193,11 +206,19 @@ export default function getRedirect(uri, context) {
193
206
// to the destination URL.
194
207
return `/${ language } ${ destination } `
195
208
}
209
+
210
+ return undefined
196
211
}
197
212
198
- function githubAERedirect ( uri , context ) {
213
+ function githubAERedirect ( uri : string , context : Context ) : string {
199
214
const { redirects, userLanguage, pages } = context
200
215
216
+ if ( ! redirects || ! pages ) {
217
+ // Fallback to home page if context is incomplete
218
+ const [ language ] = splitPathByLanguage ( uri , userLanguage )
219
+ return `/${ language } `
220
+ }
221
+
201
222
const [ language , withoutLanguage ] = splitPathByLanguage ( uri , userLanguage )
202
223
203
224
// From now on, github-ae@latest redirects to enterprise-cloud or
@@ -283,8 +304,14 @@ function githubAERedirect(uri, context) {
283
304
// This function tries different string replacement on the suffix
284
305
// (the pathname after the language and version part) until it
285
306
// finds one string replacement that yields either a page or a redirect.
286
- function tryReplacements ( prefix , suffix , { pages, redirects } ) {
287
- const test = ( suffix ) => {
307
+ function tryReplacements ( prefix : string , suffix : string , context : Context ) : string | undefined {
308
+ const { pages, redirects } = context
309
+
310
+ if ( ! pages || ! redirects ) {
311
+ return undefined
312
+ }
313
+
314
+ const test = ( suffix : string ) : boolean => {
288
315
// This is a generally broad search and replace and this particular
289
316
// replacement has never been present in api documentation only enterprise
290
317
// admin documentation, so we're excluding the REST api pages
@@ -310,4 +337,6 @@ function tryReplacements(prefix, suffix, { pages, redirects }) {
310
337
311
338
attempt = suffix . replace ( '/admin/guides' , '/admin' ) . replace ( '/user' , '/github' )
312
339
if ( test ( attempt ) ) return attempt
340
+
341
+ return undefined
313
342
}
0 commit comments