You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been messing around with this a bit.
I tried with some hooks I made. I'm now having trouble getting a filter registered before the templates are rendered.
Here is some tests I'm playing with:
importlogging, reimportmkdocs.pluginsaspluginslog=logging.getLogger('mkdocs')
defmy_filter(value):
return"Hello there"# this always runs too late, it complains the filter doesn't exist before running this event# I think mkdocstrings is doing jinja stuff on their own before mkdocs gives the eventdefon_env(env, config, files, **kwargs):
log.warning("Hello from one env!")
env.filters['my_filter'] =my_filterreturnenv# This actually works for nowdefon_page_content(html, page, **kwargs):
returnhtml.replace('{kind}', page.meta.get('kind', 'unknown'))
The on_page_content event seems to be late in the game enough. However, I'm not sure how to incorporate jinja magic here or do any kind of safe replacement of variables.
The on_page_markdown event happens very early on. Anything replaced here is before any of the plugins replace their stuff, ie mkdocstrings.
So maybe this plugin can run a bit later? What event does this plugin run during?
💡 Tip: if all else fails you could use the env.render() method to interpret your Jinja2 template and then ouput it as the result of a macro (set force_rendering=True).
As long, however, as the output of mkdocstrings is inserted into the page before Mkdocs-Macros does its magic.
It is not! At least not as mkdocs-macros would expect it. mkdocstrings works during Markdown conversion. MkDocs hooks never get to see the raw contents of docstrings.
I think mkdocstrings is doing jinja stuff on their own before mkdocs gives the event
Activity
github-actions commentedon Jul 17, 2024
Welcome to this project and thank you!' first issue
fralau commentedon Jul 17, 2024
In theory, that might work 🤔
As long, however, as the output of mkdocstrings is inserted into the page before Mkdocs-Macros does its magic.
Have you tried?
kferrone commentedon Jul 17, 2024
I have been messing around with this a bit.
I tried with some hooks I made. I'm now having trouble getting a filter registered before the templates are rendered.
Here is some tests I'm playing with:
The
on_page_contentevent seems to be late in the game enough. However, I'm not sure how to incorporate jinja magic here or do any kind of safe replacement of variables.The
on_page_markdownevent happens very early on. Anything replaced here is before any of the plugins replace their stuff, ie mkdocstrings.So maybe this plugin can run a bit later? What event does this plugin run during?
fralau commentedon Jul 18, 2024
Have you tried writing a Mkdocs-Macros module in Python (
main.py), and trigger events from there, using one of the hooks available?💡 Tip: if all else fails you could use the
env.render()method to interpret your Jinja2 template and then ouput it as the result of a macro (setforce_rendering=True).mkdocs-macros-plugin/mkdocs_macros/plugin.py
Lines 495 to 503 in 9a08f11
pawamoy commentedon Jul 20, 2024
It is not! At least not as mkdocs-macros would expect it. mkdocstrings works during Markdown conversion. MkDocs hooks never get to see the raw contents of docstrings.
Indeed, we use our own env.
To enable mkdocs-macros' features inside docstrings you will have to use a hack like this one: mkdocstrings/mkdocstrings#615 (comment) 🙂
fralau commentedon Jul 20, 2024
@pawamoy My bad, I had forgotten about that... 😄
I am still in awe, because you might have a better understanding of Mkdocs-Macros, than I of mkdocstrings.