Skip to content

Commit 75deecd

Browse files
committed
add some clarifying comments
1 parent 5da9d74 commit 75deecd

1 file changed

Lines changed: 27 additions & 22 deletions

File tree

lib/path-utils.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,81 @@ const allVersions = require('./all-versions')
77
const supportedVersions = new Set(Object.keys(allVersions))
88
const { getNewVersionedPath } = require('./old-versions-utils')
99

10-
// construct appropriate versioned path for any given HREF
10+
// This function constructs an appropriate versioned path for any given HREF.
11+
// NOTE: this gets called by findPage and various other functions, and
12+
// has to return a proper versioned link given a wide variety of incoming
13+
// modern or legacy-formatted links, so it is somewhat overloaded. At some point
14+
// this could probably be broken up into separate functions to handle different incoming
15+
// paths. But it is currently optimized to handle lots of edge cases.
1116
function getVersionedPathWithoutLanguage (href, version) {
12-
// start clean without language code or trailing slash
17+
// Start clean without language code or trailing slash
1318
href = getPathWithoutLanguage(href.replace(patterns.trailingSlash, '$1'))
1419

15-
// if this is an old versioned path that includes a deprecated version, do not change!
20+
// If this is an old versioned path that includes a deprecated version, do not change!
1621
// example: /enterprise/11.10.340/admin/articles/upgrading-to-the-latest-release
1722
const oldEnterpriseVersionNumber = href.match(patterns.getEnterpriseVersionNumber)
1823
if (oldEnterpriseVersionNumber && deprecated.includes(oldEnterpriseVersionNumber[1])) {
1924
return href
2025
}
2126

22-
// try to derive the current version from the path
27+
// Try to derive the current version from the path
2328
// example: enterprise-server@2.22 or free-pro-team@latest
2429
let versionFromPath = getVersionStringFromPath(href)
2530

26-
// if a real version was found, add it to the path so we can go through the rest of the checks
31+
// If a supported version was found, add it to the path so we can go through the rest of the checks
2732
if (supportedVersions.has(versionFromPath)) {
2833
href = href.replace(href.split('/')[1], versionFromPath)
2934
}
3035

31-
// if the version found is NOT a currently supported version...
36+
// If a currently supported version was NOT found...
3237
let productObjectFromPath
3338
if (!supportedVersions.has(versionFromPath)) {
34-
// first check if the first segment is instead a current product;
39+
// First check if the segment is instead a current product;
3540
// example: /admin/foo or /desktop/foo
3641
productObjectFromPath = allProducts[versionFromPath]
3742

38-
// if so, add the first supported version for that product to the href
43+
// If so, add the first supported version for that product to the href
3944
// (this is just to get a path with all the expected segments; the version will be updated later if needed)
4045
if (productObjectFromPath) {
4146
href = path.join('/', productObjectFromPath.versions[0], href)
4247
versionFromPath = productObjectFromPath.versions[0]
4348
} else {
44-
// otherwise, this may be an old path that should be converted to new path;
49+
// Otherwise, this may be an old path that should be converted to new path;
4550
// OLD: /enterprise/2.22/admin/installation OR /enterprise/admin/installation
4651
// NEW: /enterprise-server@2.22/admin/installation
4752
href = getNewVersionedPath(href)
4853
versionFromPath = getVersionStringFromPath(href)
4954
}
5055
}
5156

52-
// if not previously found, derive the product object from the path (e.g., github or admin)
57+
// If not previously found, derive the product object from the path (e.g., github or admin)
5358
if (!productObjectFromPath) {
5459
productObjectFromPath = getProductObjectFromPath(href)
5560
}
5661

57-
// if the product's versions don't include the specified version, nothing to change!
62+
// If the product's versions don't include the specified version, nothing to change!
5863
if (productObjectFromPath && !productObjectFromPath.versions.includes(version)) {
5964
return slash(href)
6065
}
6166

62-
// update the version
67+
// Update the version and return the path
6368
return slash(href.replace(versionFromPath, version))
6469
}
6570

66-
// add language code
71+
// Add language code to a versioned path
6772
function getVersionedPathWithLanguage (href, version, languageCode) {
6873
return getPathWithLanguage(getVersionedPathWithoutLanguage(href, version), languageCode)
6974
}
7075

71-
// add the language to the given HREF
72-
// /en/articles/foo -> /articles/foo
76+
// Add the language to the given HREF
77+
// /articles/foo -> /en/articles/foo
7378
function getPathWithLanguage (href, languageCode) {
7479
return slash(path.posix.join('/', languageCode, getPathWithoutLanguage(href)))
7580
.replace(patterns.trailingSlash, '$1')
7681
}
7782

78-
// remove the language from the given HREF
79-
// /articles/foo -> /en/articles/foo
83+
// Remove the language from the given HREF
84+
// /en/articles/foo -> /articles/foo
8085
function getPathWithoutLanguage (href) {
8186
return slash(href.replace(patterns.hasLanguageCode, '/'))
8287
}
@@ -90,30 +95,30 @@ function getPathWithoutVersion (href) {
9095
function getVersionStringFromPath (href) {
9196
href = getPathWithoutLanguage(href)
9297

93-
// return immediately if this is a link to the homepage
98+
// Return immediately if this is a link to the homepage
9499
if (href === '/') {
95100
return 'homepage'
96101
}
97102

98-
// check if the first segment is a supported version
103+
// Check if the first segment is a supported version
99104
const versionFromPath = href.split('/')[1]
100105

101106
if (supportedVersions.has(versionFromPath)) {
102107
return versionFromPath
103108
}
104109

105-
// if the version segment is the latest enterprise-server release, return the latest release
110+
// If the version segment is the latest enterprise-server release, return the latest release
106111
if (versionFromPath === 'enterprise-server@latest') {
107112
return `enterprise-server@${latest}`
108113
}
109114

110-
// if it's just a plan with no @release (e.g., `enterprise-server`), return the latest release
115+
// If it's just a plan with no @release (e.g., `enterprise-server`), return the latest release
111116
const planObject = Object.values(allVersions).find(v => v.plan === versionFromPath)
112117
if (planObject) {
113118
return allVersions[planObject.latestVersion].version
114119
}
115120

116-
// otherwise, return the first segment as-is, which may not be a proper version
121+
// Otherwise, return the first segment as-is, which may not be a real supported version,
117122
// but additional checks are done on this segment in getVersionedPathWithoutLanguage
118123
return versionFromPath
119124
}

0 commit comments

Comments
 (0)