@@ -18,6 +18,15 @@ function humanizeBytes(bytes) {
18
18
return Math . round ( bytes / Math . pow ( 1024 , i ) , 2 ) + ' ' + sizes [ i ] ;
19
19
}
20
20
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
+ */
21
30
function humanizeDate ( date ) {
22
31
const options = { year : 'numeric' , month : 'long' , day : 'numeric' } ;
23
32
return new Date ( date ) . toLocaleDateString ( 'en-US' , options ) ;
@@ -89,6 +98,44 @@ function fetchWithRetry(url, headers) {
89
98
}
90
99
}
91
100
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
+
92
139
module . exports = function ( registry ) {
93
140
registry . blockMacro ( 'mrdocs-releases' , function ( ) {
94
141
const self = this
@@ -118,9 +165,11 @@ module.exports = function (registry) {
118
165
let text = '|===\n'
119
166
text += '| 3+| 🪟 Windows 2+| 🐧 Linux \n'
120
167
text += '| 📃 Release | 📦 7z | 📦 msi | 📦 zip | 📦 tar.xz | 📦 tar.gz \n'
168
+ releases . sort ( ( a , b ) => getReleaseDate ( b ) - getReleaseDate ( a ) ) ;
121
169
for ( const release of releases ) {
122
170
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 ) } `
124
173
const assetSuffixes = [ 'win64.7z' , 'win64.msi' , 'win64.zip' , 'Linux.tar.xz' , 'Linux.tar.gz' ]
125
174
for ( const suffix of assetSuffixes ) {
126
175
const asset = release . assets . find ( asset => asset . name . endsWith ( suffix ) )
0 commit comments