Files
openmlsys-zh/theme/version-selector.js
Yeqi Huang 95a086903b fix: fix 404 on version switch from index.html and strip pandoc heading IDs (#496)
Two fixes:

1. version-selector.js: Simplify basePath() to determine site root from
   the URL prefix (/ or /docs/) instead of trying to strip path segments.
   Previously, visiting /index.html caused basePath to compute
   /index.html/v1/ (404) because it didn't strip the filename first.

2. prepare_mdbook.py: Strip pandoc-style heading IDs ({#label}) from
   both extracted titles (used in SUMMARY.md sidebar) and rendered
   markdown content. Fixes "Model Deployment {#ch:deploy}" showing
   raw in the sidebar and page headings.

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

76 lines
2.2 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 — always navigate to root of target version
function basePath(version, lang) {
// Determine site root: everything before /v1/, /cn/, or first path segment after /docs/
var root = "/";
var docsMatch = path.match(/^(\/docs\/)/);
if (docsMatch) root = docsMatch[1];
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();
}
})();