fralau / mkdocs-macros-plugin

Create richer and more beautiful pages in MkDocs, by using variables and calls to macros in the markdown code.
https://mkdocs-macros-plugin.readthedocs.io
Other
323 stars 50 forks source link

Need a registration hook for other plugins #237

Open fralau opened 1 week ago

fralau commented 1 week ago

As explained by @timvink in squidfunk/mkdocs-material#5933, writers of other plugins would need some hook to declare variables, macros and filters before the Mkdocs-Macros is actually run.

The rule adopted is following: three methods register_variables(), register_macros() and register_filters().

To facilitate work, these three method should work regardless of the order of declaration of plugins.

In both cases, those keys are added on top of all other ones. Duplicate keys are not allowed and will cause a KeyError.

fralau commented 1 week ago

@timvink It's written. I have tested it roughly, so that I see that the code should work; but it would take me some time to build a test case.

Would you like to see if it works for you?

timvink commented 1 week ago

Sure, will try next week.

At the scale mkdocs-macros is at already, sure you don't write to write a unit test for this functionality, to ensure it keeps working in the future?

fralau commented 1 week ago

Sure, will try next week.

At the scale mkdocs-macros is at already, sure you don't write to write a unit test for this functionality, to ensure it keeps working in the future?

Yes. I have relied on testing my plugin on test websites (available), but I need to introduce a unit testing system.

In a few words, how do you go about unit-testing an mkdocs plugin, without using mkdocs serve (since it relies on MkDocs framework)?

timvink commented 1 week ago

You can test the functions / custom classes separately of course. I actually do prefer small 'integration' testing using mkdocs build.

What's worked for me is to build up a collection of small standalone examples (like here https://github.com/timvink/mkdocs-table-reader-plugin/tree/master/tests/fixtures) and test whether they fail (sometimes they should) or succeed, and whether the resulting html page(s) contain expected output. See f.e. https://github.com/timvink/mkdocs-table-reader-plugin/blob/master/tests/test_build.py

fralau commented 1 week ago

@timvink Thank you for these examples, this is very interesting!

timvink commented 1 week ago

Started having a look, and here's a first thing we can improve. I will need to implement it in a backwards compatible way, or at least require the user to upgrade to the latest version. So I want to know the version of macros plugin installed by the user.

Previously the convention was for python packages to have __version__ defined in the main __init__.py, so you can use package.__version__ to get the version. You can now do this dynamically by using this:

# __init__.py
import importlib.metadata

__version__ = importlib.metadata.version("mypackage")

But that will require you to move from setup.py to pyproject.toml first though. Soon pip will be updated to v25 and then things like editable installs for this plugin will start breaking, so that update will need to happen anyway. I've already upgraded some of my own plugins, for an example see https://github.com/timvink/mkdocs-table-reader-plugin/blob/master/pyproject.toml. Do note that this also then requires a different way to build your package (pip install build; python -m build or even better and way faster if you use uv: uv build)

For now I'll work around by testing to see if the register methods are available in table-reader.

timvink commented 4 days ago

Fixed it. See PR #238

Implemented in table reader here: https://github.com/timvink/mkdocs-table-reader-plugin/pull/77 (I will wait for new macros release before releasing it table reader)

fralau commented 3 days ago

I passed the PR. Thanks a lot!