Skip to content

Commit 58c9b0d

Browse files
committed
optimize version banner warning in our docs
On-behalf-of: @SAP christoph.mewes@sap.com
1 parent c15679d commit 58c9b0d

File tree

4 files changed

+93
-155
lines changed

4 files changed

+93
-155
lines changed

docs/mkdocs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ theme:
5252
name: Switch to system preference
5353

5454
extra_css:
55+
- stylesheets/version_banner.css
5556
- stylesheets/crd.css
5657

58+
extra_javascript:
59+
- scripts/version_banner.js
60+
5761
extra:
5862
version:
5963
# Enable mike for multi-version selection

docs/overrides/main.html

Lines changed: 0 additions & 155 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
(function () {
2+
"use strict";
3+
4+
const UNRELEASED_VERSION = "main";
5+
6+
function getCurrentVersion() {
7+
const path = window.location.pathname;
8+
const versionMatch = path.match(/\/(main|v\d+\.\d+[^\/]*)/);
9+
return versionMatch ? versionMatch[1] : null;
10+
}
11+
12+
function getAvailableVersions() {
13+
const versionList = document.querySelector("ul.md-version__list");
14+
if (!versionList) return [];
15+
16+
const links = Array.from(
17+
versionList.querySelectorAll("a.md-version__link")
18+
);
19+
20+
return links
21+
.map((link) => {
22+
const href = link.href || link.getAttribute("href");
23+
const match = href.match(/\/(v\d+\.\d+[^\/]*)\//);
24+
return match ? match[1] : null;
25+
})
26+
.filter((v) => v && /^v\d+\.\d+/.test(v))
27+
.filter((v, i, arr) => arr.indexOf(v) === i);
28+
}
29+
30+
function getLatestVersionPath() {
31+
const versions = getAvailableVersions();
32+
const latestVersion = versions[0];
33+
const currentPath = window.location.pathname;
34+
35+
return (
36+
currentPath.replace(/\/main(\/|$)/, `/${latestVersion}$1`) ||
37+
`/${latestVersion}/`
38+
);
39+
}
40+
41+
function createBanner() {
42+
const banner = document.createElement("div");
43+
banner.id = "version-banner";
44+
banner.innerHTML = `
45+
<strong>You are viewing the docs for an unreleased version.</strong>
46+
<a href="#" id="latest-version-link">Click here to go to the latest stable version.</a>
47+
`;
48+
49+
banner.querySelector("#latest-version-link").href = getLatestVersionPath();
50+
51+
document.body.insertBefore(banner, document.body.firstChild);
52+
53+
return banner;
54+
}
55+
56+
if (getCurrentVersion() !== UNRELEASED_VERSION) {
57+
return;
58+
}
59+
60+
createBanner();
61+
})();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#version-banner {
2+
display: block;
3+
position: sticky;
4+
top: 0;
5+
left: 0;
6+
right: 0;
7+
width: 100%;
8+
background-color: #448aff;
9+
color: white;
10+
padding: 8px 16px;
11+
text-align: center;
12+
font-size: 0.7rem;
13+
z-index: 10000;
14+
}
15+
16+
#version-banner a {
17+
color: white;
18+
text-decoration: underline;
19+
margin-left: 8px;
20+
}
21+
22+
#version-banner a:hover {
23+
text-decoration: none;
24+
}
25+
26+
.md-header {
27+
transition: top 0.2s ease;
28+
}

0 commit comments

Comments
 (0)