Skip to content

Resolve Variables from mkdocstrings #233

Closed

Description

@kferrone

I have a variable in the docstrings within code

def foo():
  """The foo func be like {{ some_var }}"""

then in the markdown

---
some_var: howdy dudes
---
::: mymod.myfile.foo

Is there a way to render variables that come in the output of other plugins?

Activity

  1. github-actions commented on Jul 17, 2024

    @github-actions

    Welcome to this project and thank you!' first issue

  2. fralau commented on Jul 17, 2024

    @fralau
    Owner

    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?

  3. kferrone commented on Jul 17, 2024

    @kferrone
    Author

    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:

    import logging, re
    import mkdocs.plugins as plugins
    
    log = logging.getLogger('mkdocs')
    
    def my_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 event
    def on_env(env, config, files, **kwargs): 
        log.warning("Hello from one env!")
        env.filters['my_filter'] = my_filter
        return env
    
    # This actually works for now
    def on_page_content(html, page, **kwargs):
        return html.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?

  4. fralau commented on Jul 18, 2024

    @fralau
    Owner

    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 (set force_rendering=True).

    def render(self, markdown: str, force_rendering:bool=False):
    """
    Render a page through jinja2: it reads the code and
    executes the macros.
    It tests the `render_macros` metavariable
    in the page's header to decide whether to actually
    render or not (but you can force it).

  5. pawamoy commented on Jul 20, 2024

    @pawamoy

    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

    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) 🙂

  6. fralau commented on Jul 20, 2024

    @fralau
    Owner

    @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.

  7. Repository owner locked and limited conversation to collaborators on Jul 20, 2024
  8. converted this issue into a discussion #235 on Jul 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    useful tipA how-to, good to know

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions