Skip to content

Commit ce5b194

Browse files
committed
docs: Add RTD JS to integrate versions menu in the site navigation
1 parent 07d47f5 commit ce5b194

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

docs/javascript/readthedocs.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Use CustomEvent to generate the version selector
2+
document.addEventListener("readthedocs-addons-data-ready", function (event) {
3+
const config = event.detail.data();
4+
const versioning = `
5+
<div class="md-version">
6+
<button class="md-version__current" aria-label="Select version">
7+
${config.versions.current.slug}
8+
</button>
9+
10+
<ul class="md-version__list">
11+
${config.versions.active
12+
.map(
13+
(version) => `
14+
<li class="md-version__item">
15+
<a href="${version.urls.documentation}" class="md-version__link">
16+
${version.slug}
17+
</a>
18+
</li>`,
19+
)
20+
.join("\n")}
21+
</ul>
22+
</div>`;
23+
24+
// Check if we already added versions and remove them if so.
25+
// This happens when using the "Instant loading" feature.
26+
// See https://squidfunk.github.io/mkdocs-material/setup/setting-up-navigation/#instant-loading
27+
const currentVersions = document.querySelector(".md-version");
28+
if (currentVersions !== null) {
29+
currentVersions.remove();
30+
}
31+
document
32+
.querySelector(".md-header__topic")
33+
.insertAdjacentHTML("beforeend", versioning);
34+
});

docs/overrides/main.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{% extends "base.html" %}
2+
3+
{% block site_meta %}
4+
{{ super() }}
5+
<meta name="readthedocs-addons-api-version" content="1" />
6+
{% endblock %}

0 commit comments

Comments
 (0)