Skip to content

Commit c4531cc

Browse files
committed
release documentation ordered by asset dates
#docs fix #802
1 parent 6c70e4d commit c4531cc

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

docs/extensions/mrdocs-releases.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ function humanizeBytes(bytes) {
1818
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
1919
}
2020

21+
/**
22+
* Converts a date to a human-readable string format.
23+
*
24+
* This function takes a date object or a date string and returns a formatted string
25+
* representing the date in the 'en-US' locale with the format 'Month Day, Year'.
26+
*
27+
* @param {Date|string} date - The date to be formatted.
28+
* @returns {string} - A human-readable string representing the date.
29+
*/
2130
function humanizeDate(date) {
2231
const options = {year: 'numeric', month: 'long', day: 'numeric'};
2332
return new Date(date).toLocaleDateString('en-US', options);
@@ -89,6 +98,44 @@ function fetchWithRetry(url, headers) {
8998
}
9099
}
91100

101+
/**
102+
* Gets the latest date among the various date fields in the release object.
103+
*
104+
* This function checks the `published_at` and `created_at` fields of the release,
105+
* as well as the `created_at` and `updated_at` fields of each asset in the release.
106+
* It returns the most recent date found among these fields.
107+
*
108+
* @param {Object} release - The release object containing date fields and assets.
109+
* @param {string} [release.published_at] - The published date of the release.
110+
* @param {string} [release.created_at] - The creation date of the release.
111+
* @param {Array} [release.assets] - An array of asset objects.
112+
* @param {string} [release.assets[].created_at] - The creation date of an asset.
113+
* @param {string} [release.assets[].updated_at] - The last updated date of an asset.
114+
* @returns {Date|null} - The latest date found among the release and asset dates, or null if no dates are found.
115+
*/
116+
function getReleaseDate(release) {
117+
const dates = [];
118+
119+
if (release.published_at) {
120+
dates.push(new Date(release.published_at));
121+
}
122+
if (release.created_at) {
123+
dates.push(new Date(release.created_at));
124+
}
125+
if (release.assets && Array.isArray(release.assets)) {
126+
release.assets.forEach(asset => {
127+
if (asset.created_at) {
128+
dates.push(new Date(asset.created_at));
129+
}
130+
if (asset.updated_at) {
131+
dates.push(new Date(asset.updated_at));
132+
}
133+
});
134+
}
135+
136+
return dates.length > 0 ? new Date(Math.max(...dates)) : null;
137+
}
138+
92139
module.exports = function (registry) {
93140
registry.blockMacro('mrdocs-releases', function () {
94141
const self = this
@@ -118,9 +165,11 @@ module.exports = function (registry) {
118165
let text = '|===\n'
119166
text += '| 3+| 🪟 Windows 2+| 🐧 Linux \n'
120167
text += '| 📃 Release | 📦 7z | 📦 msi | 📦 zip | 📦 tar.xz | 📦 tar.gz \n'
168+
releases.sort((a, b) => getReleaseDate(b) - getReleaseDate(a));
121169
for (const release of releases) {
122170
if (release.name === 'llvm-package') continue
123-
text += `| ${release.html_url}[${release.name},window=_blank]\n\n${humanizeDate(release.published_at)} `
171+
const date = getReleaseDate(release)
172+
text += `| ${release.html_url}[${release.name},window=_blank]\n\n${humanizeDate(date)} `
124173
const assetSuffixes = ['win64.7z', 'win64.msi', 'win64.zip', 'Linux.tar.xz', 'Linux.tar.gz']
125174
for (const suffix of assetSuffixes) {
126175
const asset = release.assets.find(asset => asset.name.endsWith(suffix))

0 commit comments

Comments
 (0)