mkdocs-toggle-sidebar-plugin 0.0.6__tar.gz → 0.0.7__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 (17) hide show
  1. {mkdocs_toggle_sidebar_plugin-0.0.6/src/mkdocs_toggle_sidebar_plugin.egg-info → mkdocs_toggle_sidebar_plugin-0.0.7}/PKG-INFO +2 -2
  2. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/README.md +1 -1
  3. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/setup.cfg +1 -1
  4. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/src/mkdocs_toggle_sidebar_plugin/__init__.py +37 -29
  5. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/src/mkdocs_toggle_sidebar_plugin/material.js +2 -1
  6. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7/src/mkdocs_toggle_sidebar_plugin.egg-info}/PKG-INFO +2 -2
  7. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/LICENSE +0 -0
  8. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/MANIFEST.in +0 -0
  9. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/pyproject.toml +0 -0
  10. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/src/mkdocs_toggle_sidebar_plugin/mkdocs.js +0 -0
  11. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/src/mkdocs_toggle_sidebar_plugin/readthedocs.js +0 -0
  12. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/src/mkdocs_toggle_sidebar_plugin/toggle-sidebar.js +0 -0
  13. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/src/mkdocs_toggle_sidebar_plugin.egg-info/SOURCES.txt +0 -0
  14. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/src/mkdocs_toggle_sidebar_plugin.egg-info/dependency_links.txt +0 -0
  15. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/src/mkdocs_toggle_sidebar_plugin.egg-info/entry_points.txt +0 -0
  16. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/src/mkdocs_toggle_sidebar_plugin.egg-info/requires.txt +0 -0
  17. {mkdocs_toggle_sidebar_plugin-0.0.6 → mkdocs_toggle_sidebar_plugin-0.0.7}/src/mkdocs_toggle_sidebar_plugin.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mkdocs-toggle-sidebar-plugin
3
- Version: 0.0.6
3
+ Version: 0.0.7
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
@@ -93,7 +93,7 @@ Option | Type | Default value | Description
93
93
  async | `bool` | `False` | Asynchronously load the JavaScript file created by the plugin
94
94
  debug | `bool` | `False` | Show some debug messages during mkdocs build (for example related to theme detection)
95
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
96
+ inline | `bool` | `False` | 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
97
  javascript | `str` | `"assets/javascripts/toggle-sidebar.js"` | The path where to store the output file
98
98
  show_navigation_by_default | `bool` | `True` | Whether to show the navigation by default
99
99
  show_toc_by_default | `bool` | `True` | Whether to show the table of contents by default
@@ -71,7 +71,7 @@ Option | Type | Default value | Description
71
71
  async | `bool` | `False` | Asynchronously load the JavaScript file created by the plugin
72
72
  debug | `bool` | `False` | Show some debug messages during mkdocs build (for example related to theme detection)
73
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
74
+ inline | `bool` | `False` | 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
75
  javascript | `str` | `"assets/javascripts/toggle-sidebar.js"` | The path where to store the output file
76
76
  show_navigation_by_default | `bool` | `True` | Whether to show the navigation by default
77
77
  show_toc_by_default | `bool` | `True` | Whether to show the table of contents by default
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = mkdocs-toggle-sidebar-plugin
3
- version = 0.0.6
3
+ version = 0.0.7
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
@@ -1,4 +1,5 @@
1
1
  import os
2
+ import urllib.parse
2
3
  # pip dependency
3
4
  from mkdocs.plugins import BasePlugin, get_plugin_logger
4
5
  from mkdocs.config.defaults import MkDocsConfig
@@ -25,7 +26,7 @@ class PluginConfig(Config):
25
26
  async_ = Type(bool, default=False)
26
27
  javascript = Type(str, default="assets/javascripts/toggle-sidebar.js")
27
28
  debug = Type(bool, default=False)
28
- inline = Type(bool, default=True)
29
+ inline = Type(bool, default=False)
29
30
 
30
31
 
31
32
  def is_known_theme(theme_name: str) -> bool:
@@ -43,6 +44,10 @@ def get_unknown_theme_message(theme_name: str, auto_detect_enabled: bool) -> str
43
44
  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
45
  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
+ def get_base_url_by_url(url: str):
48
+ if url == '':
49
+ return './'
50
+ return url.count('/') * '../'
46
51
 
47
52
  class Plugin(BasePlugin[PluginConfig]):
48
53
  def on_config(self, config: MkDocsConfig, **kwargs) -> MkDocsConfig:
@@ -70,7 +75,7 @@ class Plugin(BasePlugin[PluginConfig]):
70
75
  else:
71
76
  if not is_known_theme(theme_name):
72
77
  LOGGER.warning(get_unknown_theme_message(theme_name, False))
73
-
78
+
74
79
  # If the theme is known to be based on another theme, then we resolve it to the base theme
75
80
  resolved_theme_name = THEME_COMPATIBILITY.get(theme_name, theme_name)
76
81
 
@@ -80,17 +85,9 @@ class Plugin(BasePlugin[PluginConfig]):
80
85
  with open(theme_path) as f:
81
86
  self.theme_function_definitions = f.read()
82
87
 
83
- if self.theme_function_definitions:
84
- if self.config.inline:
88
+ if self.theme_function_definitions and self.config.inline:
85
89
  # 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)
90
+ self.inline_javascript = f"<script>{self.get_toggle_sidebar_javascript(config)}</script>"
94
91
 
95
92
  if self.config.toggle_button not in ALLOWED_TOGGLE_BUTTON_VALUES:
96
93
  raise PluginError(f"Unexpected value for 'toggle_button': '{self.config.toggle_button}'. Allowed values are {', '.join(ALLOWED_TOGGLE_BUTTON_VALUES)}")
@@ -103,29 +100,40 @@ class Plugin(BasePlugin[PluginConfig]):
103
100
  LOGGER.debug(message)
104
101
 
105
102
  def on_post_page(self, html, /, *, page, config):
106
- if self.inline_javascript:
107
- html = html.replace("</head>", self.inline_javascript + "</head>")
103
+ if self.config.enabled and self.theme_function_definitions:
104
+ if self.inline_javascript:
105
+ html = html.replace("</head>", self.inline_javascript + "</head>")
106
+ else:
107
+ base_url = get_base_url_by_url(page.url)
108
+ script_src = urllib.parse.quote(base_url + self.config.javascript.lstrip("./"))
109
+ if self.config.async_:
110
+ script = f'<script src="{script_src}" async></script>'
111
+ else:
112
+ script = f'<script src="{script_src}"></script>'
113
+ html = html.replace("</head>", script + "</head>")
114
+
108
115
  return html
109
116
 
110
117
  def on_post_build(self, config: MkDocsConfig) -> None:
111
118
  if self.theme_function_definitions and not self.config.inline:
112
119
  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)
120
+ # To properly replace placeholders, we need to replace the JS file, even if it already exists
121
+ # Make sure that the folder exists
122
+ parent_dir = os.path.dirname(target_path)
123
+ os.makedirs(parent_dir, exist_ok=True)
124
+
125
+ # Copy the file, while also editing it on the fly
126
+ javascript = self.get_toggle_sidebar_javascript(config)
127
+ with open(target_path, "w") as f:
128
+ f.write(javascript)
129
+
130
+ def get_toggle_sidebar_javascript(self, config: MkDocsConfig):
131
+ # Default to the output path of the JavaScript file, so that users can modify the JavaScript (even in inline mode)
132
+ asset_path = os.path.join(config.docs_dir, self.config.javascript)
133
+ if not os.path.exists(asset_path):
134
+ asset_path = os.path.join(SCRIPT_DIR, "toggle-sidebar.js")
135
+ self.debug(f"[*] Loading JavaScript template from {asset_path}")
126
136
 
127
- def get_toggle_sidebar_javascript(self):
128
- asset_path = os.path.join(SCRIPT_DIR, "toggle-sidebar.js")
129
137
  with open(asset_path) as f:
130
138
  data = f.read()
131
139
  data = data.replace("THEME_DEPENDENT_FUNCTION_DEFINITION_PLACEHOLDER", self.theme_function_definitions)
@@ -29,10 +29,11 @@ if (!showTOC) {
29
29
  }
30
30
 
31
31
  // We always have to show the navigation in mobile view, otherwise the hamburger menu is broken
32
+ // In material for mkdocs's blog mode, navigation's class is '.md-sidebar--post', see #9
32
33
  if (!showNavigation) {
33
34
  style += `
34
35
  @media screen and (min-width: 76.1875em) {
35
- div.md-sidebar.md-sidebar--primary {
36
+ div.md-sidebar.md-sidebar--primary, div.md-sidebar.md-sidebar--post {
36
37
  display: none;
37
38
  }
38
39
  }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mkdocs-toggle-sidebar-plugin
3
- Version: 0.0.6
3
+ Version: 0.0.7
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
@@ -93,7 +93,7 @@ Option | Type | Default value | Description
93
93
  async | `bool` | `False` | Asynchronously load the JavaScript file created by the plugin
94
94
  debug | `bool` | `False` | Show some debug messages during mkdocs build (for example related to theme detection)
95
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
96
+ inline | `bool` | `False` | 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
97
  javascript | `str` | `"assets/javascripts/toggle-sidebar.js"` | The path where to store the output file
98
98
  show_navigation_by_default | `bool` | `True` | Whether to show the navigation by default
99
99
  show_toc_by_default | `bool` | `True` | Whether to show the table of contents by default