Skip to content

Commit efa186c

Browse files
committed
restore legacy redirect handling needed for (internal docs) links
1 parent 7e4286f commit efa186c

1 file changed

Lines changed: 79 additions & 17 deletions

File tree

lib/redirects/get-old-paths-from-permalink.js

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,106 @@
1-
const { latest } = require('../enterprise-server-releases')
1+
const { latest, deprecated, lastReleaseWithLegacyFormat } = require('../enterprise-server-releases')
22
const { getPathWithoutLanguage, getPathWithLanguage, getVersionStringFromPath } = require('../path-utils')
3+
const patterns = require('../patterns')
4+
const versionSatisfiesRange = require('../version-satisfies-range')
35
const currentlySupportedVersions = Object.keys(require('../all-versions'))
46
const nonEnterpriseDefaultVersion = require('../non-enterprise-default-version')
57

68
// This function takes a current path, applies what we know about historically
79
// supported paths, and returns an array of ALL possible associated old
810
// paths that users might try to hit.
9-
module.exports = function getOldPathsFromPath (currentPath, languageCode) {
11+
module.exports = function getOldPathsFromPath (currentPath, languageCode, currentVersion) {
1012
const oldPaths = new Set()
1113

1214
const versionFromPath = getVersionStringFromPath(currentPath)
1315

14-
// The following only applies to Dotcom paths.
15-
// Create old path /free-pro-team@latest/github from new path /github (or from a frontmatter `redirect_from` path like /articles)
16-
if (versionFromPath === 'homepage' || !currentlySupportedVersions.includes(versionFromPath) || (versionFromPath === nonEnterpriseDefaultVersion && !currentPath.includes(nonEnterpriseDefaultVersion))) {
16+
// This only applies to Dotcom paths, so no need to determine whether the version is deprecated
17+
// create old path /free-pro-team@latest/github from new path /github (or from a frontmatter `redirect_from` path like /articles)
18+
if (versionFromPath === 'homepage' || !(currentlySupportedVersions.includes(versionFromPath) || deprecated.includes(versionFromPath)) || (versionFromPath === nonEnterpriseDefaultVersion && !currentPath.includes(nonEnterpriseDefaultVersion))) {
1719
oldPaths.add(currentPath
1820
.replace(`/${languageCode}`, `/${languageCode}/${nonEnterpriseDefaultVersion}`))
1921
}
2022

21-
oldPaths.add(currentPath
22-
.replace(/\/enterprise-server@(\d)/, '/enterprise/$1'))
23+
// ------ BEGIN LEGACY VERSION FORMAT REPLACEMENTS ------//
24+
// These remain relevant to handle legacy-formatted frontmatter redirects
25+
// and archived versions paths.
2326

24-
// create old path /enterprise/<version>/user from new path /enterprise-server@<version>/github
27+
// create old path /insights from current path /enterprise/version/insights
2528
oldPaths.add(currentPath
26-
.replace(/\/enterprise-server@(\d.+?)\/github/, '/enterprise/$1/user'))
29+
.replace(`/${languageCode}/enterprise/${latest}/user/insights`, '/insights'))
2730

28-
// create old path /insights from new path /enterprise-server@<latest>/insights
29-
oldPaths.add(currentPath
30-
.replace(`/enterprise-server@${latest}/insights`, '/insights'))
31+
// create old path /desktop/guides from current path /desktop
32+
// create old path /admin/guides from current path /admin
33+
if (!currentPath.includes('/guides')) {
34+
oldPaths.add(currentPath
35+
.replace('/desktop', '/desktop/guides')
36+
.replace('/admin', '/admin/guides'))
37+
}
38+
39+
// create old path /user from current path /user/github on 2.16+ only
40+
if (currentlySupportedVersions.includes(currentVersion) || versionSatisfiesRange(currentVersion, '>2.15')) {
41+
oldPaths.add(currentPath
42+
.replace('/user/github', '/user'))
43+
}
3144

32-
// create old path /admin from new path /enterprise-server@<latest>/admin
45+
// create old path /enterprise from current path /enterprise/latest
3346
oldPaths.add(currentPath
34-
.replace(`/enterprise-server@${latest}/admin`, '/admin'))
47+
.replace(`/enterprise/${latest}`, '/enterprise'))
48+
49+
// create old path /enterprise/foo from current path /enterprise/user/foo
50+
// this supports old developer paths like /enterprise/webhooks with no /user in them
51+
if (currentPath.includes('/enterprise/')) {
52+
oldPaths.add(currentPath
53+
.replace('/user/', '/'))
54+
}
3555

36-
// create old path /enterprise from new path /enterprise-server@<latest>
56+
// create old path /enterprise/foo from current path /enterprise/latest/user/foo
3757
oldPaths.add(currentPath
38-
.replace(`/enterprise-server@${latest}`, '/enterprise'))
58+
.replace(`/enterprise/${latest}/user/`, '/enterprise/'))
59+
60+
// ------ END LEGACY VERSION FORMAT REPLACEMENTS ------//
61+
62+
// ------ BEGIN MODERN VERSION FORMAT REPLACEMENTS ------//
63+
if (currentlySupportedVersions.includes(currentVersion) || versionSatisfiesRange(currentVersion, `>${lastReleaseWithLegacyFormat}`)) {
64+
(new Set(oldPaths)).forEach(oldPath => {
65+
// create old path /enterprise/<version> from new path /enterprise-server@<version>
66+
oldPaths.add(oldPath
67+
.replace(/\/enterprise-server@(\d)/, '/enterprise/$1'))
68+
69+
// create old path /enterprise/<version>/user from new path /enterprise-server@<version>/github
70+
oldPaths.add(oldPath
71+
.replace(/\/enterprise-server@(\d.+?)\/github/, '/enterprise/$1/user'))
72+
73+
// create old path /insights from new path /enterprise-server@<latest>/insights
74+
oldPaths.add(oldPath
75+
.replace(`/enterprise-server@${latest}/insights`, '/insights'))
76+
77+
// create old path /admin from new path /enterprise-server@<latest>/admin
78+
oldPaths.add(oldPath
79+
.replace(`/enterprise-server@${latest}/admin`, '/admin'))
80+
81+
// create old path /enterprise from new path /enterprise-server@<latest>
82+
oldPaths.add(oldPath
83+
.replace(`/enterprise-server@${latest}`, '/enterprise'))
84+
85+
// create old path /enterprise-server from new path /enterprise-server@<latest>
86+
oldPaths.add(oldPath
87+
.replace(`/enterprise-server@${latest}`, '/enterprise-server'))
88+
89+
if (!patterns.adminProduct.test(oldPath)) {
90+
// create old path /enterprise/<version>/user/foo from new path /enterprise-server@<version>/foo
91+
oldPaths.add(currentPath
92+
.replace(/\/enterprise-server@(\d.+?)\//, '/enterprise/$1/user/'))
93+
94+
// create old path /enterprise/user/foo from new path /enterprise-server@<latest>/foo
95+
oldPaths.add(currentPath
96+
.replace(`/enterprise-server@${latest}/`, '/enterprise/user/'))
97+
}
98+
})
99+
}
100+
// ------ END MODERN VERSION FORMAT REPLACEMENTS ------//
39101

40102
// For each old path added to the set above, do the following...
41-
oldPaths.forEach(oldPath => {
103+
(new Set(oldPaths)).forEach(oldPath => {
42104
// for English only, remove language code
43105
if (languageCode === 'en') {
44106
oldPaths.add(getPathWithoutLanguage(oldPath))

0 commit comments

Comments
 (0)