Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix version-switcher with dirhtml builder #1795

Merged
merged 3 commits into from
May 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
? `/`
Copy link
Contributor Author

@casperdcl casperdcl May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw I'm unsure if this would suffice (avoids duplicate // suffix, but maybe breaks in some unknown edge cases? I haven't tested it thoroughly)

Suggested change
? `/`
? ""

: `${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
Loading