Debian Codegraph @

docs/user_guide/version-dropdown.rst

raw
1 .. _version-dropdowns:
2
3 Version switcher dropdowns
4 ==========================
5
6 You can add a button to your site that allows users to
7 switch between versions of your documentation. The links in the version
8 switcher will differ depending on which page of the docs is being viewed. For
9 example, on the page ``https://mysite.org/en/v2.0/changelog.html``, the
10 switcher links will go to ``changelog.html`` in the other versions of your
11 docs. When clicked, the switcher will check for the existence of that page, and
12 if it doesn't exist, will redirect to the homepage instead (in the requested version of the docs).
13
14 The switcher requires the following configuration steps:
15
16 1. Add a JSON file containing a list of the documentation versions that the
17 switcher should show on each page.
18
19 2. Add a configuration dictionary called ``switcher`` to the
20 ``html_theme_options`` dict in ``conf.py``. ``switcher`` should have 2 keys:
21
22 - ``json_url``: the persistent location of the JSON file described above.
23 - ``version_match``: a string stating the version of the documentation that
24 is currently being browsed.
25
26 3. Specify where to place the switcher in your page layout. For example, add
27 the ``"version-switcher"`` template to one of the layout lists in
28 ``html_theme_options`` (e.g., ``navbar_end``, ``footer_start``, etc.).
29
30 Below is a more in-depth description of each of these configuration steps.
31
32 .. warning::
33 Modern web browsers do not allow loading data when the request is made from a page loaded via the ``file:`` protocol. This means that if you build your documentation locally and then directly open one of its pages in the browser, **the version switcher will not be allowed to load its JSON data source** and you'll end up with an empty switcher menu. Possible work-arounds are:
34
35 1. View the locally-built files through a local webserver (Python provides a builtin module for this: https://docs.python.org/3/library/http.server.html).
36 2. Disabling your browser's security settings (either by passing a command-line flag when launching the browser, or through a browser add-on).
37
38
39 Add a JSON file to define your switcher's versions
40 --------------------------------------------------
41
42 First, write a JSON file stating which versions of your docs will be listed in
43 the switcher's dropdown menu. That file should contain a list of entries that
44 each can have the following fields:
45
46 - ``version``: a version string. This is checked against
47 ``switcher['version_match']`` to provide styling to the switcher.
48 - ``url``: the URL for this version.
49 - ``name``: an optional name to display in the switcher dropdown instead of the
50 version string (e.g., "latest", "stable", "dev", etc.).
51 - ``preferred``: an optional field that *should occur on at most one entry* in the JSON file.
52 It specifies which version is considered "latest stable", and is used to customize the message used on :ref:`version-warning-banners` (if they are enabled).
53
54 Here is an example JSON file:
55
56 .. code:: json
57
58 [
59 {
60 "name": "v2.1 (stable)",
61 "version": "2.1",
62 "url": "https://mysite.org/en/2.1/"
63 },
64 {
65 "version": "2.1rc1",
66 "url": "https://mysite.org/en/2.1rc1/"
67 },
68 {
69 "version": "2.0",
70 "url": "https://mysite.org/en/2.0/"
71 },
72 {
73 "version": "1.0",
74 "url": "https://mysite.org/en/1.0/"
75 }
76 ]
77
78 See the discussion of ``switcher['json_url']`` (below) for options of where to
79 save the JSON file.
80
81
82 Configure ``switcher['json_url']``
83 ----------------------------------
84
85 *The JSON file needs to be at a stable, persistent, fully-resolved URL* (i.e.,
86 not specified as a path relative to the sphinx root of the current doc build).
87 Each version of your documentation should point to the same URL, so that as new
88 versions are added to the JSON file all the older versions of the docs will
89 gain switcher dropdown entries linking to the new versions. This could be done
90 in a few different ways:
91
92 - The location could be one that is always associated with the most recent
93 documentation build (i.e., if your docs server aliases "latest" to the most
94 recent version, it could point to a location in the build tree of version
95 "latest"). For example:
96
97 .. code:: python
98
99 html_theme_options = {
100 ...,
101 "switcher": {
102 "json_url": "https://mysite.org/en/latest/_static/switcher.json",
103 }
104 }
105
106 In this case, the JSON is versioned alongside the rest of the docs pages but
107 only the most recent version is ever loaded (even by older versions of the
108 docs).
109
110 - The JSON could be stored outside the doc build trees. This probably means it
111 would be outside the software repo, and would require you to add new version
112 entries to the JSON file manually as part of your release process. Example:
113
114 .. code:: python
115
116 html_theme_options = {
117 ...,
118 "switcher": {
119 "json_url": "https://mysite.org/switcher.json",
120 }
121 }
122
123 - In theory the JSON could be saved in a folder that is listed under your site's
124 ``html_static_path`` configuration, **but this is not recommended**. If you want to
125 do it this way, see `the Sphinx static path documentation
126 <https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_static_path>`_
127 for more information but do so knowing that we do not support this use case.
128
129 By default, the theme is testing the :code:`.json` file provided and outputs warnings in the Sphinx build. If this test breaks the pipeline of your docs, the test can be disabled by configuring the :code:`check_switcher` parameter in :code:`conf.py`:
130
131 .. code-block:: python
132
133 html_theme_options = {
134 # ...
135 "check_switcher": False
136 }
137
138 Configure ``switcher['version_match']``
139 ---------------------------------------
140
141 This configuration value tells the switcher what docs version is currently
142 being viewed, and is used to style the switcher (i.e., to highlight the current
143 docs version in the switcher's dropdown menu, and to change the text displayed
144 on the switcher button).
145
146 Typically, you can re-use one of the sphinx variables ``version``
147 or ``release`` as the value of ``switcher['version_match']``; which one you use
148 depends on how granular your docs versioning is. See
149 `the Sphinx "project info" documentation
150 <https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information>`__
151 for more information). Example:
152
153 .. code:: python
154
155 version = my_package_name.__version__.replace("dev0", "") # may differ
156 html_theme_options = {
157 ...,
158 "switcher": {
159 "version_match": version,
160 }
161 }
162
163
164 Specify where to display the switcher
165 -------------------------------------
166
167 Finally, tell the theme where on your site's pages you want the switcher to
168 appear. There are many choices here: you can add ``"version-switcher"`` to one
169 of the locations in ``html_theme_options`` (e.g., ``navbar_end``,
170 ``footer_start``, etc). For example:
171
172 .. code:: python
173
174 html_theme_options = {
175 ...,
176 "navbar_start": ["navbar-logo", "version-switcher"]
177 }
178
179
180 Alternatively, you could override one of the other templates to include the
181 version switcher in a sidebar. For example, you could define
182 ``_templates/sidebar-nav-bs.html`` as:
183
184 .. code:: jinja
185
186 {%- include 'version-switcher.html' -%}
187 {{ super() }}
188
189 to insert a version switcher at the top of the primary sidebar, while still
190 keeping the default navigation below it. See :doc:`layout` for more
191 information.
192
193 Style the switcher buttons
194 --------------------------
195
196 You may apply styles via CSS to any of the switcher buttons to control their look and feel.
197 Each button has two `HTML dataset entries <https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset>`_
198 that you can use to apply CSS rules to subsets of buttons. These entries are:
199
200 .. code-block::
201
202 data-version
203 data-version-name
204
205 For example, the link for an entry with ``version="0.2"``,
206 and ``name="My version"`` would have metadata like so:
207
208 .. code-block:: html
209
210 <a data-version-name="My version" data-version="0.2" class="<classes...>">
211
212 You can create CSS rules that select this metadata like so:
213
214 .. code-block:: scss
215
216 // Style all links with a specific subset of versions
217 .version-switcher__container a[data-version="0.2"],
218 .version-switcher__container a[data-version="0.3"] {
219 background-color: red;
220 }
221 // Style all links with `stable` in the version name
222 .version-switcher__container a[data-version-name*="stable"] {
223 background-color: green;
224 }
225
226 In addition, the parent button of the dropdown list contains similar metadata
227 about the **current version**. This could be used to style the entire dropdown
228 a certain color based on the active version.
229
230 For example, if you wanted to style the dropdown button to use the theme's secondary color (PyData orange by default) if it was a ``dev``
231 version, you could use the following CSS selector:
232
233 .. code-block:: scss
234
235 // If the active version has the name "dev", style it orange
236 .version-switcher__button[data-active-version-name*="dev"] {
237 background-color: var(--pst-color-secondary);
238 }
239
240 .. seealso::
241
242 See the `MDN documentation on dataset properties <https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset>`_
243 for more information on using and styling these properties.