Files
ROCm/docs/sphinx/static/setup-toc-install-headings.js
2025-12-11 16:51:22 -05:00

84 lines
2.7 KiB
JavaScript

const TOC_QUERY = ".bd-docs-nav";
const TOC2_QUERY = ".bd-sidebar-secondary";
// /install/rocm.html
const INSTALL_PAGE_DIR = "install";
const INSTALL_PAGE_FILE = "rocm.html";
const TOC_ENTRIES_TO_MODIFY = [
{ tocQuery: "a[href$='redirect/_prerequisites.html']", toc2Query: "a[href='#prerequisites']" },
{ tocQuery: "a[href$='redirect/_install.html']", toc2Query: "a[href='#installation']" },
{ tocQuery: "a[href$='redirect/_post-install.html']", toc2Query: "a[href='#post-installation']" },
{ tocQuery: "a[href$='redirect/_uninstall.html']", toc2Query: "a[href='#uninstalling']" },
];
function domReady(callback) {
if (document.readyState !== "loading") callback();
else document.addEventListener("DOMContentLoaded", callback, { once: true });
}
function getDocsRoot() {
const pathname = window.location.pathname;
const parts = pathname.split("/").filter(Boolean);
// If ur already inside /install/, preserve the path up to install/
if (pathname.includes(`/${INSTALL_PAGE_DIR}/`)) {
const installIdx = parts.indexOf(INSTALL_PAGE_DIR);
return "/" + parts.slice(0, installIdx + 1).join("/") + "/";
}
// HACK:
// Look for version pattern: at least two dots (e.g., 6.4.3, 7.10.0-preview)
const versionIdx = parts.findIndex(part =>
(part.match(/\./g) || []).length >= 2 || /^\d+$/.test(part)
);
if (versionIdx !== -1) {
return "/" + parts.slice(0, versionIdx + 1).join("/") + `/${INSTALL_PAGE_DIR}/`;
}
// Fallback to root-level /install/
return `/${INSTALL_PAGE_DIR}/`;
}
function buildHref(docsRoot, page, hash) {
return `${docsRoot}${page}${hash ? "#" + hash : ""}`;
}
function watchClassChange(elem, cls, onAdd, onRemove) {
if (!elem) return;
const obs = new MutationObserver(() => {
elem.classList.contains(cls) ? onAdd?.() : onRemove?.();
});
obs.observe(elem, { attributes: true, attributeFilter: ["class"] });
}
domReady(() => {
const toc = document.querySelector(TOC_QUERY);
const toc2 = document.querySelector(TOC2_QUERY);
if (!toc) return;
const docsRoot = getDocsRoot();
TOC_ENTRIES_TO_MODIFY.forEach((item) => {
const tocElem = toc.querySelector(item.tocQuery);
if (!tocElem) {
console.warn(`[ROCmDocsToc]: No ${item.tocQuery} found on page`);
return;
}
const anchorId = item.toc2Query.match(/#([^'"\]]+)/)?.[1];
if (!anchorId) return;
tocElem.href = buildHref(docsRoot, INSTALL_PAGE_FILE, anchorId);
const toc2Elem = toc2?.querySelector(item.toc2Query);
if (!toc2Elem) return;
watchClassChange(
toc2Elem,
"active",
() => tocElem.parentElement.classList.add("current"),
() => tocElem.parentElement.classList.remove("current"),
);
});
});