-
Notifications
You must be signed in to change notification settings - Fork 341
✨ NEW: add version switcher #436
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
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
84d0562
add version switcher (cleaner diff this time)
drammock 5f4a023
remove unreachable ancient versions
drammock 93b0aa0
fallback on version if name missing
drammock e913a48
simplify redundancies in switcher.json
drammock cefafeb
separate out version switcher
drammock 0f12757
remove language-related stuff
drammock 0ff914e
add recent versions to JSON
drammock 852cc4e
do AJAX onclick, not in advance
drammock ac2a57b
use href instead of data-href
drammock 26e5988
better comments
drammock 0328868
update the dropdown button text
drammock 2a38901
fix url shown on hover
drammock 768a76e
fix template URL
drammock 5318996
change display name "latest" → "stable"
drammock ab261a3
add ID to div
drammock def2d67
document the switcher
drammock d968eef
change default button text to be sphinx "version" variable
drammock fec276c
add comment about JS needing jinja
drammock 24da28e
doc rewrite
drammock 407159d
better variable name, remove vars from theme.conf, style current vers…
drammock f009b9d
minor cleanups
drammock 87c0dc9
use html_theme_options instead of html_context
drammock 191a847
fix copy-paste error
drammock e683a50
better code comments
drammock 3258028
make switcher config a single dict
drammock 9ad61e2
Merge remote-tracking branch 'upstream/master' into redo-switcher
jorisvandenbossche a010d24
switch url
jorisvandenbossche 865ed64
prettier json
jorisvandenbossche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
[ | ||
{ | ||
"name": "v0.7.1 (stable)", | ||
"version": "0.7.1" | ||
}, | ||
{ | ||
"version": "0.7.0" | ||
}, | ||
{ | ||
"version": "0.6.3" | ||
}, | ||
{ | ||
"version": "0.6.2" | ||
}, | ||
{ | ||
"version": "0.6.1" | ||
}, | ||
{ | ||
"version": "0.6.0" | ||
}, | ||
{ | ||
"version": "0.5.2" | ||
}, | ||
{ | ||
"version": "0.5.1" | ||
}, | ||
{ | ||
"version": "0.5.0" | ||
}, | ||
{ | ||
"version": "0.4.3" | ||
}, | ||
{ | ||
"version": "0.4.2" | ||
}, | ||
{ | ||
"version": "0.4.1" | ||
}, | ||
{ | ||
"version": "0.4.0" | ||
}, | ||
{ | ||
"version": "0.3.2" | ||
}, | ||
{ | ||
"version": "0.3.1" | ||
}, | ||
{ | ||
"version": "0.3.0" | ||
} | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/pydata_sphinx_theme/theme/pydata_sphinx_theme/_templates/version-switcher.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<div class="dropdown" id="version_switcher"> | ||
<button type="button" class="btn btn-primary btn-sm navbar-btn dropdown-toggle" id="version_switcher_button" data-toggle="dropdown"> | ||
{{ theme_switcher.get('version_match') }} <!-- this text may get changed later by javascript --> | ||
<span class="caret"></span> | ||
</button> | ||
<div id="version_switcher_menu" class="dropdown-menu list-group-flush py-0" aria-labelledby="version_switcher_button"> | ||
<!-- dropdown will be populated by javascript on page load --> | ||
</div> | ||
</div> | ||
|
||
<!-- NOTE: this JS must live here (not in our global JS file) because it relies | ||
on being processed by Jinja before it is run (specifically for replacing | ||
variables {{ pagename }} and {{ theme_switcher }}. | ||
--> | ||
|
||
<script type="text/javascript"> | ||
// Construct the target URL from the JSON components | ||
function buildURL(entry) { | ||
var template = "{{ theme_switcher.get('url_template') }}"; // supplied by jinja | ||
template = template.replace("{version}", entry.version); | ||
return template; | ||
} | ||
|
||
// 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 | ||
function checkPageExistsAndRedirect(event) { | ||
const currentFilePath = "{{ pagename }}.html", | ||
tryUrl = event.target.getAttribute("href"); | ||
let otherDocsHomepage = tryUrl.replace(currentFilePath, ""); | ||
$.ajax({ | ||
type: 'HEAD', | ||
url: tryUrl, | ||
// if the page exists, go there | ||
success: function() { | ||
location.href = tryUrl; | ||
} | ||
}).fail(function() { | ||
location.href = otherDocsHomepage; | ||
}); | ||
// this prevents the browser from following the href of the clicked node | ||
// (which is fine because this function takes care of redirecting) | ||
return false; | ||
} | ||
|
||
// Populate the version switcher from the JSON config file | ||
(function () { | ||
$.getJSON("{{ theme_switcher.get('json_url') }}", function(data, textStatus, jqXHR) { | ||
const currentFilePath = "{{ pagename }}.html"; | ||
// create links to the corresponding page in the other docs versions | ||
$.each(data, function(index, entry) { | ||
// if no custom name specified (e.g., "latest"), use version string | ||
if (!("name" in entry)) { | ||
entry.name = entry.version; | ||
} | ||
// create the node | ||
const node = document.createElement("a"); | ||
node.setAttribute("class", "list-group-item list-group-item-action py-1"); | ||
node.textContent = `${entry.name}`; | ||
// get the base URL for that doc version, add the current page's | ||
// path to it, and set as `href` | ||
entry.url = buildURL(entry); | ||
node.setAttribute("href", `${entry.url}${currentFilePath}`); | ||
// on click, AJAX calls will check if the linked page exists before | ||
// trying to redirect, and if not, will redirect to the homepage | ||
// for that version of the docs. | ||
node.onclick = checkPageExistsAndRedirect; | ||
$("#version_switcher_menu").append(node); | ||
// replace dropdown button text with the preferred display name of | ||
// this version, rather than using sphinx's {{ version }} variable. | ||
// also highlight the dropdown entry for the currently-viewed | ||
// version's entry | ||
if (entry.version == "{{ theme_switcher.get('version_match') }}") { | ||
node.classList.add("active"); | ||
$("#version_switcher_button").text(entry.name); | ||
} | ||
}); | ||
}); | ||
})(); | ||
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.