Skip to content

Commit 0deabeb

Browse files
author
Dylan Storey
committed
Add version dropdown to documentation
1 parent 5fd5f0f commit 0deabeb

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

docs/book.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ build-dir = "book"
1111
[output.html]
1212
git-repository-url = "https://github.com/colliery-io/graphqlite"
1313
edit-url-template = "https://github.com/colliery-io/graphqlite/edit/main/docs/{path}"
14-
additional-css = []
15-
additional-js = []
14+
additional-css = ["theme/version-select.css"]
15+
additional-js = ["theme/version-select.js"]
1616

1717
[output.html.fold]
1818
enable = true

docs/theme/version-select.css

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Version selector styling */
2+
.version-select {
3+
display: flex;
4+
align-items: center;
5+
margin-left: 1rem;
6+
font-size: 0.9em;
7+
}
8+
9+
.version-select label {
10+
margin-right: 0.3rem;
11+
color: var(--icons);
12+
}
13+
14+
.version-select select {
15+
background: var(--bg);
16+
color: var(--fg);
17+
border: 1px solid var(--icons);
18+
border-radius: 4px;
19+
padding: 0.2rem 0.4rem;
20+
font-size: 0.9em;
21+
cursor: pointer;
22+
}
23+
24+
.version-select select:hover {
25+
border-color: var(--links);
26+
}
27+
28+
.version-select select:focus {
29+
outline: none;
30+
border-color: var(--links);
31+
}

docs/theme/version-select.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Version selector for mdBook
2+
// Reads versions.json and creates a dropdown in the menu bar
3+
4+
(function() {
5+
'use strict';
6+
7+
// Get current version from URL path
8+
function getCurrentVersion() {
9+
const path = window.location.pathname;
10+
const match = path.match(/^\/(v[\d.]+|latest)\//);
11+
return match ? match[1] : 'latest';
12+
}
13+
14+
// Fetch versions and create dropdown
15+
async function initVersionSelect() {
16+
try {
17+
const response = await fetch('/versions.json');
18+
if (!response.ok) return;
19+
20+
const versions = await response.json();
21+
if (!versions || versions.length === 0) return;
22+
23+
const currentVersion = getCurrentVersion();
24+
25+
// Create dropdown HTML
26+
const dropdown = document.createElement('div');
27+
dropdown.className = 'version-select';
28+
dropdown.innerHTML = `
29+
<label for="version-selector">Version: </label>
30+
<select id="version-selector">
31+
${versions.map(v => `<option value="${v}" ${v === currentVersion ? 'selected' : ''}>${v}</option>`).join('')}
32+
</select>
33+
`;
34+
35+
// Add change handler
36+
dropdown.querySelector('select').addEventListener('change', function(e) {
37+
const newVersion = e.target.value;
38+
const currentPath = window.location.pathname;
39+
const newPath = currentPath.replace(/^\/(v[\d.]+|latest)\//, `/${newVersion}/`);
40+
window.location.href = newPath;
41+
});
42+
43+
// Insert into menu bar
44+
const menuBar = document.querySelector('.left-buttons');
45+
if (menuBar) {
46+
menuBar.appendChild(dropdown);
47+
}
48+
} catch (e) {
49+
console.log('Version selector not available:', e.message);
50+
}
51+
}
52+
53+
// Run when DOM is ready
54+
if (document.readyState === 'loading') {
55+
document.addEventListener('DOMContentLoaded', initVersionSelect);
56+
} else {
57+
initVersionSelect();
58+
}
59+
})();

0 commit comments

Comments
 (0)