Skip to content

Instantly share code, notes, and snippets.

@muellerzr
Created June 8, 2023 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muellerzr/fcf8176eee94041896f533422d1910b2 to your computer and use it in GitHub Desktop.
Save muellerzr/fcf8176eee94041896f533422d1910b2 to your computer and use it in GitHub Desktop.
Javascript which will hide semantically-versioned sidebars in Quarto. Designed to be used in conjunction with nbquarto/referenced from it
/**
* Enables semantic versioning through careful sidebar menu item selection.
* Hide sidebar menu items that are not related to the current page that is open.
* Assumes a directory structure of:
* - version_1
* - page_1
* - page_2
* - version_2
* - page_2
* - page_3
*
* If version_1 is open, then version_2 and it's pages will not be visible to the sidebar.
* These will also link to {url}/{version_num}/page_{num}.
*
* In the `_quarto.yml` sidebar *must* be set to `auto` for this to work.
*/
var all_versioned_menus = $(".sidebar-menu-container > .list-unstyled").children()
// Get the current url, which should be something like: /branch_name/{version_number}/{something}
// the latter parts after the version number are not important nor will be in there.
// eventually need to handle a special case when we use the latest stable version
var url = window.location.pathname.split("/")[1]
for (var versioned_menu of all_versioned_menus){
// Check if the current url extension is in the sidebar menu
var active_sidebar = $(versioned_menu).find(`a[href*="${url}"]`)[0]
// If it is, wrap it in a div so it's easily recognizeable
if (active_sidebar !== undefined){
$(active_sidebar).parent().parent().wrap("<div id='active-sidebar'></div>")
}
// Else hide the additional menus
else {
versioned_menu.style.display = "none"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment