rosscdh / mkdocs-markdownextradata-plugin

A MkDocs plugin that injects the mkdocs.yml extra variables into the markdown template
MIT License
83 stars 18 forks source link

Plugin chain breaks when using jinja2.Template #26

Closed steve-todorov closed 3 years ago

steve-todorov commented 4 years ago

Hi,

While writing my own plugin mkdocs-pom-parser-plugin I noticed it was specifically not working when it was together with yours. It took me a while to understand the issue, but in essence the code below:

https://github.com/rosscdh/mkdocs-markdownextradata-plugin/blob/9af2957fde9a99efa40faea67097c3c43d85cc98/markdownextradata/plugin.py#L91-L95

Should be converted to something like this (I haven't tested it in your repo, but works in mine):

    def on_page_markdown(self, markdown, config, **kwargs): 
        # log.debug("pom on_page_markdown[markdown: %s]", markdown)
        # Do not use Template because it will remove all unknown template variables and break the chain
        # if there are other plugins hooked to on_page_markdown event.
        # https://stackoverflow.com/questions/20120957/jinja-leave-template-tags-after-render
        # md_template = Template(markdown)
        env = jinja2.Environment(undefined=jinja2.DebugUndefined)
        md_template = env.from_string(markdown)
        return md_template.render(**config.get("extra"))

Since you are using Template, all other plugins which are also hooked to on_page_markdown will receive a sanitized template string which will not contain any of the unknown variables and thus resulting in head scratches and wondering why nothing shows when all tests are actually passing. :)

https://stackoverflow.com/questions/20120957/jinja-leave-template-tags-after-render

miffels commented 3 years ago

@steve-todorov feel free to provide a PR yourself next time. I made a PR real quick since I was taking a look, anyway.