Skip to content

Commit f5bb6a1

Browse files
pfeerickclaude
andcommitted
feat: load the version banner from /latest/, reword legacy notice
Syncs the banner loader and version-banner.js from edgetx_2.12 (ff82cff), so this version's banner is loaded from /latest/ and future wording changes only need the latest release branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 954bb59 commit f5bb6a1

2 files changed

Lines changed: 153 additions & 114 deletions

File tree

Lines changed: 31 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,34 @@
1-
// Shows a notice above the page content when the version being viewed is
2-
// the development build ("dev") or anything other than the one mike aliases
3-
// as "latest". The version is worked out at runtime from the URL and mike's
4-
// versions.json rather than from per-version config, so every version can
5-
// ship the same mkdocs.yml (main is a straight copy of the release branch).
6-
// Material doesn't expose `extra` in #__config, so config can't drive this.
7-
8-
const DEV_VERSION = "dev";
9-
const LATEST_ALIAS = "latest";
10-
11-
function getSiteConfig() {
12-
const configNode = document.querySelector("#__config");
13-
if (!configNode) return null;
14-
15-
try {
16-
return JSON.parse(configNode.textContent || "{}");
17-
} catch {
18-
return null;
19-
}
20-
}
21-
22-
function getVersionRoot(config) {
23-
// config.base is the relative path from this page to the version root,
24-
// e.g. "../.." -- resolving it against the page URL gives /<version>/.
25-
const base = config?.base;
26-
if (typeof base !== "string") return null;
27-
return new URL(base.endsWith("/") ? base : `${base}/`, window.location.href);
28-
}
29-
30-
async function loadVersions(siteRoot) {
1+
// Loader for the version banner. The banner itself (logic and wording) lives
2+
// in version-banner.js, and every version loads the copy published under
3+
// /latest/, so it can be changed in one place, on the latest release branch,
4+
// instead of on every docs branch. If that copy can't be loaded (e.g. a site
5+
// not served through mike), this version's own copy is used instead.
6+
(() => {
7+
let base;
318
try {
32-
const response = await fetch(new URL("versions.json", siteRoot));
33-
if (!response.ok) return null;
34-
const versions = await response.json();
35-
return Array.isArray(versions) ? versions : null;
9+
base = JSON.parse(document.querySelector("#__config")?.textContent || "{}").base;
3610
} catch {
37-
return null;
38-
}
39-
}
40-
41-
function buildBanner({ label, message, linkText, linkHref }) {
42-
const banner = document.createElement("aside");
43-
banner.className = "legacy-version-banner";
44-
banner.setAttribute("role", "note");
45-
banner.setAttribute("aria-label", label);
46-
47-
const labelNode = document.createElement("p");
48-
labelNode.className = "legacy-version-banner__label";
49-
labelNode.textContent = label;
50-
51-
const messageNode = document.createElement("p");
52-
messageNode.className = "legacy-version-banner__message";
53-
messageNode.textContent = `${message} `;
54-
55-
if (linkHref) {
56-
const link = document.createElement("a");
57-
link.href = linkHref;
58-
link.textContent = linkText;
59-
messageNode.appendChild(link);
60-
}
61-
62-
banner.appendChild(labelNode);
63-
banner.appendChild(messageNode);
64-
return banner;
65-
}
66-
67-
async function mountVersionBanner() {
68-
const container = document.querySelector(".md-content__inner");
69-
if (!container || container.querySelector(".legacy-version-banner")) return;
70-
71-
const versionRoot = getVersionRoot(getSiteConfig());
72-
if (!versionRoot) return;
73-
74-
// Not served through mike (e.g. plain `mkdocs serve`): no versions.json
75-
// one level up, so no banner.
76-
const siteRoot = new URL("../", versionRoot);
77-
const versions = await loadVersions(siteRoot);
78-
if (!versions) return;
79-
80-
const segment = decodeURIComponent(versionRoot.pathname.split("/").filter(Boolean).pop() || "");
81-
const current = versions.find(
82-
(entry) => entry.version === segment || (entry.aliases || []).includes(segment),
83-
);
84-
if (!current) return;
85-
86-
const latest = versions.find((entry) => (entry.aliases || []).includes(LATEST_ALIAS));
87-
const latestHref = latest ? new URL(`${LATEST_ALIAS}/`, siteRoot).href : null;
88-
const latestTitle = latest?.title || latest?.version;
89-
90-
let banner = null;
91-
if (current.version === DEV_VERSION) {
92-
banner = buildBanner({
93-
label: "Development version",
94-
message:
95-
"These docs track EdgeTX development and may describe features or changes that are not in a released version yet.",
96-
linkText: latest ? `View the ${latestTitle} release docs.` : "",
97-
linkHref: latestHref,
98-
});
99-
} else if (latest && current.version !== latest.version) {
100-
banner = buildBanner({
101-
label: "Legacy version",
102-
message: `You are viewing the documentation for EdgeTX ${current.title || current.version}. It is kept for reference and may not include later corrections.`,
103-
linkText: `Go to the latest version (${latestTitle}).`,
104-
linkHref: latestHref,
105-
});
11+
return;
10612
}
107-
108-
if (banner && !container.querySelector(".legacy-version-banner")) {
109-
container.insertBefore(banner, container.firstChild);
110-
}
111-
}
112-
113-
if (document.readyState === "loading") {
114-
document.addEventListener("DOMContentLoaded", mountVersionBanner, { once: true });
115-
} else {
116-
mountVersionBanner();
117-
}
13+
if (typeof base !== "string") return;
14+
15+
// config.base is the relative path from this page to the version root.
16+
const versionRoot = new URL(base.endsWith("/") ? base : `${base}/`, window.location.href);
17+
const sources = [
18+
new URL("../latest/javascripts/version-banner.js", versionRoot),
19+
new URL("javascripts/version-banner.js", versionRoot),
20+
];
21+
22+
const load = (index) => {
23+
if (index >= sources.length) return;
24+
const script = document.createElement("script");
25+
script.src = sources[index].href;
26+
script.onerror = () => {
27+
script.remove();
28+
load(index + 1);
29+
};
30+
document.head.appendChild(script);
31+
};
32+
33+
load(0);
34+
})();
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Shows a notice above the page content when the version being viewed is
2+
// the development build ("dev") or anything other than the one mike aliases
3+
// as "latest". The version is worked out at runtime from the URL and mike's
4+
// versions.json rather than from per-version config, so every version can
5+
// ship the same mkdocs.yml (main is a straight copy of the release branch).
6+
// Material doesn't expose `extra` in #__config, so config can't drive this.
7+
//
8+
// Every version's pages load this file from /latest/ (see
9+
// legacy-version-banner.js), so the copy on the latest release branch is the
10+
// one that runs everywhere: change the wording or logic there.
11+
(() => {
12+
const DEV_VERSION = "dev";
13+
const LATEST_ALIAS = "latest";
14+
15+
function getSiteConfig() {
16+
const configNode = document.querySelector("#__config");
17+
if (!configNode) return null;
18+
19+
try {
20+
return JSON.parse(configNode.textContent || "{}");
21+
} catch {
22+
return null;
23+
}
24+
}
25+
26+
function getVersionRoot(config) {
27+
// config.base is the relative path from this page to the version root,
28+
// e.g. "../.." -- resolving it against the page URL gives /<version>/.
29+
const base = config?.base;
30+
if (typeof base !== "string") return null;
31+
return new URL(base.endsWith("/") ? base : `${base}/`, window.location.href);
32+
}
33+
34+
async function loadVersions(siteRoot) {
35+
try {
36+
const response = await fetch(new URL("versions.json", siteRoot));
37+
if (!response.ok) return null;
38+
const versions = await response.json();
39+
return Array.isArray(versions) ? versions : null;
40+
} catch {
41+
return null;
42+
}
43+
}
44+
45+
function buildBanner({ label, message, linkText, linkHref }) {
46+
const banner = document.createElement("aside");
47+
banner.className = "legacy-version-banner";
48+
banner.setAttribute("role", "note");
49+
banner.setAttribute("aria-label", label);
50+
51+
const labelNode = document.createElement("p");
52+
labelNode.className = "legacy-version-banner__label";
53+
labelNode.textContent = label;
54+
55+
const messageNode = document.createElement("p");
56+
messageNode.className = "legacy-version-banner__message";
57+
messageNode.textContent = `${message} `;
58+
59+
if (linkHref) {
60+
const link = document.createElement("a");
61+
link.href = linkHref;
62+
link.textContent = linkText;
63+
messageNode.appendChild(link);
64+
}
65+
66+
banner.appendChild(labelNode);
67+
banner.appendChild(messageNode);
68+
return banner;
69+
}
70+
71+
async function mountVersionBanner() {
72+
const container = document.querySelector(".md-content__inner");
73+
if (!container || container.querySelector(".legacy-version-banner")) return;
74+
75+
const versionRoot = getVersionRoot(getSiteConfig());
76+
if (!versionRoot) return;
77+
78+
// Not served through mike (e.g. plain `mkdocs serve`): no versions.json
79+
// one level up, so no banner.
80+
const siteRoot = new URL("../", versionRoot);
81+
const versions = await loadVersions(siteRoot);
82+
if (!versions) return;
83+
84+
const segment = decodeURIComponent(versionRoot.pathname.split("/").filter(Boolean).pop() || "");
85+
const current = versions.find(
86+
(entry) => entry.version === segment || (entry.aliases || []).includes(segment),
87+
);
88+
if (!current) return;
89+
90+
const latest = versions.find((entry) => (entry.aliases || []).includes(LATEST_ALIAS));
91+
const latestHref = latest ? new URL(`${LATEST_ALIAS}/`, siteRoot).href : null;
92+
const latestTitle = latest?.title || latest?.version;
93+
94+
let banner = null;
95+
if (current.version === DEV_VERSION) {
96+
banner = buildBanner({
97+
label: "Development version",
98+
message:
99+
"These docs track EdgeTX development and may describe features or changes that are not in a released version yet.",
100+
linkText: latest ? `View the ${latestTitle} release docs.` : "",
101+
linkHref: latestHref,
102+
});
103+
} else if (latest && current.version !== latest.version) {
104+
banner = buildBanner({
105+
label: "Legacy version",
106+
message: `You are viewing the documentation for EdgeTX ${current.title || current.version}. It is kept for reference and may not reflect API changes, new features or corrections in later versions.`,
107+
linkText: `Go to the latest version (${latestTitle}).`,
108+
linkHref: latestHref,
109+
});
110+
}
111+
112+
if (banner && !container.querySelector(".legacy-version-banner")) {
113+
container.insertBefore(banner, container.firstChild);
114+
}
115+
}
116+
117+
if (document.readyState === "loading") {
118+
document.addEventListener("DOMContentLoaded", mountVersionBanner, { once: true });
119+
} else {
120+
mountVersionBanner();
121+
}
122+
})();

0 commit comments

Comments
 (0)