Support theme-namespaced plugin loading by oprypin · Pull Request #2998 · mkdocs/mkdocs · GitHub
Skip to content

Support theme-namespaced plugin loading#2998

Merged
oprypin merged 2 commits into
masterfrom
them
Oct 12, 2022
Merged

Support theme-namespaced plugin loading#2998
oprypin merged 2 commits into
masterfrom
them

Conversation

@oprypin

@oprypin oprypin commented Oct 8, 2022

Copy link
Copy Markdown

This is mainly aimed at 'material' theme which also ships plugins with it. It will be able to ship plugins under the name e.g. 'material/search' and that will ensure the following effects:

  • If the current theme is 'material', the plugin 'material/search' will always be preferred over 'search'.
  • If the current theme isn't 'material', the only way to use this plugin is by specifying plugins: [material/search].

One can also specify plugins: ['/search'] instead of plugins: ['search'] to definitely avoid the theme-namespaced plugin.

Previously:

@squidfunk

This is mainly aimed at 'material' theme which also ships plugins with it.
It will be able to ship plugins under the name e.g. 'material:search' and it will ensure the following effects:

* If the current theme is 'material', the plugin 'material:search' will always be preferred over 'search'.
* If the current theme *isn't* 'material', the only way to use this plugin is by specifying `plugins: [material:search]`.

One can also specify `plugins: [':search']` instead of `plugins: ['search']` to avoid the theme-namespaced plugin.
@squidfunk

squidfunk commented Oct 9, 2022

Copy link
Copy Markdown
Contributor

@oprypin

This comment was marked as abuse.

@squidfunk

Copy link
Copy Markdown
Contributor

I agree with your concerns. Just wanted to bring it up before it's out in the world. I guess most users won't use the : syntax anyway but rely on the defaults, so it's probably not that much of a problem. Not easy to find a better separator. >, < and | are all part of YAML syntax and . suffers from the problem you mentioned. So I guess : is really the best choice.

@squidfunk

Copy link
Copy Markdown
Contributor

... or maybe material/search and /search?

@oprypin

This comment was marked as abuse.

@ultrabug ultrabug left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool move

@unverbuggt

unverbuggt commented Nov 8, 2022

Copy link
Copy Markdown

Hi,

just stumbled upon this merge while researching why the search plugin of material theme is always used if material was installed. And there is currently no way to use the default search plugin (only by uninstalling mkdocs-material).

As mentioned in this merge there can be only one plugin named "search" after get_plugins() method and the merge defines the least priority to the "mkdocs.contrib." one.

In my opinion this is unwanted behaviour because you could install and use different themes and plugins in the same python installation. The built "search_index.json" won't differ much between the material and mkdocs search plugin, but if one installs the insiders version of material it is different (more slim, but contains html tags)

So this merge provides a slim solution for the problem, but the merge alone won't solve it.

We also need to change the way get_plugins() in plugins.py builds the plugin dictionary. Here is my quick (and probably not very elegant) fix for "plugins.py":

@@ -55,10 +55,8 @@
     # Allow third-party plugins to override core plugins
     pluginmap = {}
     for plugin in plugins:
-        if plugin.name in pluginmap and plugin.value.startswith("mkdocs.contrib."):
-            continue
-
-        pluginmap[plugin.name] = plugin
+        name = plugin.value.split('.',1)[0] + '/' + plugin.name
+        pluginmap[name] = plugin
 
     return pluginmap

After the patch I get the behaviour described in the merge: If theme is not material then default search plugin is used, however if I wanted to use material-theme's search I'd specify it as "material/search"

@oprypin

This comment was marked as abuse.

@squidfunk

Copy link
Copy Markdown
Contributor

unverbuggt added a commit to unverbuggt/mkdocs-encryptcontent-plugin that referenced this pull request Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants