mkdocs-toggle-sidebar-plugin 0.0.5__tar.gz → 0.0.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (19) hide show
  1. {mkdocs_toggle_sidebar_plugin-0.0.5/src/mkdocs_toggle_sidebar_plugin.egg-info → mkdocs_toggle_sidebar_plugin-0.0.6}/PKG-INFO +76 -11
  2. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/README.md +70 -8
  3. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/setup.cfg +4 -2
  4. mkdocs_toggle_sidebar_plugin-0.0.6/src/mkdocs_toggle_sidebar_plugin/__init__.py +135 -0
  5. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/src/mkdocs_toggle_sidebar_plugin/toggle-sidebar.js +17 -4
  6. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6/src/mkdocs_toggle_sidebar_plugin.egg-info}/PKG-INFO +76 -11
  7. mkdocs_toggle_sidebar_plugin-0.0.6/src/mkdocs_toggle_sidebar_plugin.egg-info/requires.txt +1 -0
  8. mkdocs_toggle_sidebar_plugin-0.0.5/src/mkdocs_toggle_sidebar_plugin/__init__.py +0 -73
  9. mkdocs_toggle_sidebar_plugin-0.0.5/src/mkdocs_toggle_sidebar_plugin.egg-info/requires.txt +0 -1
  10. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/LICENSE +0 -0
  11. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/MANIFEST.in +0 -0
  12. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/pyproject.toml +0 -0
  13. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/src/mkdocs_toggle_sidebar_plugin/material.js +0 -0
  14. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/src/mkdocs_toggle_sidebar_plugin/mkdocs.js +0 -0
  15. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/src/mkdocs_toggle_sidebar_plugin/readthedocs.js +0 -0
  16. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/src/mkdocs_toggle_sidebar_plugin.egg-info/SOURCES.txt +0 -0
  17. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/src/mkdocs_toggle_sidebar_plugin.egg-info/dependency_links.txt +0 -0
  18. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/src/mkdocs_toggle_sidebar_plugin.egg-info/entry_points.txt +0 -0
  19. {mkdocs_toggle_sidebar_plugin-0.0.5 → mkdocs_toggle_sidebar_plugin-0.0.6}/src/mkdocs_toggle_sidebar_plugin.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: mkdocs-toggle-sidebar-plugin
3
- Version: 0.0.5
3
+ Version: 0.0.6
4
4
  Summary: Add keybindings to toggle the navigation and table of contents sidebars
5
5
  Home-page: https://github.com/six-two/mkdocs-toggle-sidebar-plugin
6
6
  Author: six-two
@@ -12,10 +12,13 @@ Classifier: Programming Language :: Python :: 3
12
12
  Classifier: Programming Language :: Python :: 3.9
13
13
  Classifier: Programming Language :: Python :: 3.10
14
14
  Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
15
17
  Requires-Python: >=3.9
16
18
  Description-Content-Type: text/markdown
17
19
  License-File: LICENSE
18
- Requires-Dist: mkdocs>=1.4.0
20
+ Requires-Dist: mkdocs>=1.5.0
21
+ Dynamic: license-file
19
22
 
20
23
  # mkdocs-toggle-sidebar-plugin
21
24
 
@@ -64,7 +67,41 @@ Key | Action
64
67
  For some themes like `readthedocs` navigation and TOC are combined.
65
68
  In this case the state of TOC is ignored, and only calls for navigation (or all) are interpreted.
66
69
 
67
- ### Toggle button
70
+ ### Configuration options
71
+
72
+ You can overwrite the defaults like this:
73
+
74
+ ```yaml
75
+ plugins:
76
+ - search
77
+ - toggle-sidebar:
78
+ async: False
79
+ debug: True
80
+ enabled: True
81
+ inline: False
82
+ javascript: ./toggle-sidebar.js
83
+ show_navigation_by_default: False
84
+ show_toc_by_default: False
85
+ theme: material
86
+ toggle_button: all
87
+ ```
88
+
89
+ The following options exist:
90
+
91
+ Option | Type | Default value | Description
92
+ --- | ---| --- | ---
93
+ async | `bool` | `False` | Asynchronously load the JavaScript file created by the plugin
94
+ debug | `bool` | `False` | Show some debug messages during mkdocs build (for example related to theme detection)
95
+ enabled | `bool` | `True` | Can be used to disable the plugin. Usually used in combination with environment variables like `enabled: !ENV [TOGGLE_SIDEBAR, false]` as described in [mkdocs's docs](https://www.mkdocs.org/user-guide/configuration/#enabled-option)
96
+ inline | `bool` | `True` | Instead of storing the javascript code in the file specified by `javascript`, it is directly copied into each page. Slightly increases page size, but can improve load times a little bit and reduce flickering on page (re-)load
97
+ javascript | `str` | `"assets/javascripts/toggle-sidebar.js"` | The path where to store the output file
98
+ show_navigation_by_default | `bool` | `True` | Whether to show the navigation by default
99
+ show_toc_by_default | `bool` | `True` | Whether to show the table of contents by default
100
+ theme | `str` | `auto` | Used for theme detection. With `auto`, the plugin tries to automatically detect the theme. But you can also force it to use a specific theme preset that you know will work. Currently supported values: `material`/`ansible`, `mkdocs`, `readthedocs`.
101
+ toggle_button | `str` | `"none"` | Can be set to show a toggle button (see below)
102
+
103
+
104
+ #### Toggle button
68
105
 
69
106
  When you set the `toggle_button` option to `navigation`, `toc` or `all`, it will add a button that looks like a hamburger menu (three horizontal bars) on a theme-dependent location.
70
107
  It is usually in the nav or the top bar.
@@ -90,30 +127,58 @@ The names and parameters should be self-explanatory.
90
127
  ## Theme support
91
128
 
92
129
  Below shows the latest themes that I have tested.
93
- They are not updated that often, and the plugin should generally work for other of theme versions too.
130
+ The table is not updated regularly, but the plugin should generally work for other theme versions too.
94
131
 
95
132
  Theme | Theme version | Plugin version | Status
96
133
  --- | --- | --- | ---
97
- mkdocs-material | 9.5.34 | 0.0.4 | works
98
- mkdocs (default) | 1.6.1 | 0.0.4 | works
99
- readthedocs | 1.6.1 | 0.0.4 | works
134
+ mkdocs-ansible | 25.6.0 | 0.0.6 | works
135
+ mkdocs-material | 9.6.14 | 0.0.4+ | works
136
+ mkdocs (default) | 1.6.1 | 0.0.4+ | works
137
+ readthedocs | 1.6.1 | 0.0.4+ | works
100
138
 
101
- Just open a issue / PR if you use a strange theme or the info above is not up to date anymore.
139
+ Just open an issue / PR if you use a strange theme or the info above is not up-to-date anymore.
102
140
 
103
141
  ### Note to self
104
142
 
105
- Test mkdocs-material theme:
143
+ Test `material` theme:
106
144
  ```bash
107
145
  ./serve.sh
108
146
  ```
109
147
 
110
- Test mkdocs theme:
148
+ Test `mkdocs` theme:
111
149
  ```bash
112
150
  ./serve.sh --theme mkdocs
113
151
  ```
114
152
 
153
+ Test `mkdocs`, `readthedocs` and `material` themes:
154
+ ```bash
155
+ ./build.sh
156
+ python3 -m http.server --directory './public/'
157
+ ```
158
+
159
+ Test oldest python version supported by me (3.9):
160
+ ```bash
161
+ docker run --rm -it -v "$PWD:/share" -w "/share" -p 8000:8000 --entrypoint=bash python:3.9 ./serve.sh
162
+ ```
163
+
164
+ Test newest available python version (currently 3.13):
165
+ ```bash
166
+ docker run --rm -it -v "$PWD:/share" -w "/share" -p 8000:8000 --entrypoint=bash python:latest ./serve.sh
167
+ ```
168
+
169
+
115
170
  ## Notable changes
116
171
 
172
+ ### Version 0.0.6
173
+
174
+ - Fixed toggle button appearing delayed on slow loading pages (see #6)
175
+ - Fixed behavior when using Material's `navigation.instant` feature (see #5)
176
+ - Added `inline` option that prevents page flickering on reload (see #4). It is now enabled by default and async is disabled by default, to prevent the flickering. To revert to the old behavior you can set `async: True` and `inline: False` in the plugin's config in your `mkdocs.yml`
177
+ - Added `theme` option that allows you to override theme detection (see #3)
178
+ - Added support for `ansible` theme (see #3)
179
+ - Added fallback to check `theme.extra.base_theme` from `mkdocs.yml` when other theme detection logic fails (see #3)
180
+ - Added `debug` option
181
+
117
182
  ### Version 0.0.5
118
183
 
119
184
  - Bug fix: On small screens with the material theme the navigation would be hidden, even when the hamburger menu was opened.
@@ -45,7 +45,41 @@ Key | Action
45
45
  For some themes like `readthedocs` navigation and TOC are combined.
46
46
  In this case the state of TOC is ignored, and only calls for navigation (or all) are interpreted.
47
47
 
48
- ### Toggle button
48
+ ### Configuration options
49
+
50
+ You can overwrite the defaults like this:
51
+
52
+ ```yaml
53
+ plugins:
54
+ - search
55
+ - toggle-sidebar:
56
+ async: False
57
+ debug: True
58
+ enabled: True
59
+ inline: False
60
+ javascript: ./toggle-sidebar.js
61
+ show_navigation_by_default: False
62
+ show_toc_by_default: False
63
+ theme: material
64
+ toggle_button: all
65
+ ```
66
+
67
+ The following options exist:
68
+
69
+ Option | Type | Default value | Description
70
+ --- | ---| --- | ---
71
+ async | `bool` | `False` | Asynchronously load the JavaScript file created by the plugin
72
+ debug | `bool` | `False` | Show some debug messages during mkdocs build (for example related to theme detection)
73
+ enabled | `bool` | `True` | Can be used to disable the plugin. Usually used in combination with environment variables like `enabled: !ENV [TOGGLE_SIDEBAR, false]` as described in [mkdocs's docs](https://www.mkdocs.org/user-guide/configuration/#enabled-option)
74
+ inline | `bool` | `True` | Instead of storing the javascript code in the file specified by `javascript`, it is directly copied into each page. Slightly increases page size, but can improve load times a little bit and reduce flickering on page (re-)load
75
+ javascript | `str` | `"assets/javascripts/toggle-sidebar.js"` | The path where to store the output file
76
+ show_navigation_by_default | `bool` | `True` | Whether to show the navigation by default
77
+ show_toc_by_default | `bool` | `True` | Whether to show the table of contents by default
78
+ theme | `str` | `auto` | Used for theme detection. With `auto`, the plugin tries to automatically detect the theme. But you can also force it to use a specific theme preset that you know will work. Currently supported values: `material`/`ansible`, `mkdocs`, `readthedocs`.
79
+ toggle_button | `str` | `"none"` | Can be set to show a toggle button (see below)
80
+
81
+
82
+ #### Toggle button
49
83
 
50
84
  When you set the `toggle_button` option to `navigation`, `toc` or `all`, it will add a button that looks like a hamburger menu (three horizontal bars) on a theme-dependent location.
51
85
  It is usually in the nav or the top bar.
@@ -71,30 +105,58 @@ The names and parameters should be self-explanatory.
71
105
  ## Theme support
72
106
 
73
107
  Below shows the latest themes that I have tested.
74
- They are not updated that often, and the plugin should generally work for other of theme versions too.
108
+ The table is not updated regularly, but the plugin should generally work for other theme versions too.
75
109
 
76
110
  Theme | Theme version | Plugin version | Status
77
111
  --- | --- | --- | ---
78
- mkdocs-material | 9.5.34 | 0.0.4 | works
79
- mkdocs (default) | 1.6.1 | 0.0.4 | works
80
- readthedocs | 1.6.1 | 0.0.4 | works
112
+ mkdocs-ansible | 25.6.0 | 0.0.6 | works
113
+ mkdocs-material | 9.6.14 | 0.0.4+ | works
114
+ mkdocs (default) | 1.6.1 | 0.0.4+ | works
115
+ readthedocs | 1.6.1 | 0.0.4+ | works
81
116
 
82
- Just open a issue / PR if you use a strange theme or the info above is not up to date anymore.
117
+ Just open an issue / PR if you use a strange theme or the info above is not up-to-date anymore.
83
118
 
84
119
  ### Note to self
85
120
 
86
- Test mkdocs-material theme:
121
+ Test `material` theme:
87
122
  ```bash
88
123
  ./serve.sh
89
124
  ```
90
125
 
91
- Test mkdocs theme:
126
+ Test `mkdocs` theme:
92
127
  ```bash
93
128
  ./serve.sh --theme mkdocs
94
129
  ```
95
130
 
131
+ Test `mkdocs`, `readthedocs` and `material` themes:
132
+ ```bash
133
+ ./build.sh
134
+ python3 -m http.server --directory './public/'
135
+ ```
136
+
137
+ Test oldest python version supported by me (3.9):
138
+ ```bash
139
+ docker run --rm -it -v "$PWD:/share" -w "/share" -p 8000:8000 --entrypoint=bash python:3.9 ./serve.sh
140
+ ```
141
+
142
+ Test newest available python version (currently 3.13):
143
+ ```bash
144
+ docker run --rm -it -v "$PWD:/share" -w "/share" -p 8000:8000 --entrypoint=bash python:latest ./serve.sh
145
+ ```
146
+
147
+
96
148
  ## Notable changes
97
149
 
150
+ ### Version 0.0.6
151
+
152
+ - Fixed toggle button appearing delayed on slow loading pages (see #6)
153
+ - Fixed behavior when using Material's `navigation.instant` feature (see #5)
154
+ - Added `inline` option that prevents page flickering on reload (see #4). It is now enabled by default and async is disabled by default, to prevent the flickering. To revert to the old behavior you can set `async: True` and `inline: False` in the plugin's config in your `mkdocs.yml`
155
+ - Added `theme` option that allows you to override theme detection (see #3)
156
+ - Added support for `ansible` theme (see #3)
157
+ - Added fallback to check `theme.extra.base_theme` from `mkdocs.yml` when other theme detection logic fails (see #3)
158
+ - Added `debug` option
159
+
98
160
  ### Version 0.0.5
99
161
 
100
162
  - Bug fix: On small screens with the material theme the navigation would be hidden, even when the hamburger menu was opened.
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = mkdocs-toggle-sidebar-plugin
3
- version = 0.0.5
3
+ version = 0.0.6
4
4
  author = six-two
5
5
  author_email = pip@six-two.dev
6
6
  description = Add keybindings to toggle the navigation and table of contents sidebars
@@ -15,6 +15,8 @@ classifiers =
15
15
  Programming Language :: Python :: 3.9
16
16
  Programming Language :: Python :: 3.10
17
17
  Programming Language :: Python :: 3.11
18
+ Programming Language :: Python :: 3.12
19
+ Programming Language :: Python :: 3.13
18
20
 
19
21
  [options]
20
22
  include_package_data = True
@@ -24,7 +26,7 @@ packages = find:
24
26
  python_requires = >=3.9
25
27
  scripts =
26
28
  install_requires =
27
- mkdocs>=1.4.0
29
+ mkdocs>=1.5.0
28
30
 
29
31
  [options.entry_points]
30
32
  mkdocs.plugins =
@@ -0,0 +1,135 @@
1
+ import os
2
+ # pip dependency
3
+ from mkdocs.plugins import BasePlugin, get_plugin_logger
4
+ from mkdocs.config.defaults import MkDocsConfig
5
+ from mkdocs.config.base import Config
6
+ from mkdocs.config.config_options import Type, ExtraScriptValue
7
+ from mkdocs.exceptions import PluginError
8
+
9
+ LOGGER = get_plugin_logger(__name__)
10
+ SCRIPT_DIR = os.path.dirname(__file__)
11
+ ALLOWED_TOGGLE_BUTTON_VALUES = ["none", "navigation", "toc", "all"]
12
+ # This is a map of compatible themes. For example 'ansible' inherits from 'material', so the material javascript will work for the ansible theme too
13
+ THEME_COMPATIBILITY = {
14
+ "ansible": "material",
15
+ }
16
+ # May not always be accurate, this is just for a more helpful error message
17
+ KNOWN_THEME_NAMES = ["material", "mkdocs", "readthedocs"] + list(THEME_COMPATIBILITY.keys())
18
+
19
+ class PluginConfig(Config):
20
+ enabled = Type(bool, default=True)
21
+ show_toc_by_default = Type(bool, default=True)
22
+ show_navigation_by_default = Type(bool, default=True)
23
+ theme = Type(str, default="auto")
24
+ toggle_button = Type(str, default="none")
25
+ async_ = Type(bool, default=False)
26
+ javascript = Type(str, default="assets/javascripts/toggle-sidebar.js")
27
+ debug = Type(bool, default=False)
28
+ inline = Type(bool, default=True)
29
+
30
+
31
+ def is_known_theme(theme_name: str) -> bool:
32
+ # If the theme is known to be based on another theme, then we resolve it to the base theme
33
+ theme_name = THEME_COMPATIBILITY.get(theme_name, theme_name)
34
+
35
+ # Then we check if we have a javascript file for the theme
36
+ theme_path = os.path.join(SCRIPT_DIR, f"{theme_name}.js")
37
+ return os.path.exists(theme_path) and theme_name != "toggle-sidebar"
38
+
39
+ def get_unknown_theme_message(theme_name: str, auto_detect_enabled: bool) -> str:
40
+ if auto_detect_enabled:
41
+ basic_help = "If this theme is based on or similar to one of the supported themes above, add 'theme: <NAME_OF_SIMILAR_SUPPORTED_THEME>' to this plugin's configuration in your mkdocs.yml"
42
+ else:
43
+ basic_help = "You are overwriting the theme in this plugin's configuration in your mkdocs.yml. Make sure you spelled the theme's name correctly."
44
+ return f"Theme '{theme_name}' is not (yet) supported. The currently supported themes are: {', '.join(KNOWN_THEME_NAMES)}.\nRecommended steps:\n1. {basic_help}\n2. Try updating this plugin to the latest version: pip install -U mkdocs-toggle-sidebar-plugin\n3. Check if an issue for this theme exists: https://github.com/six-two/mkdocs-toggle-sidebar-plugin/issues\n4. If no issue exists feel free to open one. Please put the theme name and path where to download it in the issue"
45
+
46
+
47
+ class Plugin(BasePlugin[PluginConfig]):
48
+ def on_config(self, config: MkDocsConfig, **kwargs) -> MkDocsConfig:
49
+ """
50
+ Called once when the config is loaded.
51
+ It will make modify the config and initialize this plugin.
52
+ """
53
+ self.theme_function_definitions = None
54
+ self.inline_javascript = None
55
+ if self.config.enabled:
56
+ theme_name = self.config.theme
57
+ # Default to automatically determining the theme
58
+ if theme_name == "auto" or not theme_name:
59
+ theme_name = config.theme.name or "mkdocs"
60
+ base_theme_name = config.theme.get("extra", {}).get("base_theme") # check mkdocs.yml:theme.extra.base_theme
61
+ self.debug(f"Automatically detected theme: {theme_name}. Optional base theme: {base_theme_name}")
62
+
63
+ if not is_known_theme(theme_name):
64
+ # Handle themes
65
+ if base_theme_name and is_known_theme(base_theme_name):
66
+ LOGGER.debug(f"Theme is unknown, so we fell back to base theme: {base_theme_name}")
67
+ theme_name = base_theme_name
68
+ else:
69
+ LOGGER.warning(get_unknown_theme_message(theme_name, True))
70
+ else:
71
+ if not is_known_theme(theme_name):
72
+ LOGGER.warning(get_unknown_theme_message(theme_name, False))
73
+
74
+ # If the theme is known to be based on another theme, then we resolve it to the base theme
75
+ resolved_theme_name = THEME_COMPATIBILITY.get(theme_name, theme_name)
76
+
77
+ theme_path = os.path.join(SCRIPT_DIR, f"{resolved_theme_name}.js")
78
+ if os.path.exists(theme_path):
79
+ self.debug(f"Using JavaScript {resolved_theme_name}.js for theme {theme_name}")
80
+ with open(theme_path) as f:
81
+ self.theme_function_definitions = f.read()
82
+
83
+ if self.theme_function_definitions:
84
+ if self.config.inline:
85
+ # We cache it for performance reasons
86
+ self.inline_javascript = f"<script>{self.get_toggle_sidebar_javascript()}</script>"
87
+ else:
88
+ # Add a custom script reference
89
+ custom_script = ExtraScriptValue(self.config.javascript)
90
+ if self.config.async_:
91
+ custom_script.async_ = True
92
+
93
+ config.extra_javascript.append(custom_script)
94
+
95
+ if self.config.toggle_button not in ALLOWED_TOGGLE_BUTTON_VALUES:
96
+ raise PluginError(f"Unexpected value for 'toggle_button': '{self.config.toggle_button}'. Allowed values are {', '.join(ALLOWED_TOGGLE_BUTTON_VALUES)}")
97
+ return config
98
+
99
+ def debug(self, message: str) -> None:
100
+ if self.config.debug:
101
+ LOGGER.info(message)
102
+ else:
103
+ LOGGER.debug(message)
104
+
105
+ def on_post_page(self, html, /, *, page, config):
106
+ if self.inline_javascript:
107
+ html = html.replace("</head>", self.inline_javascript + "</head>")
108
+ return html
109
+
110
+ def on_post_build(self, config: MkDocsConfig) -> None:
111
+ if self.theme_function_definitions and not self.config.inline:
112
+ target_path = os.path.join(config.site_dir, self.config.javascript)
113
+ if os.path.exists(target_path):
114
+ # The file exists. This probably means, that the user wanted to override the default file
115
+ # So we just do nothing
116
+ pass
117
+ else:
118
+ # Make sure that the folder exists
119
+ parent_dir = os.path.dirname(target_path)
120
+ os.makedirs(parent_dir, exist_ok=True)
121
+
122
+ # Copy the file, while also editing it on the fly
123
+ javascript = self.get_toggle_sidebar_javascript()
124
+ with open(target_path, "w") as f:
125
+ f.write(javascript)
126
+
127
+ def get_toggle_sidebar_javascript(self):
128
+ asset_path = os.path.join(SCRIPT_DIR, "toggle-sidebar.js")
129
+ with open(asset_path) as f:
130
+ data = f.read()
131
+ data = data.replace("THEME_DEPENDENT_FUNCTION_DEFINITION_PLACEHOLDER", self.theme_function_definitions)
132
+ data = data.replace("TOC_DEFAULT_PLACEHOLDER", "true" if self.config.show_toc_by_default else "false")
133
+ data = data.replace("NAVIGATION_DEFAULT_PLACEHOLDER", "true" if self.config.show_toc_by_default else "false")
134
+ data = data.replace("TOGGLE_BUTTON_PLACEHOLDER", self.config.toggle_button)
135
+ return data
@@ -1,6 +1,7 @@
1
1
  (function() {
2
2
  const customDynamicStyle = document.createElement("style");
3
- document.head.appendChild(customDynamicStyle);
3
+ // We must put it outside of the head and body, since they are replaced by the instant navigation feature in the Material theme
4
+ document.documentElement.appendChild(customDynamicStyle);
4
5
 
5
6
  const TOGGLE_BUTTON_REFERENCE_ELEMENT_NOT_FOUND_WARNING = "[mkdocs-toggle-sidebar-plugin] Reference element for inserting 'toggle_button' not found. This version of the plugin may not be compatible with this version of the theme. Try updating both to the latest version. If that fails, you can open an GitHub issue.";
6
7
 
@@ -65,7 +66,7 @@
65
66
  }
66
67
  }
67
68
 
68
- window.addEventListener("load", () => {
69
+ const onPageLoadedAction = () => {
69
70
  console.log("The mkdocs-toggle-sidebar-plugin is installed. It adds the following key bindings:\n T -> toggle table of contents sidebar\n M -> toggle navigation menu sidebar\n B -> toggle both sidebars (TOC and navigation)");
70
71
 
71
72
  const toggle_button = "TOGGLE_BUTTON_PLACEHOLDER";
@@ -82,8 +83,7 @@
82
83
  }
83
84
 
84
85
  registerKeyboardEventHandler();
85
- customDynamicStyle.innerHTML = setCombinedVisibility(loadNavigationState(), loadTocState());
86
- });
86
+ }
87
87
 
88
88
  const createDefaultToggleButton = (toggleNavigation, toggleTOC) => {
89
89
  const toggleBtn = document.createElement("div");
@@ -119,4 +119,17 @@
119
119
  toggleTocVisibility: () => toggleVisibility(false, true),
120
120
  toggleAllVisibility: () => toggleVisibility(true, true)
121
121
  };
122
+
123
+ // Run this immediately instead of waiting for page.onload to prevent page flicker
124
+ customDynamicStyle.innerHTML = setCombinedVisibility(loadNavigationState(), loadTocState());
125
+ // console.log("Debug: hide sidebar completed");
126
+
127
+ // SEE https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event#checking_whether_loading_is_already_complete
128
+ if (document.readyState === "loading") {
129
+ // console.debug("Registering DOMContentLoaded event listener");
130
+ document.addEventListener("DOMContentLoaded", onPageLoadedAction);
131
+ } else {
132
+ // console.debug("DOMContentLoaded has already fired");
133
+ onPageLoadedAction();
134
+ }
122
135
  }());
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: mkdocs-toggle-sidebar-plugin
3
- Version: 0.0.5
3
+ Version: 0.0.6
4
4
  Summary: Add keybindings to toggle the navigation and table of contents sidebars
5
5
  Home-page: https://github.com/six-two/mkdocs-toggle-sidebar-plugin
6
6
  Author: six-two
@@ -12,10 +12,13 @@ Classifier: Programming Language :: Python :: 3
12
12
  Classifier: Programming Language :: Python :: 3.9
13
13
  Classifier: Programming Language :: Python :: 3.10
14
14
  Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
15
17
  Requires-Python: >=3.9
16
18
  Description-Content-Type: text/markdown
17
19
  License-File: LICENSE
18
- Requires-Dist: mkdocs>=1.4.0
20
+ Requires-Dist: mkdocs>=1.5.0
21
+ Dynamic: license-file
19
22
 
20
23
  # mkdocs-toggle-sidebar-plugin
21
24
 
@@ -64,7 +67,41 @@ Key | Action
64
67
  For some themes like `readthedocs` navigation and TOC are combined.
65
68
  In this case the state of TOC is ignored, and only calls for navigation (or all) are interpreted.
66
69
 
67
- ### Toggle button
70
+ ### Configuration options
71
+
72
+ You can overwrite the defaults like this:
73
+
74
+ ```yaml
75
+ plugins:
76
+ - search
77
+ - toggle-sidebar:
78
+ async: False
79
+ debug: True
80
+ enabled: True
81
+ inline: False
82
+ javascript: ./toggle-sidebar.js
83
+ show_navigation_by_default: False
84
+ show_toc_by_default: False
85
+ theme: material
86
+ toggle_button: all
87
+ ```
88
+
89
+ The following options exist:
90
+
91
+ Option | Type | Default value | Description
92
+ --- | ---| --- | ---
93
+ async | `bool` | `False` | Asynchronously load the JavaScript file created by the plugin
94
+ debug | `bool` | `False` | Show some debug messages during mkdocs build (for example related to theme detection)
95
+ enabled | `bool` | `True` | Can be used to disable the plugin. Usually used in combination with environment variables like `enabled: !ENV [TOGGLE_SIDEBAR, false]` as described in [mkdocs's docs](https://www.mkdocs.org/user-guide/configuration/#enabled-option)
96
+ inline | `bool` | `True` | Instead of storing the javascript code in the file specified by `javascript`, it is directly copied into each page. Slightly increases page size, but can improve load times a little bit and reduce flickering on page (re-)load
97
+ javascript | `str` | `"assets/javascripts/toggle-sidebar.js"` | The path where to store the output file
98
+ show_navigation_by_default | `bool` | `True` | Whether to show the navigation by default
99
+ show_toc_by_default | `bool` | `True` | Whether to show the table of contents by default
100
+ theme | `str` | `auto` | Used for theme detection. With `auto`, the plugin tries to automatically detect the theme. But you can also force it to use a specific theme preset that you know will work. Currently supported values: `material`/`ansible`, `mkdocs`, `readthedocs`.
101
+ toggle_button | `str` | `"none"` | Can be set to show a toggle button (see below)
102
+
103
+
104
+ #### Toggle button
68
105
 
69
106
  When you set the `toggle_button` option to `navigation`, `toc` or `all`, it will add a button that looks like a hamburger menu (three horizontal bars) on a theme-dependent location.
70
107
  It is usually in the nav or the top bar.
@@ -90,30 +127,58 @@ The names and parameters should be self-explanatory.
90
127
  ## Theme support
91
128
 
92
129
  Below shows the latest themes that I have tested.
93
- They are not updated that often, and the plugin should generally work for other of theme versions too.
130
+ The table is not updated regularly, but the plugin should generally work for other theme versions too.
94
131
 
95
132
  Theme | Theme version | Plugin version | Status
96
133
  --- | --- | --- | ---
97
- mkdocs-material | 9.5.34 | 0.0.4 | works
98
- mkdocs (default) | 1.6.1 | 0.0.4 | works
99
- readthedocs | 1.6.1 | 0.0.4 | works
134
+ mkdocs-ansible | 25.6.0 | 0.0.6 | works
135
+ mkdocs-material | 9.6.14 | 0.0.4+ | works
136
+ mkdocs (default) | 1.6.1 | 0.0.4+ | works
137
+ readthedocs | 1.6.1 | 0.0.4+ | works
100
138
 
101
- Just open a issue / PR if you use a strange theme or the info above is not up to date anymore.
139
+ Just open an issue / PR if you use a strange theme or the info above is not up-to-date anymore.
102
140
 
103
141
  ### Note to self
104
142
 
105
- Test mkdocs-material theme:
143
+ Test `material` theme:
106
144
  ```bash
107
145
  ./serve.sh
108
146
  ```
109
147
 
110
- Test mkdocs theme:
148
+ Test `mkdocs` theme:
111
149
  ```bash
112
150
  ./serve.sh --theme mkdocs
113
151
  ```
114
152
 
153
+ Test `mkdocs`, `readthedocs` and `material` themes:
154
+ ```bash
155
+ ./build.sh
156
+ python3 -m http.server --directory './public/'
157
+ ```
158
+
159
+ Test oldest python version supported by me (3.9):
160
+ ```bash
161
+ docker run --rm -it -v "$PWD:/share" -w "/share" -p 8000:8000 --entrypoint=bash python:3.9 ./serve.sh
162
+ ```
163
+
164
+ Test newest available python version (currently 3.13):
165
+ ```bash
166
+ docker run --rm -it -v "$PWD:/share" -w "/share" -p 8000:8000 --entrypoint=bash python:latest ./serve.sh
167
+ ```
168
+
169
+
115
170
  ## Notable changes
116
171
 
172
+ ### Version 0.0.6
173
+
174
+ - Fixed toggle button appearing delayed on slow loading pages (see #6)
175
+ - Fixed behavior when using Material's `navigation.instant` feature (see #5)
176
+ - Added `inline` option that prevents page flickering on reload (see #4). It is now enabled by default and async is disabled by default, to prevent the flickering. To revert to the old behavior you can set `async: True` and `inline: False` in the plugin's config in your `mkdocs.yml`
177
+ - Added `theme` option that allows you to override theme detection (see #3)
178
+ - Added support for `ansible` theme (see #3)
179
+ - Added fallback to check `theme.extra.base_theme` from `mkdocs.yml` when other theme detection logic fails (see #3)
180
+ - Added `debug` option
181
+
117
182
  ### Version 0.0.5
118
183
 
119
184
  - Bug fix: On small screens with the material theme the navigation would be hidden, even when the hamburger menu was opened.
@@ -1,73 +0,0 @@
1
- import os
2
- import logging
3
- # pip dependency
4
- from mkdocs.plugins import BasePlugin, get_plugin_logger
5
- from mkdocs.config.defaults import MkDocsConfig
6
- from mkdocs.config.base import Config
7
- from mkdocs.config.config_options import Type, ExtraScriptValue
8
- from mkdocs.exceptions import PluginError
9
-
10
- LOGGER = get_plugin_logger(__name__)
11
- SCRIPT_DIR = os.path.dirname(__file__)
12
- ALLOWED_TOGGLE_BUTTON_VALUES = ["none", "navigation", "toc", "all"]
13
-
14
- class PluginConfig(Config):
15
- enabled = Type(bool, default=True)
16
- show_toc_by_default = Type(bool, default=True)
17
- show_navigation_by_default = Type(bool, default=True)
18
- toggle_button = Type(str, default="none")
19
- async_ = Type(bool, default=True)
20
- javascript = Type(str, default="assets/javascripts/toggle-sidebar.js")
21
-
22
-
23
- class Plugin(BasePlugin[PluginConfig]):
24
- def on_config(self, config: MkDocsConfig, **kwargs) -> MkDocsConfig:
25
- """
26
- Called once when the config is loaded.
27
- It will make modify the config and initialize this plugin.
28
- """
29
- self.theme_function_definitions = None
30
- if self.config.enabled:
31
- theme_name = config.theme.name or "mkdocs"
32
- theme_path = os.path.join(SCRIPT_DIR, f"{theme_name}.js")
33
- if os.path.exists(theme_path):
34
- with open(theme_path) as f:
35
- self.theme_function_definitions = f.read()
36
- else:
37
- LOGGER.warning(f"[toggle-sidebar] Theme '{theme_name}' is not (yet) supported. Hint:\n1. Try updating the plugin to the latest version: pip install -U mkdocs-toggle-sidebar-plugin\n2. Check if an issue for this theme exists: https://github.com/six-two/mkdocs-toggle-sidebar-plugin/issues\n3. If no issue exists feel free to open one. Please put the theme name and path where to download it in the issue")
38
-
39
- if self.theme_function_definitions:
40
- custom_script = ExtraScriptValue(self.config.javascript)
41
- if self.config.async_:
42
- custom_script.async_ = True
43
-
44
- config.extra_javascript.append(custom_script)
45
-
46
- if self.config.toggle_button not in ALLOWED_TOGGLE_BUTTON_VALUES:
47
- raise PluginError(f"Unexpected value for 'toggle_button': '{self.config.toggle_button}'. Allowed values are {', '.join(ALLOWED_TOGGLE_BUTTON_VALUES)}")
48
- return config
49
-
50
-
51
- def on_post_build(self, config: MkDocsConfig) -> None:
52
- if self.theme_function_definitions:
53
- target_path = os.path.join(config.site_dir, self.config.javascript)
54
- if os.path.exists(target_path):
55
- # The file exists. This probably means, that the user wanted to override the default file
56
- # So we just do nothing
57
- pass
58
- else:
59
- # Make sure that the folder exists
60
- parent_dir = os.path.dirname(target_path)
61
- os.makedirs(parent_dir, exist_ok=True)
62
-
63
- # Copy the file, while also editing it on the fly
64
- asset_path = os.path.join(SCRIPT_DIR, "toggle-sidebar.js")
65
- with open(asset_path) as f:
66
- data = f.read()
67
- data = data.replace("THEME_DEPENDENT_FUNCTION_DEFINITION_PLACEHOLDER", self.theme_function_definitions)
68
- data = data.replace("TOC_DEFAULT_PLACEHOLDER", "true" if self.config.show_toc_by_default else "false")
69
- data = data.replace("NAVIGATION_DEFAULT_PLACEHOLDER", "true" if self.config.show_toc_by_default else "false")
70
- data = data.replace("TOGGLE_BUTTON_PLACEHOLDER", self.config.toggle_button)
71
- with open(target_path, "w") as f:
72
- f.write(data)
73
-