-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathversion-selector.js
More file actions
83 lines (71 loc) · 2.52 KB
/
version-selector.js
File metadata and controls
83 lines (71 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(function () {
"use strict";
// Determine the base path (e.g., "/torc/")
var basePath = "/torc/";
// Parse current version from URL path
function getCurrentVersion() {
var path = window.location.pathname;
// Match /torc/<version>/ where version is "latest" or a tag like v1.2.3, v1.2.3-rc1
var match = path.match(/^\/torc\/(v[^/]+|latest)\//);
return match ? match[1] : "latest";
}
// Get the relative page path within the versioned docs
function getRelativePath() {
var path = window.location.pathname;
var match = path.match(/^\/torc\/(?:v[^/]+|latest)\/(.*)/);
return match ? match[1] : "";
}
function createSelector(versions, currentVersion) {
var container = document.createElement("div");
container.className = "version-selector";
var select = document.createElement("select");
select.id = "version-select";
select.setAttribute("aria-label", "Select documentation version");
versions.forEach(function (v) {
var option = document.createElement("option");
option.value = v.path;
option.textContent = v.label;
if (v.version === currentVersion) {
option.selected = true;
}
select.appendChild(option);
});
select.addEventListener("change", function () {
var relativePath = getRelativePath();
var targetUrl = select.value + relativePath;
// Check if the page exists in the target version; fall back to version root
fetch(targetUrl, { method: "HEAD" })
.then(function (response) {
window.location.href = response.ok ? targetUrl : select.value;
})
.catch(function () {
window.location.href = select.value;
});
});
container.appendChild(select);
return container;
}
function init() {
fetch(basePath + "versions.json")
.then(function (response) {
if (!response.ok) throw new Error("versions.json not found");
return response.json();
})
.then(function (data) {
var currentVersion = getCurrentVersion();
var selector = createSelector(data.versions, currentVersion);
var rightButtons = document.querySelector(".right-buttons");
if (rightButtons) {
rightButtons.insertBefore(selector, rightButtons.firstChild);
}
})
.catch(function () {
// Silently fail — expected in local dev without versions.json
});
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
})();