Skip to content

Commit 66f491d

Browse files
Version switcher provides explicit URLs (#604)
Co-authored-by: Damian Avila <[email protected]>
1 parent 9481c3d commit 66f491d

File tree

4 files changed

+23
-53
lines changed

4 files changed

+23
-53
lines changed

docs/_static/switcher.json

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
[
22
{
33
"name": "dev",
4-
"version": "latest"
4+
"version": "latest",
5+
"url": "https://pydata-sphinx-theme.readthedocs.io/en/latest/"
56
},
67
{
78
"name": "0.8.0 (stable)",
89
"version": "stable",
9-
"extra_classes": ["stable"]
10+
"url": "https://pydata-sphinx-theme.readthedocs.io/en/stable/"
1011
},
1112
{
1213
"name": "0.8.0",
13-
"version": "v0.8.0"
14+
"version": "v0.8.0",
15+
"url": "https://pydata-sphinx-theme.readthedocs.io/en/v0.8.0/"
1416
},
1517
{
1618
"name": "0.7.2",
17-
"version": "v0.7.2"
19+
"version": "v0.7.2",
20+
"url": "https://pydata-sphinx-theme.readthedocs.io/en/v0.7.2/"
1821
},
1922
{
2023
"name": "0.6.3",
21-
"version": "v0.6.3"
24+
"version": "v0.6.3",
25+
"url": "https://pydata-sphinx-theme.readthedocs.io/en/v0.6.3/"
2226
},
2327
{
2428
"name": "0.5.2",
25-
"version": "v0.5.2"
29+
"version": "v0.5.2",
30+
"url": "https://pydata-sphinx-theme.readthedocs.io/en/v0.5.2/"
2631
},
2732
{
2833
"name": "0.4.3",
29-
"version": "v0.4.3"
34+
"version": "v0.4.3",
35+
"url": "https://pydata-sphinx-theme.readthedocs.io/en/v0.4.3/"
3036
},
3137
{
3238
"name": "0.3.2",
33-
"version": "v0.3.2"
39+
"version": "v0.3.2",
40+
"url": "https://pydata-sphinx-theme.readthedocs.io/en/v0.3.2/"
3441
}
3542
]

docs/conf.py

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
# "footer_items": ["copyright", "sphinx-version", ""]
132132
"switcher": {
133133
"json_url": json_url,
134-
"url_template": "https://pydata-sphinx-theme.readthedocs.io/en/{version}/",
135134
"version_match": version_match,
136135
},
137136
}

docs/user_guide/configuring.rst

+8-32
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,6 @@ The switcher requires the following configuration steps:
361361
``html_theme_options`` dict in ``conf.py``. ``switcher`` should have 3 keys:
362362

363363
- ``json_url``: the persistent location of the JSON file described above.
364-
- ``url_template``: a template string used to generate the correct URLs for
365-
the different documentation versions.
366364
- ``version_match``: a string stating the version of the documentation that
367365
is currently being browsed.
368366

@@ -380,14 +378,11 @@ First, write a JSON file stating which versions of your docs will be listed in
380378
the switcher's dropdown menu. That file should contain a list of entries that
381379
each can have the following fields:
382380

383-
- ``version``: a version string. This will be inserted into
384-
``switcher['url_template']`` to create the links to other docs versions, and
385-
also checked against ``switcher['version_match']`` to provide styling to the
386-
switcher.
381+
- ``version``: a version string. This is checked against
382+
``switcher['version_match']`` to provide styling to the switcher.
383+
- ``url``: the URL for this version.
387384
- ``name``: an optional name to display in the switcher dropdown instead of the
388385
version string (e.g., "latest", "stable", "dev", etc).
389-
- ``url``: an optional URL. If provided, it links the version to ``url``
390-
instead of ``switcher['url_template']``.
391386

392387
Here is an example JSON file:
393388

@@ -396,13 +391,16 @@ Here is an example JSON file:
396391
[
397392
{
398393
"name": "v2.1 (stable)",
399-
"version": "2.1"
394+
"version": "2.1",
395+
"url": "https://mysite.org/en/2.1/index.html"
400396
},
401397
{
402398
"version": "2.1rc1",
399+
"url": "https://mysite.org/en/2.1rc1/index.html"
403400
},
404401
{
405-
"version": "2.0"
402+
"version": "2.0",
403+
"url": "https://mysite.org/en/2.0/index.html"
406404
},
407405
{
408406
"version": "1.0",
@@ -461,28 +459,6 @@ a few different ways:
461459
}
462460
463461
464-
Configure ``switcher['url_template']``
465-
--------------------------------------
466-
467-
The switcher's links to other versions of your docs are made by combining the
468-
*version strings* from the JSON file with a *template string* you provide in
469-
``switcher['url_template']``. The template string must contain a placeholder
470-
``{version}`` and otherwise be a fully-resolved URL. For example:
471-
472-
.. code:: python
473-
474-
html_theme_options = {
475-
...,
476-
"switcher": {
477-
"url_template": "https://mysite.org/en/version-{version}/",
478-
}
479-
}
480-
481-
The example above will result in a link to
482-
``https://mysite.org/en/version-1.0/`` for the JSON entry for version
483-
``"1.0"``.
484-
485-
486462
Configure ``switcher['version_match']``
487463
---------------------------------------
488464

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/_templates/version-switcher.html

-12
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
-->
1515

1616
<script type="text/javascript">
17-
// Construct the target URL from the JSON components
18-
function buildURL(entry) {
19-
var template = "{{ theme_switcher.get('url_template') }}"; // supplied by jinja
20-
template = template.replace("{version}", entry.version);
21-
return template;
22-
}
23-
2417
// Check if corresponding page path exists in other version of docs
2518
// and, if so, go there instead of the homepage of the other docs version
2619
function checkPageExistsAndRedirect(event) {
@@ -56,11 +49,6 @@
5649
const node = document.createElement("a");
5750
node.setAttribute("class", "list-group-item list-group-item-action py-1");
5851
node.textContent = `${entry.name}`;
59-
// get the base URL for that doc version, add the current page's
60-
// path to it, and set as `href`
61-
if (!("url" in entry)) {
62-
entry.url = buildURL(entry);
63-
}
6452
node.setAttribute("href", `${entry.url}${currentFilePath}`);
6553
// on click, AJAX calls will check if the linked page exists before
6654
// trying to redirect, and if not, will redirect to the homepage

0 commit comments

Comments
 (0)