Files
openmlsys-zh/theme/version-selector.js
Yeqi Huang d953030747 feat: add v1/v2 versioning with language selector (#494)
* feat: add v1/v2 versioning and language selector for mdbook

- Copy current content to v1/ directory (1st Edition)
- Create v2/ directory with new TOC structure (2nd Edition) and placeholder chapters
- Add version selector (V1/V2) and language toggle (EN/ZH) in top-right nav bar
- Add build scripts: build_mdbook_v1.sh, build_mdbook_v2.sh
- Update assemble_docs_publish_tree.py to support v1/v2 deployment layout
- Fix mdbook preprocessor to use 'sections' key (v0.4.43 compatibility)
- Update .gitignore for new build artifact directories
- Deployment layout: / = v2 EN, /cn/ = v2 ZH, /v1/ = v1 EN, /v1/cn/ = v1 ZH

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* build: update CI to build and verify all four books (v1/v2 x EN/ZH)

- Clarify step names: "Build v2 (EN + ZH)" and "Build v1 (EN + ZH)"
- Add verification step to check all four index.html outputs exist
- Deploy workflow assembles: / = v2 EN, /cn/ = v2 ZH, /v1/ = v1 EN, /v1/cn/ = v1 ZH

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: gracefully skip missing TOC entries instead of crashing

resolve_toc_target() now returns None for missing files instead of
raising FileNotFoundError. This fixes v1 EN build where chapter index
files reference TOC entry names that don't match actual filenames.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 13:37:42 +00:00

75 lines
2.1 KiB
JavaScript

// Version and Language selector for OpenMLSys mdbook
(function () {
"use strict";
var path = window.location.pathname;
// Detect current version and language from URL
var currentVersion = "v2";
var currentLang = "en";
if (path.match(/\/v1(\/|$)/)) {
currentVersion = "v1";
}
if (path.match(/\/cn(\/|$)/)) {
currentLang = "zh";
}
// Build base paths
function basePath(version, lang) {
var docsRoot = path.replace(/\/v1\/.*/, "/").replace(/\/cn\/.*/, "/");
docsRoot = docsRoot.replace(/(\/docs\/?).*/, "/docs/");
if (!docsRoot.endsWith("/")) docsRoot += "/";
var p = docsRoot;
if (version === "v1") p += "v1/";
if (lang === "zh") p += "cn/";
return p;
}
var container = document.createElement("span");
container.className = "openmlsys-nav-selectors";
// --- Version links: V1 | V2 ---
var versions = [
{ label: "V1", value: "v1" },
{ label: "V2", value: "v2" },
];
versions.forEach(function (v) {
var a = document.createElement("a");
a.className = "openmlsys-selector-link";
a.textContent = v.label;
a.href = basePath(v.value, currentLang);
if (v.value === currentVersion) a.classList.add("active");
container.appendChild(a);
});
// Separator
var sep = document.createElement("span");
sep.className = "openmlsys-selector-sep";
container.appendChild(sep);
// --- Language toggle: single button that switches to the other language ---
var otherLang = currentLang === "zh" ? "en" : "zh";
var langLink = document.createElement("a");
langLink.className = "openmlsys-selector-link";
langLink.textContent = currentLang === "zh" ? "EN" : "ZH";
langLink.href = basePath(currentVersion, otherLang);
container.appendChild(langLink);
// Insert into .right-buttons, before existing icons
function insertSelector() {
var rightButtons = document.querySelector(".right-buttons");
if (rightButtons) {
rightButtons.insertBefore(container, rightButtons.firstChild);
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", insertSelector);
} else {
insertSelector();
}
})();