Replies: 5 comments 3 replies
-
Thanks for your question. I think I understand what you wish to do. Alas, Jinja2 does not allow (at least to my knowledge) to know what's "in-between". To solve that problem, I am not sure you that creating snippets would give you a simple solution, since you would have to store them somewhere, etc. Do you have a special motivation for that? What I would do would be the following: in extra in the config file (or in the page header): extra:
authorized_users: usertype1, usertype2
In that way, if you wanted to change the number of users, or their names, etc., you would only have to change your config file. |
Beta Was this translation helpful? Give feedback.
-
@jcyrss Thanks a lot for this information. I looked up what a shortcode is in Hugo, and it seems that it is functionally equivalent to an MkDocs macro, right? I had toyed in the past with the concept of "shortcodes with opening and closing tag". If one wants to implement that elegantly, one obviously needs a stack that keeps track of the tags (because they can be embedded in each other). Basically you have sets of twin macros (opening and end), which rely on some scaffolding. It is not difficult to implement that (I did it before), it just requires some precision work. But accessing the "inner", as Hugo is doing so elegantly... I had thought about it, and it might definitely have some applications. Right now, I am not sure how we could do that. We might have, as you suggest, to do some parsing of the markdown. And furthermore, if we managed to do it as a pluglet, rather than a core function of Mkdocs-Macros... Basically we should start by writing a module 🤔 . It wouldn't be any old module, though, but something more sophisticated. As you see, I went full circle and back to your initial idea. 🙂 |
Beta Was this translation helpful? Give feedback.
-
I asked Ma (ChatGPT) the following question:
Answer In Jinja2, the content between two directives is processed when the template is rendered. However, Jinja2 does not provide a built-in way to directly access the raw, unprocessed code between two directives. For example, if you have a block in your Jinja2 template like this:
The string If you need to manipulate or analyze the raw template code, you might need to read the template file as a regular text file in Python, and then use standard string manipulation or regular expressions to extract the content between the directives. Please note that this approach bypasses Jinja2’s template processing and works directly on the raw template code. It might not be suitable for all use cases, especially if your templates use advanced Jinja2 features or inherit from other templates. My comment: Pretty to the point, I must say. |
Beta Was this translation helpful? Give feedback.
-
Yes, it would, a little of course, though I wouldn't expect that to be a huge problem. I guess it would be possible to build a data structure (tree) containing the open blocks and their contents (or how to access it, in case Jinja2 allows it). |
Beta Was this translation helpful? Give feedback.
-
@jcyrss By the way (lateral thinking) have you had a look at Template Inheritance, in Jinja2? It might solve your problem, as well? I can't tell, because I don't have the full statement of the problem you want to solve, and your constraints. |
Beta Was this translation helpful? Give feedback.
-
Some markdown contents only for some users. I know it could be done like this in markdown file,
But if I want to define a macro do that, like this,
How to define end of the
onlyfor
scope in markdown file? like{{ end only_for}}
?How to get the markdown snippet in macro function definition?
Beta Was this translation helpful? Give feedback.
All reactions