Register custom events? #3815
Replies: 4 comments
-
|
Awesome use-case, great write-up I personally find this proposal very interesting, and am actually considering implementing it in Griffe since I based its extension system on the same event/hook approach as MkDocs. The ability to isolate the custom events from the main ones is very important IMO, to prevent future conflicts (just like top-level keys in pyproject.toml are reserved, and tools must put their configuration under |
Beta Was this translation helpful? Give feedback.
-
Yep nicely done. I'll convert this into a discussion and answer there. |
Beta Was this translation helpful? Give feedback.
-
|
So... counter-intuitively this is actually a really good example of why I'm looking towards a future iteration of MkDocs that doesn't require or support plugins in order to customise the page generation. My sense is that a revisited approach to templating, themes, and styling would remove almost all the requirements for custom Python code through plugins. There's also clear a smaller subset of cases that do strictly require a Python code-approach. I believe these could be supported through customising mkdocs at the core level, rather than injecting plugins. I believe this'll be worth pursing in order to avoid the complexity feedback loop, and move instead towards a more design-constrained but equally capable system. (I recognise that I'm not yet `demonstrating what I'm working towards here, figured it was worth keeping in touch tho since the discussion relates to this...) |
Beta Was this translation helpful? Give feedback.
-
|
I may not be the right person to say this, but as an MkDocs user, I believe that removing the plugin system would break the MkDocs ecosystem. I understand that MkDocs itself is going to be redesigned, but MkDocs 2 is supposed to be compatible with the current version, which relies on plugins. Moreover, many projects and custom themes depend on plugins . For example, mkdocs-materialx uses plugins to add functionality to the theme, and many plugin authors have filled gaps by providing features that are not available in MkDocs core. As I understand it, plugins were meant to allow users to extend MkDocs and make it more useful by adding the functionality they need without waiting for changes in the core project. Instead of removing plugins, I think we should focus on improving the existing system—making it more robust, removing constraints, and fixing current issues—because, as it stands, developers often have to compromise due to limitations in the current system. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Use Case Overview
I'm developing a plugin system where one plugin, referred to as a Visitor, needs to interact with other plugins during the build/serve process. These other plugins, referred to as Visitable, should be able to opt into this interaction by declaring a custom event that the Visitor can trigger.
Ideal Implementation
In an ideal scenario, the implementation might look like this:
Here, the
Visitorplugin triggers a custom event calledvisit, and anyVisitableplugin that defines anon_visitmethod would respond to this event.Current Limitations
However, I've encountered a limitation in MkDocs that prevents this pattern from working as intended:
Static Event Registration: MkDocs populates the
config.plugins.eventsdictionary when the configuration is first created. Since plugins are loaded at this stage, no plugin can modify the events dictionary before MkDocs finalizes the plugin setup. This means custom events likevisitcannot be dynamically added later in the process.Automatic Event Registration: Methods in a
BasePluginthat start withon_are automatically registered as events by MkDocs. However, since custom events are not pre-defined in theeventsdictionary, any attempt to register them will result in failure. Essentially, allon_methods are reserved for predefined MkDocs events, leaving no room for custom ones.To work around this, I tried manually registering a custom event:
While this approach technically works, it feels more like a hack than a robust solution. It’s not ideal for long-term maintainability or for broader plugin development.
Proposal
I propose that MkDocs should allow custom events to be automatically registered by any plugin. To avoid potential conflicts with MkDocs' own event system, these custom events could use a unique prefix, such as
on_custom_<event>, making it clear that they are user-defined.Real-World Application
In my specific use case, I'm developing a plugin that generates a PDF book from MkDocs documentation. This plugin creates a LaTeX project that can be compiled into a PDF. I opted not to use existing Markdown-to-PDF converters like Pandoc because I wanted the system to be highly extensible, allowing other plugins to provide custom transformations for LaTeX.
For example, if a
latexplugin is loaded, it could automatically trigger events such ason_latex_page, enabling other plugins to perform their tasks as they would for the HTML output. However, without support for custom events, this level of extensibility becomes difficult to achieve.Conclusion
While my use case is somewhat specialized, I believe that enabling custom events would benefit the MkDocs plugin ecosystem as a whole. It would allow for more flexible and modular plugin designs without requiring workarounds or hacks.
What are your thoughts on this proposal?
Beta Was this translation helpful? Give feedback.
All reactions