Skip to content

Commit ebf9ad2

Browse files
committed
banner works if no canonical link is set
1 parent 96537eb commit ebf9ad2

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

src/pallets_sphinx_themes/themes/pocoo/static/describe_version.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function compareVersions(a, b) {
6363
/**
6464
* Get the list of released versions for the project from PyPI. Prerelease and
6565
* development versions are discarded. The list is sorted in descending order,
66-
* latest version first.
66+
* highest version first.
6767
*
6868
* This will be called on every page load. To avoid making excessive requests to
6969
* PyPI, the result is cached for 1 day. PyPI also sends cache headers, so a
@@ -103,8 +103,9 @@ async function getReleasedVersions(name) {
103103
}
104104

105105
/**
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.
108109
*
109110
* @param name The normalized PyPI project name.
110111
* @param value The version being documented.
@@ -122,55 +123,63 @@ async function describeVersion(name, value) {
122123
return [1, null]
123124
}
124125

125-
let latestVersion = releasedVersions[0]
126-
let compared = compareVersions(currentVersion, latestVersion)
126+
let highestVersion = releasedVersions[0]
127+
let compared = compareVersions(currentVersion, highestVersion)
127128

128129
if (compared === 1) {
129-
return [1, latestVersion]
130+
return [1, highestVersion]
130131
}
131132

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,
134135
// which is a prefix of 2.0.3. If we were just looking at the compare result,
135136
// 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]
138139
}
139140

140-
return [-1, latestVersion]
141+
return [-1, highestVersion]
141142
}
142143

143144
/**
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.
146147
*
147148
* @param project The normalized PyPI project name.
148149
* @param version The version being documented.
149150
* @returns {Promise<void>}
150151
*/
151152
async function createBanner(project, version) {
152-
let [desc, latest] = await describeVersion(project, version)
153+
let [compared, stable] = await describeVersion(project, version)
153154

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) {
156157
return
157158
}
158159

159160
let banner = document.createElement("p")
160161
banner.className = "version-warning"
161162

162-
if (desc === 1) {
163+
if (compared === 1) {
163164
banner.textContent = "This is the development version. The stable version is "
164-
} else if (desc === -1) {
165+
} else if (compared === -1) {
165166
banner.textContent = "This is an old version. The current version is "
166167
}
167168

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+
}
173181

182+
document.getElementsByClassName("document")[0].prepend(banner)
174183
// Set scroll-padding-top to prevent the banner from overlapping anchors.
175184
// It's also set in CSS assuming the banner text is only 1 line.
176185
let bannerStyle = window.getComputedStyle(banner)

0 commit comments

Comments
 (0)