Skip to content

Commit b8e2e61

Browse files
committed
docs: show package tag instead of monorepo release in subpackage doc headers
1 parent 24f9ad1 commit b8e2e61

4 files changed

Lines changed: 146 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
})()

packages/zarr-indexing/mkdocs.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ plugins:
9393
- https://numpy.org/doc/stable/objects.inv
9494
- https://zarr.readthedocs.io/en/stable/objects.inv
9595

96+
# Override the header source widget's version fact (the monorepo's latest
97+
# release) with the latest zarr_indexing-v* tag.
98+
extra_javascript:
99+
- _static/source-version.js
100+
96101
markdown_extensions:
97102
- admonition
98103
- attr_list
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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_metadata-"
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+
})()

packages/zarr-metadata/mkdocs.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ plugins:
9191
- https://docs.python.org/3/objects.inv
9292
- https://zarr.readthedocs.io/en/stable/objects.inv
9393

94+
# Override the header source widget's version fact (the monorepo's latest
95+
# release) with the latest zarr_metadata-v* tag.
96+
extra_javascript:
97+
- _static/source-version.js
98+
9499
markdown_extensions:
95100
- admonition
96101
- attr_list

0 commit comments

Comments
 (0)