diff --git a/doc/source/_static/js/custom.js b/doc/source/_static/js/custom.js index 569d1e5373bd..904bc2ce1bdd 100644 --- a/doc/source/_static/js/custom.js +++ b/doc/source/_static/js/custom.js @@ -25,34 +25,16 @@ function loadVisibleTermynals() { } // Store the state of the page in the browser's local storage. -// This includes the sidebar dropdown menus which have been opened, and the -// sidebar scroll position. -function handleState() { +// For now this includes just the sidebar scroll position. +document.addEventListener("DOMContentLoaded", () => { const sidebar = document.getElementById("main-sidebar") window.addEventListener("beforeunload", () => { if (sidebar) { - // Save opened checkboxes - localStorage.setItem("checkboxes", - JSON.stringify( - Array.from( - sidebar.querySelectorAll("input[type=checkbox]") - ).filter(input => input.checked).map(input => input.id) - ) - ) - - // Save scroll postion localStorage.setItem("scroll", sidebar.scrollTop) } }) - const storedCheckboxes = localStorage.getItem("checkboxes") - if (storedCheckboxes) { - JSON.parse(storedCheckboxes).forEach(id => { - document.getElementById(id).checked = true - }) - } - const storedScrollPosition = localStorage.getItem("scroll") if (storedScrollPosition) { if (sidebar) { @@ -61,7 +43,7 @@ function handleState() { localStorage.removeItem("scroll"); } -} +}) // Send GA events any time a code block is copied document.addEventListener("DOMContentLoaded", function() { @@ -96,4 +78,3 @@ document.addEventListener("DOMContentLoaded", function() { window.addEventListener("scroll", loadVisibleTermynals); createTermynals(); loadVisibleTermynals(); -handleState() diff --git a/doc/source/custom_directives.py b/doc/source/custom_directives.py index 82bd4da02604..18e8ff3120ab 100644 --- a/doc/source/custom_directives.py +++ b/doc/source/custom_directives.py @@ -212,7 +212,12 @@ def parse_navbar_config(app: sphinx.application.Sphinx, config: sphinx.config.Co NavEntry = Dict[str, Union[str, List["NavEntry"]]] -def preload_sidebar_nav(get_toctree: Callable[[Any], str]) -> bs4.BeautifulSoup: +def preload_sidebar_nav( + get_toctree: Callable[[Any], str], + pathto: Callable[[str], str], + root_doc: str, + pagename: str, +) -> bs4.BeautifulSoup: """Return the navigation link structure in HTML. This function is modified from the pydata_sphinx_theme function @@ -247,59 +252,79 @@ def preload_sidebar_nav(get_toctree: Callable[[Any], str]) -> bs4.BeautifulSoup: Soup to display in the side navbar """ if hasattr(preload_sidebar_nav, "cached_toctree"): - return preload_sidebar_nav.cached_toctree - - toctree = get_toctree( - collapse=False, includehidden=True, maxdepth=4, titles_only=True - ) + # Need to retrieve a copy of the cached toctree HTML so as not to modify + # the cached version when we set the "checked" state of the inputs + soup = copy.copy(preload_sidebar_nav.cached_toctree) + else: + toctree = get_toctree( + collapse=False, includehidden=True, maxdepth=4, titles_only=True + ) - soup = bs4.BeautifulSoup(toctree, "html.parser") + soup = bs4.BeautifulSoup(toctree, "html.parser") + + # Remove "current" class since this toctree HTML is being reused + # from some other page + for li in soup("li", {"class": "current"}): + li["class"].remove("current") + + # Remove sidebar links to sub-headers on the page + for li in soup.select("li"): + # Remove + if li.find("a"): + href = li.find("a")["href"] + if "#" in href and href != "#": + li.decompose() + + # Add bootstrap classes for first `ul` items + for ul in soup("ul", recursive=False): + ul.attrs["class"] = ul.attrs.get("class", []) + ["nav", "bd-sidenav"] + + # Add collapse boxes for parts/captions. + # Wraps the TOC part in an extra