Skip to content

Commit

Permalink
fix version-switcher with dirhtml builder (#1795)
Browse files Browse the repository at this point in the history
Problem: version-switcher suffixes .html even if sphinx was used with the dirhtml builder.

Solution: suffix / instead where necessary.

---------

Co-authored-by: Daniel McCloy <[email protected]>
  • Loading branch information
casperdcl and drammock authored May 20, 2024
1 parent 9761e1d commit cad0c74
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@ var setupSearchButtons = () => {
* - DOCUMENTATION_OPTIONS.theme_switcher_url
*/

/**
* path component of URL
*/
var getCurrentUrlPath = () => {
if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") {
return DOCUMENTATION_OPTIONS.pagename == "index"
? `/`
: `${DOCUMENTATION_OPTIONS.pagename}/`;
}
return `${DOCUMENTATION_OPTIONS.pagename}.html`;
};

/**
* Check if corresponding page path exists in other version of docs
* and, if so, go there instead of the homepage of the other docs version
Expand All @@ -320,7 +332,7 @@ var setupSearchButtons = () => {
async function checkPageExistsAndRedirect(event) {
// ensure we don't follow the initial link
event.preventDefault();
let currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`;
const currentFilePath = getCurrentUrlPath();
let tryUrl = event.currentTarget.getAttribute("href");
let otherDocsHomepage = tryUrl.replace(currentFilePath, "");
try {
Expand Down Expand Up @@ -372,7 +384,7 @@ async function fetchVersionSwitcherJSON(url) {

// Populate the version switcher from the JSON data
function populateVersionSwitcher(data, versionSwitcherBtns) {
const currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`;
const currentFilePath = getCurrentUrlPath();
versionSwitcherBtns.forEach((btn) => {
// Set empty strings by default so that these attributes exist and can be used in CSS selectors
btn.dataset["activeVersionName"] = "";
Expand Down Expand Up @@ -490,7 +502,7 @@ function showVersionWarningBanner(data) {
inner.classList = "sidebar-message";
button.classList =
"btn text-wrap font-weight-bold ms-3 my-1 align-baseline pst-button-link-to-stable-version";
button.href = `${preferredURL}${DOCUMENTATION_OPTIONS.pagename}.html`;
button.href = `${preferredURL}${getCurrentUrlPath()}`;
button.innerText = "Switch to stable version";
button.onclick = checkPageExistsAndRedirect;
// add the version-dependent text
Expand Down

0 comments on commit cad0c74

Please sign in to comment.