From 308868a6dfed64515a720a5fca358aa79572b925 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Tue, 30 Apr 2024 13:42:49 +0100 Subject: [PATCH 1/3] fix version-switcher with dirhtml builder --- .../assets/scripts/pydata-sphinx-theme.js | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js b/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js index 805185739..3da384df3 100644 --- a/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js +++ b/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js @@ -320,7 +320,15 @@ var setupSearchButtons = () => { async function checkPageExistsAndRedirect(event) { // ensure we don't follow the initial link event.preventDefault(); - let currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`; + let currentFilePath; + if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") { + currentFilePath = + DOCUMENTATION_OPTIONS.pagename == "index" + ? `/` + : `${DOCUMENTATION_OPTIONS.pagename}/`; + } else { + currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`; + } let tryUrl = event.currentTarget.getAttribute("href"); let otherDocsHomepage = tryUrl.replace(currentFilePath, ""); try { @@ -372,7 +380,15 @@ async function fetchVersionSwitcherJSON(url) { // Populate the version switcher from the JSON data function populateVersionSwitcher(data, versionSwitcherBtns) { - const currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`; + let currentFilePath; + if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") { + currentFilePath = + DOCUMENTATION_OPTIONS.pagename == "index" + ? `/` + : `${DOCUMENTATION_OPTIONS.pagename}/`; + } else { + currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`; + } versionSwitcherBtns.forEach((btn) => { // Set empty strings by default so that these attributes exist and can be used in CSS selectors btn.dataset["activeVersionName"] = ""; @@ -490,7 +506,14 @@ 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`; + if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") { + button.href = + DOCUMENTATION_OPTIONS.pagename == "index" + ? `/` + : `${DOCUMENTATION_OPTIONS.pagename}/`; + } else { + button.href = `${DOCUMENTATION_OPTIONS.pagename}.html`; + } button.innerText = "Switch to stable version"; button.onclick = checkPageExistsAndRedirect; // add the version-dependent text From f614def69cabafb4b5ea265ade640012c6921ad0 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Tue, 14 May 2024 16:03:28 +0100 Subject: [PATCH 2/3] DRY URL path --- .../assets/scripts/pydata-sphinx-theme.js | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js b/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js index 3da384df3..331842a13 100644 --- a/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js +++ b/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js @@ -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 @@ -320,15 +332,7 @@ var setupSearchButtons = () => { async function checkPageExistsAndRedirect(event) { // ensure we don't follow the initial link event.preventDefault(); - let currentFilePath; - if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") { - currentFilePath = - DOCUMENTATION_OPTIONS.pagename == "index" - ? `/` - : `${DOCUMENTATION_OPTIONS.pagename}/`; - } else { - currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`; - } + const currentFilePath = getCurrentUrlPath(); let tryUrl = event.currentTarget.getAttribute("href"); let otherDocsHomepage = tryUrl.replace(currentFilePath, ""); try { @@ -380,15 +384,7 @@ async function fetchVersionSwitcherJSON(url) { // Populate the version switcher from the JSON data function populateVersionSwitcher(data, versionSwitcherBtns) { - let currentFilePath; - if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") { - currentFilePath = - DOCUMENTATION_OPTIONS.pagename == "index" - ? `/` - : `${DOCUMENTATION_OPTIONS.pagename}/`; - } else { - 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"] = ""; @@ -506,14 +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"; - if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") { - button.href = - DOCUMENTATION_OPTIONS.pagename == "index" - ? `/` - : `${DOCUMENTATION_OPTIONS.pagename}/`; - } else { - button.href = `${DOCUMENTATION_OPTIONS.pagename}.html`; - } + button.href = getCurrentUrlPath(); button.innerText = "Switch to stable version"; button.onclick = checkPageExistsAndRedirect; // add the version-dependent text From 6e3760c4033e5df04f122404cceea529119daae6 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Tue, 14 May 2024 10:35:26 -0500 Subject: [PATCH 3/3] Update src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js --- src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js b/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js index 331842a13..173d68e1b 100644 --- a/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js +++ b/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js @@ -502,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 = getCurrentUrlPath(); + button.href = `${preferredURL}${getCurrentUrlPath()}`; button.innerText = "Switch to stable version"; button.onclick = checkPageExistsAndRedirect; // add the version-dependent text