Skip to content

Commit 5721207

Browse files
lidavidmianmcook
andauthored
docs: show a warning banner when viewing old/dev docs (#2860)
This is not as visually fancy as the Arrow docs warning banner but because it is generated on the fly we can update this without having to regenerate or edit any pages. Fixes #2850. --------- Co-authored-by: Ian Cook <[email protected]>
1 parent e5e5a73 commit 5721207

File tree

1 file changed

+107
-38
lines changed

1 file changed

+107
-38
lines changed

docs/source/_static/version.js

Lines changed: 107 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,48 +30,117 @@ function adbcInjectVersionSwitcher() {
3030
// path;version\npath2;version2;\n...
3131
// Versions are sorted at generation time
3232

33-
versions
34-
.trim()
35-
.split(/\n/g)
36-
.map((version) => version.split(/;/))
37-
// Most recent on top
38-
.reverse()
39-
.forEach((version) => {
40-
const el = document.createElement("a");
41-
// Variable injected by template
42-
el.setAttribute("href", versionsRoot + "/" + version[0]);
43-
el.innerText = version[1];
44-
if (version[1] === currentVersion) {
45-
el.classList.toggle("active");
46-
}
47-
const li = document.createElement("li");
48-
li.appendChild(el);
49-
root.appendChild(li);
33+
const parsedVersions = versions
34+
.trim()
35+
.split(/\n/g)
36+
.map((version) => version.split(/;/))
37+
// Most recent on top
38+
.reverse();
39+
parsedVersions.forEach((version) => {
40+
const el = document.createElement("a");
41+
// Variable injected by template
42+
el.setAttribute("href", versionsRoot + "/" + version[0]);
43+
el.innerText = version[1];
44+
if (version[1] === currentVersion) {
45+
el.classList.toggle("active");
46+
}
47+
const li = document.createElement("li");
48+
li.appendChild(el);
49+
root.appendChild(li);
5050

51-
el.addEventListener("click", (e) => {
52-
e.preventDefault();
53-
try {
54-
let relativePart = window.location.pathname.replace(/^\//, "");
55-
// Remove the adbc/ prefix
56-
relativePart = relativePart.replace(/^adbc[^\/]+\//, "");
57-
// Remove the version number
58-
relativePart = relativePart.replace(/^[^\/]+\//, "");
59-
const newUrl = `${el.getAttribute("href")}/${relativePart}`;
60-
window.fetch(newUrl).then((resp) => {
61-
if (resp.status === 200) {
62-
window.location.href = newUrl;
63-
} else {
64-
window.location.href = el.getAttribute("href");
65-
}
66-
}, () => {
51+
el.addEventListener("click", (e) => {
52+
e.preventDefault();
53+
try {
54+
let relativePart = window.location.pathname.replace(/^\//, "");
55+
// Remove the adbc/ prefix
56+
relativePart = relativePart.replace(/^adbc[^\/]+\//, "");
57+
// Remove the version number
58+
relativePart = relativePart.replace(/^[^\/]+\//, "");
59+
const newUrl = `${el.getAttribute("href")}/${relativePart}`;
60+
window.fetch(newUrl).then((resp) => {
61+
if (resp.status === 200) {
62+
window.location.href = newUrl;
63+
} else {
6764
window.location.href = el.getAttribute("href");
68-
});
69-
} catch (e) {
65+
}
66+
}, () => {
7067
window.location.href = el.getAttribute("href");
71-
}
72-
return false;
73-
});
68+
});
69+
} catch (e) {
70+
window.location.href = el.getAttribute("href");
71+
}
72+
return false;
73+
});
74+
});
75+
76+
// Inject a banner warning if the user is looking at older/development
77+
// version documentation
78+
79+
// If the user has dismissed the popup, don't show it again
80+
const storageKey = "adbc-ignored-version-warnings";
81+
const ignoreVersionWarnings = new Set();
82+
try {
83+
const savedVersions = JSON.parse(window.localStorage[storageKey]);
84+
for (const version of savedVersions) {
85+
ignoreVersionWarnings.add(version);
86+
}
87+
} catch (e) {
88+
// ignore
89+
}
90+
91+
if (ignoreVersionWarnings.has(currentVersion)) {
92+
return;
93+
}
94+
95+
let warningBanner = null;
96+
const redirectUrl = `${versionsRoot}/current`;
97+
let redirectText = null;
98+
if (currentVersion.endsWith(" (dev)")) {
99+
warningBanner = "This is documentation for an unstable development version.";
100+
redirectText = "Switch to stable version";
101+
} else {
102+
const stableVersions = parsedVersions
103+
.filter(v => v[0] === "current");
104+
if (stableVersions.length > 0) {
105+
const stableVersion = stableVersions[0][1].match(/^(.+) \(current\)/)[1];
106+
if (currentVersion !== stableVersion) {
107+
warningBanner = `This is documentation for version ${currentVersion}.`;
108+
redirectText = `Switch to current stable version`;
109+
}
110+
}
111+
}
112+
113+
if (warningBanner !== null) {
114+
// Generate on the fly instead of depending on the template containing
115+
// the right elements/styles
116+
const container = document.createElement("div");
117+
const text = document.createElement("span");
118+
text.innerText = warningBanner + " ";
119+
const button = document.createElement("a");
120+
button.setAttribute("href", redirectUrl);
121+
button.innerText = redirectText;
122+
const hide = document.createElement("a");
123+
hide.innerText = "Hide for this version";
124+
const spacer = document.createTextNode(" ");
125+
container.appendChild(text);
126+
container.appendChild(button);
127+
container.appendChild(spacer);
128+
container.appendChild(hide);
129+
130+
hide.addEventListener("click", (e) => {
131+
container.remove();
132+
ignoreVersionWarnings.add(currentVersion);
133+
window.localStorage[storageKey] =
134+
JSON.stringify(Array.from(ignoreVersionWarnings));
74135
});
136+
137+
container.style.background = "#f8d7da";
138+
container.style.color = "#000";
139+
container.style.padding = "1em";
140+
container.style.textAlign = "center";
141+
142+
document.body.prepend(container);
143+
}
75144
};
76145

77146
if (document.readyState !== "loading") {

0 commit comments

Comments
 (0)