Compare commits

..

1 commit

Author SHA1 Message Date
Johannes Marbach 2dd6625339
Merge 77b37cb9a9 into d28e05af87 2025-12-03 07:59:23 +00:00
2 changed files with 22 additions and 33 deletions

View file

@ -14,11 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Determine the current version as defined in hugo.toml. This will either be
// "unstable" or "vX.X" and doesn't depend on the current URL.
//
// The oddity below is an attempt at producing a readable Hugo template while
// avoiding JS syntax errors in your IDE.
// This oddity is an attempt at producing a readable Hugo template while avoiding
// JS syntax errors in your IDE
const currentVersion = `{{ if eq .Site.Params.version.status "unstable" }}
{{- /**/ -}}
unstable
@ -29,31 +26,20 @@ const currentVersion = `{{ if eq .Site.Params.version.status "unstable" }}
{{- /**/ -}}
{{ end }}`;
// Determine the current version segment by regex matching the URL. This will either
// be "unstable", "latest", "vX.X" (production) or undefined (local & netlify).
const href = window.location.href;
const segmentMatches = href.match(/(?<=\/)unstable|latest|v\d+.\d+(?=\/)/);
const currentSegment = segmentMatches ? segmentMatches[0] : undefined;
// Determine the selected menu element. If we were able to obtain the version
// segment from the URL (production), use that. Otherwise (local & netlify),
// fall back to the version as defined in Hugo.
const selected = currentSegment ?? currentVersion;
function appendVersion(parent, name, url) {
// The list item
const li = document.createElement("li");
if (name === selected) {
li.classList.add("selected");
if (name === currentVersion) {
li.classList.add("selected")
}
if (name === "latest") {
li.classList.add("latest");
if (name === "unstable") {
li.classList.add("unstable")
}
parent.appendChild(li);
// The link
const a = document.createElement("a");
a.classList.add("dropdown-item");
a.classList.add("dropdown-item")
a.setAttribute("href", url);
li.appendChild(a);
@ -65,16 +51,20 @@ function appendVersion(parent, name, url) {
return;
}
// If we couldn't determine the current segment, we cannot safely replace
// it and have to let the browser load the (root) URL instead
if (!currentSegment) {
// Stop further event handling
ev.preventDefault();
ev.stopPropagation();
// Try to find the current version segment
const href = window.location.href;
const matches = href.match(/\/unstable\/|\/latest\/|\/v\d+.\d+\//g);
if (!matches) {
window.location.href = url;
return;
}
// Otherwise, stop further event handling and replace the segment
ev.preventDefault();
ev.stopPropagation();
window.location.href = href.replace(`/${currentSegment}/`, `/${name}/`);
// Replace the segment
window.location.href = href.replace(matches[0], `/${name}/`);
});
// The link text
@ -93,9 +83,8 @@ fetch("/versions.json")
return;
}
// Add a entries for the unstable version and the "latest" shortcut
// Add an entry for the unstable version
appendVersion(ul, "unstable", "https://spec.matrix.org/unstable");
appendVersion(ul, "latest", "https://spec.matrix.org/latest");
// Add an entry for each proper version
for (const version of versions) {

View file

@ -58,11 +58,11 @@ Custom SCSS for the Matrix spec
}
ul#version-selector li.selected a {
font-weight: bold;
color: $secondary;
}
ul#version-selector li.latest a {
color: $secondary;
ul#version-selector li.unstable:not(.selected) a {
color: $warning;
}
}