|
| 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 | + const REPO = "zarr-developers/zarr-python" |
| 6 | + const PREFIX = "zarr_indexing-" |
| 7 | + |
| 8 | + const parse = version => version |
| 9 | + .replace(/^v/, "") |
| 10 | + .split(/[.+-]/) |
| 11 | + .map(part => parseInt(part, 10) || 0) |
| 12 | + |
| 13 | + const newestFirst = (a, b) => { |
| 14 | + const va = parse(a), vb = parse(b) |
| 15 | + for (let i = 0; i < Math.max(va.length, vb.length); i++) |
| 16 | + if ((vb[i] || 0) !== (va[i] || 0)) |
| 17 | + return (vb[i] || 0) - (va[i] || 0) |
| 18 | + return 0 |
| 19 | + } |
| 20 | + |
| 21 | + async function latestPackageTag() { |
| 22 | + const response = await fetch( |
| 23 | + `https://api.github.com/repos/${REPO}/tags?per_page=100` |
| 24 | + ) |
| 25 | + if (!response.ok) |
| 26 | + return undefined |
| 27 | + const tags = await response.json() |
| 28 | + const versions = tags |
| 29 | + .map(tag => tag.name) |
| 30 | + .filter(name => name.startsWith(PREFIX)) |
| 31 | + .map(name => name.slice(PREFIX.length)) |
| 32 | + .sort(newestFirst) |
| 33 | + return versions[0] |
| 34 | + } |
| 35 | + |
| 36 | + function patchDom(version) { |
| 37 | + for (const el of document.querySelectorAll(".md-source__fact--version")) |
| 38 | + el.textContent = version |
| 39 | + } |
| 40 | + |
| 41 | + function patchCache(version) { |
| 42 | + const facts = __md_get("__source", sessionStorage) |
| 43 | + if (!facts) |
| 44 | + return false |
| 45 | + facts.version = version |
| 46 | + __md_set("__source", facts, sessionStorage) |
| 47 | + return true |
| 48 | + } |
| 49 | + |
| 50 | + latestPackageTag().then(version => { |
| 51 | + if (!version) |
| 52 | + return |
| 53 | + patchDom(version) |
| 54 | + if (patchCache(version)) |
| 55 | + return |
| 56 | + /* The theme's own API request hasn't resolved yet — patch both the |
| 57 | + cache and the DOM once it renders the facts list. */ |
| 58 | + const source = document.querySelector('[data-md-component="source"]') |
| 59 | + if (!source) |
| 60 | + return |
| 61 | + new MutationObserver((_, observer) => { |
| 62 | + if (patchCache(version)) { |
| 63 | + patchDom(version) |
| 64 | + observer.disconnect() |
| 65 | + } |
| 66 | + }).observe(source, { childList: true, subtree: true }) |
| 67 | + }) |
| 68 | +})() |
0 commit comments