Skip to content

Commit edb209a

Browse files
committed
feat(docs): integrate ReadTheDocs version selector
- Add JavaScript for ReadTheDocs version menu integration - Switch from external CDN to self-hosted JS file - Improves documentation navigation and reliability
1 parent 0ff8acb commit edb209a

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

docs/javascript/readthedocs.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// ref: <https://docs.readthedocs.com/platform/stable/intro/mkdocs.html#integrate-the-read-the-docs-version-menu-into-your-site-navigation>
2+
3+
document.addEventListener("DOMContentLoaded", function (event) {
4+
// Trigger Read the Docs' search addon instead of Material MkDocs default
5+
document.querySelector(".md-search__input").addEventListener("focus", (e) => {
6+
const event = new CustomEvent("readthedocs-search-show");
7+
document.dispatchEvent(event);
8+
});
9+
});
10+
11+
// Use CustomEvent to generate the version selector
12+
document.addEventListener("readthedocs-addons-data-ready", function (event) {
13+
const config = event.detail.data();
14+
const versioning = `
15+
<div class="md-version">
16+
<button class="md-version__current" aria-label="Select version">
17+
${config.versions.current.slug}
18+
</button>
19+
20+
<ul class="md-version__list">
21+
${config.versions.active
22+
.map(
23+
(version) => `
24+
<li class="md-version__item">
25+
<a href="${version.urls.documentation}" class="md-version__link">
26+
${version.slug}
27+
</a>
28+
</li>`,
29+
)
30+
.join("\n")}
31+
</ul>
32+
</div>`;
33+
34+
// Check if we already added versions and remove them if so.
35+
// This happens when using the "Instant loading" feature.
36+
// See https://squidfunk.github.io/mkdocs-material/setup/setting-up-navigation/#instant-loading
37+
const currentVersions = document.querySelector(".md-version");
38+
if (currentVersions !== null) {
39+
currentVersions.remove();
40+
}
41+
document
42+
.querySelector(".md-header__topic")
43+
.insertAdjacentHTML("beforeend", versioning);
44+
});

template/.config/copier/mkdocs.yaml.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extra_css:
2222

2323
extra_javascript:
2424
- https://cdn.jsdelivr.net/gh/liblaf/copier-python/docs/javascript/mathjax.min.js
25-
- https://cdn.jsdelivr.net/gh/readthedocs/test-builds@mkdocs-material/docs/javascripts/readthedocs.min.js
25+
- https://cdn.jsdelivr.net/gh/liblaf/copier-python/docs/javascript/readthedocs.min.js
2626
- https://unpkg.com/mathjax@3/es5/tex-mml-chtml.js
2727

2828
theme:

0 commit comments

Comments
 (0)