Skip to content

Commit 381a9b9

Browse files
committed
add warning banner for unreleased branch or version in the documentation
Signed-off-by: olalekan odukoya <[email protected]>
1 parent a6829d3 commit 381a9b9

File tree

3 files changed

+174
-7
lines changed

3 files changed

+174
-7
lines changed

docs/overrides/main.html

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
{% extends "base.html" %} {% block extrahead %} {{ super() }}
2+
<style>
3+
#version-banner {
4+
display: none;
5+
position: sticky;
6+
top: 0;
7+
left: 0;
8+
right: 0;
9+
width: 100%;
10+
background-color: #00bcd4;
11+
color: white;
12+
padding: 8px 16px;
13+
text-align: center;
14+
font-size: 14px;
15+
line-height: 1.5;
16+
z-index: 10000;
17+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
18+
}
19+
20+
#version-banner a {
21+
color: white;
22+
text-decoration: underline;
23+
margin-left: 8px;
24+
}
25+
26+
#version-banner a:hover {
27+
text-decoration: none;
28+
}
29+
30+
.md-header {
31+
transition: top 0.2s ease;
32+
}
33+
</style>
34+
35+
<script>
36+
(function () {
37+
"use strict";
38+
39+
const CONFIG = {
40+
UNRELEASED_VERSION: "main",
41+
RETRY_DELAY: 500,
42+
HEADER_ADJUST_DELAY: 100,
43+
};
44+
45+
function getCurrentVersion() {
46+
const path = window.location.pathname;
47+
const versionMatch = path.match(/\/(main|v\d+\.\d+[^\/]*)/);
48+
return versionMatch ? versionMatch[1] : null;
49+
}
50+
51+
function getAvailableVersions() {
52+
const versionList = document.querySelector("ul.md-version__list");
53+
if (!versionList) return [];
54+
55+
const links = Array.from(
56+
versionList.querySelectorAll("a.md-version__link")
57+
);
58+
59+
return links
60+
.map((link) => {
61+
const href = link.href || link.getAttribute("href");
62+
const match = href.match(/\/(v\d+\.\d+[^\/]*)\//);
63+
return match ? match[1] : null;
64+
})
65+
.filter((v) => v && /^v\d+\.\d+/.test(v))
66+
.filter((v, i, arr) => arr.indexOf(v) === i);
67+
}
68+
69+
function getLatestVersionPath() {
70+
const versions = getAvailableVersions();
71+
72+
console.log("all versions", versions)
73+
74+
const latestVersion = versions[0];
75+
const currentPath = window.location.pathname;
76+
77+
return (
78+
currentPath.replace(/\/main(\/|$)/, `/${latestVersion}$1`) ||
79+
`/${latestVersion}/`
80+
);
81+
}
82+
83+
function createBanner() {
84+
const banner = document.createElement("div");
85+
banner.id = "version-banner";
86+
banner.innerHTML = `
87+
<strong>You are viewing the docs for an unreleased version.</strong>
88+
<a href="#" id="latest-version-link">Click here to go to the latest stable version.</a>
89+
`;
90+
91+
document.body.insertBefore(banner, document.body.firstChild);
92+
banner.style.display = "block";
93+
94+
return banner;
95+
}
96+
97+
function updateBannerLink(banner) {
98+
const link = banner.querySelector("#latest-version-link");
99+
if (!link) return;
100+
101+
const latestPath = getLatestVersionPath();
102+
console.log("latestPathlatestPath", latestPath)
103+
link.href = latestPath;
104+
}
105+
106+
function adjustHeader(banner) {
107+
setTimeout(() => {
108+
const header = document.querySelector(".md-header");
109+
if (!header) return;
110+
111+
const bannerHeight = banner.offsetHeight;
112+
const headerHeight = header.offsetHeight;
113+
114+
header.style.top = `${bannerHeight}px`;
115+
document.documentElement.style.setProperty(
116+
"--md-header-height",
117+
`${bannerHeight + headerHeight}px`
118+
);
119+
document.documentElement.style.setProperty(
120+
"--md-scroll-margin",
121+
`${bannerHeight + headerHeight}px`
122+
);
123+
}, CONFIG.HEADER_ADJUST_DELAY);
124+
}
125+
126+
function init() {
127+
const currentVersion = getCurrentVersion();
128+
129+
if (currentVersion !== CONFIG.UNRELEASED_VERSION) {
130+
return;
131+
}
132+
133+
const banner = createBanner();
134+
updateBannerLink(banner);
135+
136+
setTimeout(() => updateBannerLink(banner), CONFIG.RETRY_DELAY);
137+
138+
adjustHeader(banner);
139+
}
140+
141+
function waitForDOM() {
142+
if (document.readyState === "complete" || document.readyState === "interactive") {
143+
console.log("DOM is ready, initializing...");
144+
init();
145+
return
146+
}
147+
148+
console.log("DOM not ready yet, checking again...");
149+
setTimeout(waitForDOM, 1000);
150+
}
151+
152+
waitForDOM();
153+
154+
})();
155+
</script>
156+
{% endblock %}

docs/overrides/stylesheets/crd.css

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@ ul.crd-index {
3535
color: #fff;
3636
}
3737

38-
3938
dl.crd-meta {
4039
display: flex;
4140
flex-flow: row wrap;
4241
border-bottom: 1px solid #00000012;
4342
}
4443

45-
[data-md-color-primary=black] dl.crd-meta {
44+
[data-md-color-primary="black"] dl.crd-meta {
4645
border-bottom: 1px solid #e6e6e612;
4746
}
4847

@@ -62,13 +61,14 @@ dl.crd-meta {
6261
border-top: 1px solid #00000012;
6362
}
6463

65-
[data-md-color-primary=black] .crd-meta dt, [data-md-color-primary=black] .crd-meta dd {
64+
[data-md-color-primary="black"] .crd-meta dt,
65+
[data-md-color-primary="black"] .crd-meta dd {
6666
border-top: 1px solid #e6e6e612;
6767
}
6868

6969
.crd-meta a.version {
7070
font-family: var(--md-code-font-family);
71-
font-size:
71+
font-size: var(--md-code-font-size);
7272
margin-right: 10px;
7373
}
7474

@@ -83,8 +83,9 @@ dl.crd-meta {
8383
margin-left: -0.1em;
8484
}
8585

86-
.property-description, .property-meta {
87-
margin: 5px 0
86+
.property-description,
87+
.property-meta {
88+
margin: 5px 0;
8889
}
8990

9091
.property-title {

docs/scripts/serve-docs.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ fi
3434
# for local docs testing, we don't care what the remote branch looks like.
3535
MIKE_OPTIONS+=(--ignore-remote-status)
3636

37-
mike set-default "${MIKE_OPTIONS[@]}" --allow-undefined main
37+
MIKE_OUTPUT=$(mike list "${MIKE_OPTIONS[@]}" 2>&1)
38+
39+
VERSIONS=$(echo "$MIKE_OUTPUT" | grep -oE '^v[0-9]+\.[0-9]+' 2>/dev/null || true)
40+
41+
if [[ -n "$VERSIONS" ]]; then
42+
DEFAULT_VERSION=$(echo "$VERSIONS" | sort -V | tail -1 2>/dev/null || echo "main")
43+
else
44+
DEFAULT_VERSION="main"
45+
fi
46+
47+
mike set-default "${MIKE_OPTIONS[@]}" --allow-undefined "${DEFAULT_VERSION}"
3848
if [[ -n "${DEV_MODE:-}" ]]; then
3949
mkdocs serve --dev-addr=127.0.0.1:8000 --livereload
4050
else

0 commit comments

Comments
 (0)