-
First of all, this is a great plugin; thank you for making this available to the community. I'm working on a project where we'd like to use this plugin with a lightweight soft of "feature flags" system to show or hide certain parts of our user documentation based on the presence of these flags. For the content, defining our own is_enabled macro works great. However, we're not sure on the best way to conditionally modify the navigation. Currently we have this in the mkdocs.yml -- and we'd like to check the flags to determine if certain pages should be excluded nav:
- "Introduction": "introduction.md"
- "Feature One": "feature-one.md"
{% if is_enabled("feature-two") %}
- "Feature Two": "feature-two.md"
{% endif %} This does not work as-is, unsurprisingly. Is there another strategy to achieve this? Is the best option to subclass the plugin and override on_nav ? Or perhaps the easiest option is to just make a new plugin locally that does this one thing? I also noticed that on_nav implementation in this plugin does store off the original navigation object as "navigation" var. Is there a hook that this plugin supports where that nav structure could be modified? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Thanks a lot for your appreciation! I had not thought about your use case, but it makes a lot of sense. Indeed, Mkdocs-Macros plugin won't work directly with the snippet above, since the plugin renders only the markdown files. You could take a number of approaches, depending on your use case and circumstances. 🤔 You wish to make this choice at build/serve time, right? In that case, why don't follow your idea through, but without the plugin? A simple solution could be: you create source config file called, e.g. You could write a Python utility that does more or less this:
Then you would create a script that does this:
I hope this helps. |
Beta Was this translation helpful? Give feedback.
-
@hozn Hi! Were you able to create a solution for this? I'm trying to do the same thing. |
Beta Was this translation helpful? Give feedback.
-
Hi, thanks for reaching back again, about this issue of conditional navigation. Jinja2 is very expressive. However, Mkdocs-Macros is designed to work only on the markdown files -- that's its sphere of action (plus a few goodies around it). Concerning the config file, it's a problem of the chicken and the egg. Mkdocs-Macros relies on the config file to read its variables (from The underlying question is: where would you store your |
Beta Was this translation helpful? Give feedback.
-
The proper way to do that would be to use a hook on the on_nav event. If you don't want to write your own plugin, you could just use a hooks.py file: # hooks.py
def on_nav(nav, config, files):
# Your logic here
return nav And, in the config file: hooks:
- hooks.py Your on_nav would be able to read the |
Beta Was this translation helpful? Give feedback.
Thanks a lot for your appreciation! I had not thought about your use case, but it makes a lot of sense.
Indeed, Mkdocs-Macros plugin won't work directly with the snippet above, since the plugin renders only the markdown files.
You could take a number of approaches, depending on your use case and circumstances. 🤔 You wish to make this choice at build/serve time, right?
In that case, why don't follow your idea through, but without the plugin? A simple solution could be: you create source config file called, e.g.
mkdocs_source.yaml
which contains exactly the code you suggested.You could write a Python utility that does more or less this:
doc…