|
| 1 | +/* Material's header source widget shows the monorepo's latest release |
| 2 | + (e.g. v3.3.0). Replace it with the latest tag for this package, and |
| 3 | + patch the theme's sessionStorage cache so instant navigation keeps it. |
| 4 | +
|
| 5 | + The GitHub repo and the package's tag prefix (e.g. zarr_metadata-) are |
| 6 | + derived from repo_url, whose final path segment is the package directory, |
| 7 | + so this file is identical across all sub-package docs sites. */ |
| 8 | +(() => { |
| 9 | + const source = document.querySelector('[data-md-component="source"]') |
| 10 | + if (!source || !source.href) |
| 11 | + return |
| 12 | + const segments = new URL(source.href).pathname.split("/").filter(Boolean) |
| 13 | + const REPO = segments.slice(0, 2).join("/") |
| 14 | + const PREFIX = segments[segments.length - 1].replace(/-/g, "_") + "-" |
| 15 | + |
| 16 | + /* Final releases only (v1.2.3), matching the theme's own releases/latest |
| 17 | + lookup; pre-release parts like 0rc1 would otherwise parse as 0 and tie |
| 18 | + with (or outrank) the final release. */ |
| 19 | + const RELEASE = /^v?\d+(\.\d+)*$/ |
| 20 | + |
| 21 | + const parse = version => version |
| 22 | + .replace(/^v/, "") |
| 23 | + .split(".") |
| 24 | + .map(part => parseInt(part, 10)) |
| 25 | + |
| 26 | + const newestFirst = (a, b) => { |
| 27 | + const va = parse(a), vb = parse(b) |
| 28 | + for (let i = 0; i < Math.max(va.length, vb.length); i++) |
| 29 | + if ((vb[i] || 0) !== (va[i] || 0)) |
| 30 | + return (vb[i] || 0) - (va[i] || 0) |
| 31 | + return 0 |
| 32 | + } |
| 33 | + |
| 34 | + /* Cache the tag for the tab's lifetime (like the theme's own __source |
| 35 | + cache) so navigation doesn't burn GitHub's unauthenticated API rate |
| 36 | + limit. Keyed by prefix in case sub-package sites share an origin. */ |
| 37 | + const CACHE_KEY = `__package_tag/${PREFIX}` |
| 38 | + |
| 39 | + async function latestPackageTag() { |
| 40 | + const cached = sessionStorage.getItem(CACHE_KEY) |
| 41 | + if (cached) |
| 42 | + return cached |
| 43 | + const response = await fetch( |
| 44 | + `https://api.github.com/repos/${REPO}/tags?per_page=100` |
| 45 | + ) |
| 46 | + if (!response.ok) |
| 47 | + return undefined |
| 48 | + const tags = await response.json() |
| 49 | + const versions = tags |
| 50 | + .map(tag => tag.name) |
| 51 | + .filter(name => name.startsWith(PREFIX)) |
| 52 | + .map(name => name.slice(PREFIX.length)) |
| 53 | + .filter(version => RELEASE.test(version)) |
| 54 | + .sort(newestFirst) |
| 55 | + if (versions[0]) |
| 56 | + sessionStorage.setItem(CACHE_KEY, versions[0]) |
| 57 | + return versions[0] |
| 58 | + } |
| 59 | + |
| 60 | + function patchDom(version) { |
| 61 | + for (const el of document.querySelectorAll(".md-source__fact--version")) |
| 62 | + el.textContent = version |
| 63 | + } |
| 64 | + |
| 65 | + function patchCache(version) { |
| 66 | + const facts = __md_get("__source", sessionStorage) |
| 67 | + if (!facts) |
| 68 | + return false |
| 69 | + facts.version = version |
| 70 | + __md_set("__source", facts, sessionStorage) |
| 71 | + return true |
| 72 | + } |
| 73 | + |
| 74 | + latestPackageTag().then(version => { |
| 75 | + if (!version) |
| 76 | + return |
| 77 | + patchDom(version) |
| 78 | + if (patchCache(version)) |
| 79 | + return |
| 80 | + /* The theme's own API request hasn't resolved yet — patch both the |
| 81 | + cache and the DOM once it renders the facts list. */ |
| 82 | + new MutationObserver((_, observer) => { |
| 83 | + if (patchCache(version)) { |
| 84 | + patchDom(version) |
| 85 | + observer.disconnect() |
| 86 | + } |
| 87 | + }).observe(source, { childList: true, subtree: true }) |
| 88 | + }) |
| 89 | +})() |
0 commit comments