Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin chain breaks when using jinja2.Template #26

Closed
steve-todorov opened this issue Aug 31, 2020 · 1 comment
Closed

Plugin chain breaks when using jinja2.Template #26

steve-todorov opened this issue Aug 31, 2020 · 1 comment

Comments

@steve-todorov
Copy link

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:

def on_page_markdown(self, markdown, config, **kwargs):
context = {key: config.get(key) for key in CONFIG_KEYS if key in config}
context.update(config.get("extra", {}))
md_template = Template(markdown)
return md_template.render(**config.get("extra"))

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 added a commit to miffels/mkdocs-markdownextradata-plugin that referenced this issue Nov 2, 2020
@miffels
Copy link
Contributor

miffels commented Nov 2, 2020

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

@rosscdh rosscdh closed this as completed in b816b32 Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants