|
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; |
31 | 8 | 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; |
36 | 10 | } 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; |
106 | 12 | } |
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 | +})(); |
0 commit comments