Replies: 2 comments 3 replies
-
Having looked at the render() method in def expand_macros(env, content):
"""
Apply the macros in the given content. This is adapted from the render() method of
the MacrosPlugin class.
Beware this this could, under unusual circumstances, give rise to infinite recursion. Don't, for
example, write a snippet that includes itself or a macro that returns content that contains a
call to itself. There is nothing to protect us from this as there is no end criterium. I guess
we could count how deeply nested `expand_macros` is called?
"""
page_variables = copy.copy(env.variables)
md_template = env.env.from_string(content) # get template based on content
result = md_template.render(**page_variables) # this is where the macros get executed
return result |
Beta Was this translation helpful? Give feedback.
-
Is my understanding of your use case correct: you want to expand macros contained in included files? In that case, I would not modify the basic code. I would create an ad-hoc macro that includes files and renders them, starting from this example? To keep things simple, I would start a new Jinja2 templating engine. If you want to pass variables to it, that should be fairly straightforward? |
Beta Was this translation helpful? Give feedback.
-
Hi,
I am working with text snippets that I have written a custom include macro for. The reason for this is that I need to be language-aware (using mkdocs-static-i18n) and I may also want to add conditionals.
Now, the snippets will in themselves contain macros and I want them to be executed. For example, a snippet may contain a cross reference, which should be rendered to a link if the target exists and omitted if not.
How do I take a piece of Markdown text that I return from my include macro and expand the macros contained within it? Any suggestions?
Cheers,
Alex
Beta Was this translation helpful? Give feedback.
All reactions