Allow third-party plugins to override core plugins#2591
Merged
squidfunk merged 2 commits intomkdocs:masterfrom Oct 7, 2021
squidfunk:fix/plugin-precedence
Merged
Allow third-party plugins to override core plugins#2591squidfunk merged 2 commits intomkdocs:masterfrom squidfunk:fix/plugin-precedence
squidfunk merged 2 commits intomkdocs:masterfrom
squidfunk:fix/plugin-precedence
Conversation
Contributor
Author
|
I intend to merge this in a few days. |
This comment was marked as abuse.
This comment was marked as abuse.
Contributor
Author
|
Okay, haven't heard anything and thought you're maybe too busy. A review would be great 👍 |
Member
|
Same here, rough times for me, will do my best |
oprypin
reviewed
Oct 6, 2021
This comment was marked as abuse.
This comment was marked as abuse.
oprypin
approved these changes
Oct 7, 2021
Contributor
Author
|
Thanks! I've included your suggestions, so this is ready for merge now. Can we release this as part of 1.2.3? Changes that would be included as I read from the commit history:
The rest looks like chores. Are those save to release? |
This comment was marked as abuse.
This comment was marked as abuse.
ultrabug
pushed a commit
to ultrabug/mkdocs
that referenced
this pull request
Oct 8, 2021
* Allow third-party plugins to override core plugins * Improve resilience of check for core plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a minor change to the
get_pluginsfunction, which allows third-party plugins to override plugins inmkdocs.contribin general and thesearchplugin in particular.This was first discussed in squidfunk/mkdocs-material#3053.
Rationale
The
searchplugin, which is part of the core, is probably the most widely used MkDocs plugin. As we all know, it's enabled by default and needs to be explicitly re-enabled when other plugins are added. This means that many authors use this plugin implicitly and rely on its presence, which is also why it's probably not a good idea to change this behavior afterward.I think it's safe to say that the MkDocs plugin system really took off. Personally, I think the plugin system is very well designed, as it's super easy to get started, which is why I began writing custom theme-centric plugins as part of Material for MkDocs:
tags– allows authors to add tags to the page and integrates with searchsocial– automatically generates social preview images for sharing on social mediasearch– a complete rewrite of MkDocs' ownsearchpluginNow, the
searchplugin is the newest addition to the list of theme-provided plugins, and after writing my third plugin, I must say that it makes extreme sense to provide extensions as part of theme-provided plugins. One could argue that plugins should be theme-generic, but some things are just not generalizable. For example, the seemingly unmaintained third-partymkdocs-plugin-tagsplugin doesn't allow for injecting tags into pages without using theme extension, and it doesn't further integrate with other features of the site like search. On the contrary, Material for MkDocs own implementation is a fully integrated solution, which doesn't need any customization by the author.Problem
The new
searchplugin which I first released as part of Insiders, generates richer previews and cuts indexing time and index size in half (see benchmarks). I've extensively written about it in this blog article, explaining in depth why I believe that thesearchplugin needed a makeover. It is intended to be a drop-in replacement, as the newest version of Material for MkDocs now doesn't use or support MkDocs own search plugin anymore since it has a quite different indexing strategy. Thus, Material for MkDocs is now incompatible with MkDocs own search plugin, which is also why I've decided that I need to expose the plugin under the same name. From squidfunk/mkdocs-material#3053 (comment):Overloading the
searchplugin, i.e., exporting an entry point with the same name seems to work in most cases, but not in all. This was first discovered in squidfunk/mkdocs-material#3053 (comment). Sometimes, plugins are loaded in the what I would say correct order, allowing for easy overriding:But sometimes, they are loaded in the opposite order, and I have no idea why:
This leads to the problem where MkDocs own search plugin overrides the custom theme-provided one.
Solution
This PR will now prioritize plugins with the same name over those provided in
mkdocs.contrib. This allows theme authors to more easily provide custom implementations. After all, search is a functionality that is very theme-centric, because themes need to provide additional JavaScript to make use of the search index.