@@ -63,7 +63,7 @@ function compareVersions(a, b) {
63
63
/**
64
64
* Get the list of released versions for the project from PyPI. Prerelease and
65
65
* development versions are discarded. The list is sorted in descending order,
66
- * latest version first.
66
+ * highest version first.
67
67
*
68
68
* This will be called on every page load. To avoid making excessive requests to
69
69
* PyPI, the result is cached for 1 day. PyPI also sends cache headers, so a
@@ -103,8 +103,9 @@ async function getReleasedVersions(name) {
103
103
}
104
104
105
105
/**
106
- * Get the latest released version of the project from PyPI, and compare the
107
- * version being documented. Return the
106
+ * Get the highest released version of the project from PyPI, and compare the
107
+ * version being documented. Returns a list of two values, the comparison
108
+ * result and the highest version.
108
109
*
109
110
* @param name The normalized PyPI project name.
110
111
* @param value The version being documented.
@@ -122,55 +123,63 @@ async function describeVersion(name, value) {
122
123
return [ 1 , null ]
123
124
}
124
125
125
- let latestVersion = releasedVersions [ 0 ]
126
- let compared = compareVersions ( currentVersion , latestVersion )
126
+ let highestVersion = releasedVersions [ 0 ]
127
+ let compared = compareVersions ( currentVersion , highestVersion )
127
128
128
129
if ( compared === 1 ) {
129
- return [ 1 , latestVersion ]
130
+ return [ 1 , highestVersion ]
130
131
}
131
132
132
- // If the current version including trailing zeros is a prefix of the latest
133
- // version, then these are the latest docs. For example, 2.0.x becomes 2.0,
133
+ // If the current version including trailing zeros is a prefix of the highest
134
+ // version, then these are the stable docs. For example, 2.0.x becomes 2.0,
134
135
// which is a prefix of 2.0.3. If we were just looking at the compare result,
135
136
// it would incorrectly be marked as an old version.
136
- if ( currentVersion . parts . every ( ( n , i ) => n === latestVersion . parts [ i ] ) ) {
137
- return [ 0 , latestVersion ]
137
+ if ( currentVersion . parts . every ( ( n , i ) => n === highestVersion . parts [ i ] ) ) {
138
+ return [ 0 , highestVersion ]
138
139
}
139
140
140
- return [ - 1 , latestVersion ]
141
+ return [ - 1 , highestVersion ]
141
142
}
142
143
143
144
/**
144
- * Compare the version being documented to the latest version, and display a
145
- * warning banner if it is not the latest version.
145
+ * Compare the version being documented to the highest released version, and
146
+ * display a warning banner if it is not the highest version.
146
147
*
147
148
* @param project The normalized PyPI project name.
148
149
* @param version The version being documented.
149
150
* @returns {Promise<void> }
150
151
*/
151
152
async function createBanner ( project , version ) {
152
- let [ desc , latest ] = await describeVersion ( project , version )
153
+ let [ compared , stable ] = await describeVersion ( project , version )
153
154
154
- // No banner if this is the latest version or there are no other versions.
155
- if ( desc === 0 || latest === null ) {
155
+ // No banner if this is the highest version or there are no other versions.
156
+ if ( compared === 0 || stable === null ) {
156
157
return
157
158
}
158
159
159
160
let banner = document . createElement ( "p" )
160
161
banner . className = "version-warning"
161
162
162
- if ( desc === 1 ) {
163
+ if ( compared === 1 ) {
163
164
banner . textContent = "This is the development version. The stable version is "
164
- } else if ( desc === - 1 ) {
165
+ } else if ( compared === - 1 ) {
165
166
banner . textContent = "This is an old version. The current version is "
166
167
}
167
168
168
- let link = document . createElement ( "a" )
169
- link . href = document . querySelector ( 'link[rel="canonical"]' ) . href
170
- link . textContent = latest . value
171
- banner . append ( link , "." )
172
- document . getElementsByClassName ( "document" ) [ 0 ] . prepend ( banner )
169
+ let canonical = document . querySelector ( 'link[rel="canonical"]' )
170
+
171
+ if ( canonical !== null ) {
172
+ // If a canonical URL is available, the version is a link to it.
173
+ let link = document . createElement ( "a" )
174
+ link . href = canonical . href
175
+ link . textContent = stable . value
176
+ banner . append ( link , "." )
177
+ } else {
178
+ // Otherwise, the version is text only.
179
+ banner . append ( stable . value , "." )
180
+ }
173
181
182
+ document . getElementsByClassName ( "document" ) [ 0 ] . prepend ( banner )
174
183
// Set scroll-padding-top to prevent the banner from overlapping anchors.
175
184
// It's also set in CSS assuming the banner text is only 1 line.
176
185
let bannerStyle = window . getComputedStyle ( banner )
0 commit comments