Skip to content

Commit f614def

Browse files
committed
DRY URL path
1 parent 308868a commit f614def

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,18 @@ var setupSearchButtons = () => {
311311
* - DOCUMENTATION_OPTIONS.theme_switcher_url
312312
*/
313313

314+
/**
315+
* path component of URL
316+
*/
317+
var getCurrentUrlPath = () => {
318+
if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") {
319+
return DOCUMENTATION_OPTIONS.pagename == "index"
320+
? `/`
321+
: `${DOCUMENTATION_OPTIONS.pagename}/`;
322+
}
323+
return `${DOCUMENTATION_OPTIONS.pagename}.html`;
324+
};
325+
314326
/**
315327
* Check if corresponding page path exists in other version of docs
316328
* and, if so, go there instead of the homepage of the other docs version
@@ -320,15 +332,7 @@ var setupSearchButtons = () => {
320332
async function checkPageExistsAndRedirect(event) {
321333
// ensure we don't follow the initial link
322334
event.preventDefault();
323-
let currentFilePath;
324-
if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") {
325-
currentFilePath =
326-
DOCUMENTATION_OPTIONS.pagename == "index"
327-
? `/`
328-
: `${DOCUMENTATION_OPTIONS.pagename}/`;
329-
} else {
330-
currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`;
331-
}
335+
const currentFilePath = getCurrentUrlPath();
332336
let tryUrl = event.currentTarget.getAttribute("href");
333337
let otherDocsHomepage = tryUrl.replace(currentFilePath, "");
334338
try {
@@ -380,15 +384,7 @@ async function fetchVersionSwitcherJSON(url) {
380384

381385
// Populate the version switcher from the JSON data
382386
function populateVersionSwitcher(data, versionSwitcherBtns) {
383-
let currentFilePath;
384-
if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") {
385-
currentFilePath =
386-
DOCUMENTATION_OPTIONS.pagename == "index"
387-
? `/`
388-
: `${DOCUMENTATION_OPTIONS.pagename}/`;
389-
} else {
390-
currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`;
391-
}
387+
const currentFilePath = getCurrentUrlPath();
392388
versionSwitcherBtns.forEach((btn) => {
393389
// Set empty strings by default so that these attributes exist and can be used in CSS selectors
394390
btn.dataset["activeVersionName"] = "";
@@ -506,14 +502,7 @@ function showVersionWarningBanner(data) {
506502
inner.classList = "sidebar-message";
507503
button.classList =
508504
"btn text-wrap font-weight-bold ms-3 my-1 align-baseline pst-button-link-to-stable-version";
509-
if (DOCUMENTATION_OPTIONS.BUILDER == "dirhtml") {
510-
button.href =
511-
DOCUMENTATION_OPTIONS.pagename == "index"
512-
? `/`
513-
: `${DOCUMENTATION_OPTIONS.pagename}/`;
514-
} else {
515-
button.href = `${DOCUMENTATION_OPTIONS.pagename}.html`;
516-
}
505+
button.href = getCurrentUrlPath();
517506
button.innerText = "Switch to stable version";
518507
button.onclick = checkPageExistsAndRedirect;
519508
// add the version-dependent text

0 commit comments

Comments
 (0)