You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be helpful to render yaml key-values in markdown.
As now we have no convenient way to document for example configuration variables. These are common in technical documentation and displaying these variables as links is not really an option. Updating each document is time consuming comparing to the situation where we have these variables explicitly defined.
Current problem
If I change the development environment ip-address I need to go through all the documentation and update each document which refers to this ip-address.
I think this idea has merit, I can see it being very useful. I expect we would want something like this for the 1.0 release. Possibly tied in with #144 somehow.
I'm thinking this may be a good candidate for a Plugin (see #206) if/when that API lands. What I envision is a plugin that uses a pre-build hook, accepts the Markdown document as a template and passes it through Jinja with the document's context. The resulting Markdown document (with variables filled in from the context) would be returned to Mkdocs for building.
However, I also expect that it would use non-standard variable start and end strings (i.e., perhaps [[ somevar ]] instead of {{ somevar }}). Otherwise, is would be rather difficult to include template snippets in code blocks within the Markdown text (they would need to be manually escaped).
And of course, the output would need to be valid Markdown. Some block level templating stuff may not work and various parts of Jinja would ideally be disabled (which may or may not be possible--I haven't worked the feasibility of that out). As it comes with lots of caveats (and could be a support headache), I think it makes a better candidate for a Plugin rather than something that is supported out-of-the-box.
I would like to join this.
I also agree, that this, indeed would be usefull to have variables
So for example, I can write:
You can visit [Content > Documents bank]({{ variables.base_url }}/admin/photobanks)
And user would be pointed to specific URL. In my use case, every documentation needs to (so called) base uri.
But it is based on the project itself. We could also set up some other variables.
I should note that my proposal above may actually work better using one of the PythonMustacheClones (rather than Jinja) for "logic-less templates" -- which would be much more desirable within a Markdown document I would think.
Before pulling in yet another template library, I think it is very possible to get Jinja2 working. Maybe all of Jinja2's syntax wouldn't be useful in Markdown content, but you can let the user discover that.
block_start_string
The string marking the beginning of a block. Defaults to '{%'.
block_end_string
The string marking the end of a block. Defaults to '%}'.
variable_start_string
The string marking the beginning of a print statement. Defaults to '{{'.
variable_end_string
The string marking the end of a print statement. Defaults to '}}'.
comment_start_string
The string marking the beginning of a comment. Defaults to '{#'.
comment_end_string
The string marking the end of a comment. Defaults to '#}'.
I imagine if Jinja2 was used, if a user had conditional logic, it would be wise to keep it inline. But you would have access to filters and such as well so that is pretty cool ;).
@facelessuser as stated in my original proposal, I was intending to do just that (use Jinja). However, I feel like it offers too much for generating Markdown content. Granted that it my personal opinion (I don't even like the additional logic Jinja gives you over Django templates in general use). If it is implemented as a plugin, multiple solutions could exist side-by-side. I expect a Mustache solution would serve most users fine.
I see. Yeah, you are probably right. If it is in a plugin, then it isn't like you are adding more dependencies directly into mkdocs. Also, I guess if the inter-markdown templating is limited in the Markdown source, you will also get less noise in the issues about people trying to do something that doesn't work well in Markdown, and that is also a plus.
@waylan That definitely is an interesting solution, however I feel iffy about using HTML comments for anything but comments.
If there's a typo in a variable name or not passed into the context for replacement it'll be harder to tell than replacing {{ $variable }} -- with the exception of links not working either way. In some cases:
This documentation was published on <!-- $date_now -->.
it would still be possible to tell that something was not processed correctly, however it won't be as obvious as seeing {{ $date_now }} rendered on the page. I guess as long as the variable processing has an option to error when not all variables are replaced it probably won't matter.
I'd personally still rather continue using Jinja2 and have the markdown processed first in the context of globally defined variables/plugins, then for the templates.
The Plugin API is in the 1.0.dev branch, which is still in development and has not yet been released. When that branch stabilizes and is released, we will then turn our attention to some of the more popular plugin requests (such as this one).
Just stumbled over this, since I wanted to call some Jinja-macros from Markdown. It turned out that this can basically be added in the Jinja-templates, without any modification to MkDocs.
I now define a whitelist in the Jinja-Templates, which contains all variables/placeholders (plus their mapping to Jinja-macros/variables/functions) which can be used in Markdown, e.g.:
Markdown:
Hello World: {{ myfunc }}
Some other macro: {{ foo.bar }}
Template:
{%- from "jinja.html" import jinja %}
{%- set jinja_allowed = {
"myfunc": myfunc,
"foo.bar": foo.bar
%}
{{ jinja(page.conent, jinja_allowed) }}
This works quite well.
But of course it could be improved/simplified if (a) a list of defined macros, (b) a "run-macro-by-name"-function and (c) regular expressions would be accessible in Jinja.
my jinja.html:
{# run Jinja-expressions from markdown
Often, it's useful to use placeholders or functions in Markdown.
This can be done via the Jinja-template, by defining using placeholders
in Markdown and replacing them with the result of Jinja-macros.
Usage:
- Markdown:
{{ myexpr }}
- Template:
{%- from "jinja.html" import jinja %}
{%- set jinja_allowed = {"myexpr": mymacro, ...} %}
{{ jinja(page_content, jinja_allowed) }}
Version: 2019-02-02
Author: Roland Koebler <[email protected]>
#}
{%- macro jinja(s, allowed) %}
{%- if "{{ " not in s or " }}" not in s %}
{{- s }}
{%- else %}
{%- set s = s.replace("<p>{{ ", "{{ ").replace(" }}</p>", " }}") %}
{%- set parts = s.split("{{ ") %}
{{- parts[0] }}
{%- for part in parts[1:] %}
{%- set expr_rest = part.split(" }}", 1) %}
{%- if expr_rest|length == 2 %}
{%- set expr = expr_rest[0].strip() %}
{%- if expr in allowed %}
{{- allowed[expr]() }}
{%- endif %}
{{- expr_rest[1] }}
{%- else %}
{{- "{{ "+part }}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- endmacro %}
@rkoe Awesome! I've modified it to support variables and args/kwargs to functions.
{# run Jinja-expressions from markdown
Often, it's useful to use placeholders or functions in Markdown.
This can be done via the Jinja-template, by defining using placeholders
in Markdown and replacing them with the result of Jinja-macros.
Usage:
- Markdown:
{{ myvariable }}
{{ myfunc(http://example.com/foo.png, desc=Description }}
- Template:
{% macro myfunc(src,desc='') -%}
<img style="display:block;" src="{{src}}">
<span>{{desc}}</span>
{%- endmacro %}
{% set myvariable = "Hello world!" %}
{%- from "jinja.html" import jinja %}
{%- set allowed = {"myfunc": myfunc, "myvariable": myvariable} %}
{{ jinja(page_content, allowed) }}
Version: 2019-02-28
Author: Roland Koebler <[email protected]>
Author: Evan Widloski <[email protected]>
#}
{%- macro render_macros(s, expressions) %}
{%- if "{{ " not in s or " }}" not in s %}
{{- s }}
{%- else %}
{%- set parts = s.split("{{") %}
{{- parts[0] }}
{# render each function/variable, then the context up to the next func/var #}
{%- for part in parts[1:] %}
{%- set expr_parts = part.split("}}") %}
{%- set expression = expr_parts[0].strip(" ") %}
{%- set rest = "".join(expr_parts[1:]) %}
{# handle functions #}
{%- if "(" in expression %}
{%- set func_parts = expression.split("(") %}
{%- set func_name = func_parts[0] %}
{%- set arguments = "".join(func_parts[1:]).rstrip(")").split(",") %}
{# handle args and kwargs #}
{%- set args = [] %}
{%- set kwargs = {} %}
{%- for argument in arguments %}
{%- if "=" in argument %}
{%- set kwarg_parts = argument.strip(" ").split("=") %}
{# just a hack for kwargs[kwarg_parts[0]] = kwarg_parts[1] #}
{%- set _ = kwargs.__setitem__(kwarg_parts[0], kwarg_parts[1]) %}
{%- else -%}
{%- set _ = args.append(argument) -%}
{%- endif %}
{%- endfor %}
{# call the function #}
{%- if func_name in expressions %}
{{- expressions[func_name](*args, **kwargs) }}
{%- endif %}
{# handle regular variables #}
{%- else %}
{%- if expression in expressions %}
{{- expressions[expression] }}
{%- endif %}
{%- endif %}
{{- rest }}
{%- endfor %}
{%- endif %}
{%- endmacro %}
Activity
d0ugal commentedon Dec 29, 2014
I think this idea has merit, I can see it being very useful. I expect we would want something like this for the 1.0 release. Possibly tied in with #144 somehow.
d0ugal commentedon Jan 4, 2015
That implementation looks more like what I expect #144 to be. Data to be used from the template, not the markdwon content itself.
waylan commentedon Jun 1, 2015
I'm thinking this may be a good candidate for a Plugin (see #206) if/when that API lands. What I envision is a plugin that uses a pre-build hook, accepts the Markdown document as a template and passes it through Jinja with the document's context. The resulting Markdown document (with variables filled in from the context) would be returned to Mkdocs for building.
However, I also expect that it would use non-standard variable start and end strings (i.e., perhaps
[[ somevar ]]instead of{{ somevar }}). Otherwise, is would be rather difficult to include template snippets in code blocks within the Markdown text (they would need to be manually escaped).And of course, the output would need to be valid Markdown. Some block level templating stuff may not work and various parts of Jinja would ideally be disabled (which may or may not be possible--I haven't worked the feasibility of that out). As it comes with lots of caveats (and could be a support headache), I think it makes a better candidate for a Plugin rather than something that is supported out-of-the-box.
huglester commentedon Jun 1, 2015
I would like to join this.
I also agree, that this, indeed would be usefull to have variables
So for example, I can write:
And user would be pointed to specific URL. In my use case, every documentation needs to (so called) base uri.
But it is based on the project itself. We could also set up some other variables.
Thanks
waylan commentedon Jun 1, 2015
I should note that my proposal above may actually work better using one of the Python Mustache Clones (rather than Jinja) for "logic-less templates" -- which would be much more desirable within a Markdown document I would think.
d0ugal commentedon Jun 1, 2015
+1 to logic-less.
It would be somewhat interesting to see what people could do with the full power of jinja2, but it also sounds super messy.
waylan commentedon Jun 1, 2015
As a reminder to myself, pystache can accept alternate deliminators to it's parse function (so we can do
[[ somevar ]]instead of{{ somevar }}):facelessuser commentedon Jun 1, 2015
Why not just change the delimiters in Jinja2? http://jinja.pocoo.org/docs/dev/api/#high-level-api
Before pulling in yet another template library, I think it is very possible to get Jinja2 working. Maybe all of Jinja2's syntax wouldn't be useful in Markdown content, but you can let the user discover that.
block_start_string
The string marking the beginning of a block. Defaults to '{%'.
block_end_string
The string marking the end of a block. Defaults to '%}'.
variable_start_string
The string marking the beginning of a print statement. Defaults to '{{'.
variable_end_string
The string marking the end of a print statement. Defaults to '}}'.
comment_start_string
The string marking the beginning of a comment. Defaults to '{#'.
comment_end_string
The string marking the end of a comment. Defaults to '#}'.
facelessuser commentedon Jun 1, 2015
I imagine if Jinja2 was used, if a user had conditional logic, it would be wise to keep it inline. But you would have access to filters and such as well so that is pretty cool ;).
waylan commentedon Jun 1, 2015
@facelessuser as stated in my original proposal, I was intending to do just that (use Jinja). However, I feel like it offers too much for generating Markdown content. Granted that it my personal opinion (I don't even like the additional logic Jinja gives you over Django templates in general use). If it is implemented as a plugin, multiple solutions could exist side-by-side. I expect a Mustache solution would serve most users fine.
facelessuser commentedon Jun 1, 2015
I see. Yeah, you are probably right. If it is in a plugin, then it isn't like you are adding more dependencies directly into mkdocs. Also, I guess if the inter-markdown templating is limited in the Markdown source, you will also get less noise in the issues about people trying to do something that doesn't work well in Markdown, and that is also a plus.
geiseri commentedon Jun 1, 2015
23 remaining items
mistresseve666 commentedon Feb 13, 2016
@waylan That definitely is an interesting solution, however I feel iffy about using HTML comments for anything but comments.
If there's a typo in a variable name or not passed into the context for replacement it'll be harder to tell than replacing
{{ $variable }}-- with the exception of links not working either way. In some cases:it would still be possible to tell that something was not processed correctly, however it won't be as obvious as seeing
{{ $date_now }}rendered on the page. I guess as long as the variable processing has an option to error when not all variables are replaced it probably won't matter.I'd personally still rather continue using Jinja2 and have the markdown processed first in the context of globally defined variables/plugins, then for the templates.
facelessuser commentedon Feb 13, 2016
You could always configure jinja2 brackets to look like HTML comments if you wanted to. No sense in implementing a template engine.
geiseri commentedon Feb 25, 2016
I can live with jinja2 looking like HTML. Then it wouldn't break stuff like github markdown, would it?
undersound commentedon May 25, 2016
+1
Is there any update on this?
margagn commentedon Oct 1, 2016
waylan commentedon Oct 1, 2016
@margagn we are waiting for the Plugin API to happen first (see #206), then this feature will likely be implemented as a plugin.
hellt commentedon Aug 22, 2017
As I see API has been delivered, what about this feature of getting variables into markdown?
waylan commentedon Aug 22, 2017
The Plugin API is in the 1.0.dev branch, which is still in development and has not yet been released. When that branch stabilizes and is released, we will then turn our attention to some of the more popular plugin requests (such as this one).
Ir1d commentedon Aug 13, 2018
Hi, I've noticed a plugin here: https://github.com/rosscdh/mkdocs-markdownextradata-plugin.
Is there a way to implement this without using a plugin?
Thx in advance.
waylan commentedon Aug 13, 2018
@Ir1d no this will always be a plugin only feature. Even if we provide an official plugin, it will continue to be through a plugin only.
rkoe commentedon Feb 3, 2019
Just stumbled over this, since I wanted to call some Jinja-macros from Markdown. It turned out that this can basically be added in the Jinja-templates, without any modification to MkDocs.
I now define a whitelist in the Jinja-Templates, which contains all variables/placeholders (plus their mapping to Jinja-macros/variables/functions) which can be used in Markdown, e.g.:
This works quite well.
But of course it could be improved/simplified if (a) a list of defined macros, (b) a "run-macro-by-name"-function and (c) regular expressions would be accessible in Jinja.
my jinja.html:
Evidlo commentedon Feb 28, 2019
@rkoe Awesome! I've modified it to support variables and
args/kwargsto functions.oprypin commentedon Jul 8, 2021