Skip to content

Commit a5f73cc

Browse files
authored
docs: fix version list creation for dropdown (#1391)
1 parent 178a23b commit a5f73cc

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

tools/bin/gen_versions.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,26 @@ function main () {
4040
// go through directory that contains all languages
4141
util.listdirsSync(rootDir).forEach(function (langId) {
4242
const langPath = path.join(rootDir, langId);
43-
let versionNames = util.listdirsSync(langPath);
4443

45-
// Remove dev version for semver sort. We'll add it back later.
46-
versionNames.splice(versionNames.indexOf('dev'), 1);
44+
// Get all directories, excluding dev, and make sure it is in lexicographically order.
45+
const versionNames = util.listdirsSync(langPath)
46+
.filter(dir => dir !== 'dev')
47+
.sort((a, b) => a.localeCompare(b));
4748

48-
// semver doesn't like a value of 10.x, so we'll coerce the values into proper 10.0.0,
49-
// and store a map to easily convert map our sorted away back to our desired text.
50-
const coercionMap = {};
51-
versionNames = versionNames.map((v) => {
52-
const coerced = semver.coerce(v).toString();
53-
coercionMap[coerced] = v;
54-
return coerced;
49+
// Semver cant sort invalid values. E.g. 10.x or 12.x-2025.01
50+
// Will create an array of objects containing the coerce value (10.0.0) for sorting and
51+
// the original readable name for display.
52+
const versionMap = versionNames.map((readable) => {
53+
const [versionPart] = readable.split('-');
54+
const coerced = semver.coerce(versionPart)?.toString() || versionPart;
55+
return { readable, semantic: coerced };
5556
});
5657

57-
versionNames = semver.sort(versionNames);
58+
const sortedVersions = versionMap.filter(v => semver.valid(v.semantic))
59+
.sort((a, b) => semver.compare(a.semantic, b.semantic))
60+
.map(v => v.readable);
5861

59-
// Now we can restore our desired labelling
60-
versionNames = versionNames.map((v) => coercionMap[v]);
61-
62-
// Finally, don't forget to restore our dev version
63-
versionNames.push('dev');
62+
sortedVersions.push('dev'); // add back dev
6463

6564
// get language ID
6665
const langName = LANGUAGE_MAP[langId];
@@ -72,7 +71,7 @@ function main () {
7271
// set the language name and the versions it has
7372
config[langId] = {
7473
name: langName,
75-
versions: versionNames
74+
versions: sortedVersions
7675
};
7776
});
7877

0 commit comments

Comments
 (0)