Skip to content

Display yaml properties in markdown #304

Closed

Description

Summary

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.

Example solution

application-properties.yaml

environment.development.ip: 127.0.0.1
environment.development.port: 8000
  1. Connect to application server admin console and update the application.
  2. Test the application:
$ curl  {{ environment.development.ip }} : {{ environment.development.port }} 

would be rendered to:

$ curl 127.0.0.1:8000

Additional goals

Additionally you could create an external yaml file which would have some or all the properties defined.

Activity

  1. d0ugal commented on Dec 29, 2014

    @d0ugal
    Contributor

    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.

  2. added this to the 1.0.0 milestone on Dec 29, 2014
  3. d0ugal commented on Jan 4, 2015

    @d0ugal
    Contributor

    That implementation looks more like what I expect #144 to be. Data to be used from the template, not the markdwon content itself.

  4. waylan commented on Jun 1, 2015

    @waylan
    Contributor

    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.

  5. huglester commented on Jun 1, 2015

    @huglester

    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.

    Thanks

  6. waylan commented on Jun 1, 2015

    @waylan
    Contributor

    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.

  7. d0ugal commented on Jun 1, 2015

    @d0ugal
    Contributor

    +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.

  8. waylan commented on Jun 1, 2015

    @waylan
    Contributor

    As a reminder to myself, pystache can accept alternate deliminators to it's parse function (so we can do [[ somevar ]] instead of {{ somevar }}):

    renderer = pystache.Renderer()
    parsed = pystache.parse(template, deliminators=('[[', ']]'))
    output = renderer.render(parsed, context)
  9. facelessuser commented on Jun 1, 2015

    @facelessuser
    Contributor

    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 '#}'.

  10. facelessuser commented on Jun 1, 2015

    @facelessuser
    Contributor

    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 ;).

  11. waylan commented on Jun 1, 2015

    @waylan
    Contributor

    @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.

  12. facelessuser commented on Jun 1, 2015

    @facelessuser
    Contributor

    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.

  13. geiseri commented on Jun 1, 2015

    @geiseri
  14. 23 remaining items

  15. mistresseve666 commented on Feb 13, 2016

    @mistresseve666

    @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.

  16. facelessuser commented on Feb 13, 2016

    @facelessuser
    Contributor

    You could always configure jinja2 brackets to look like HTML comments if you wanted to. No sense in implementing a template engine.

  17. geiseri commented on Feb 25, 2016

    @geiseri

    I can live with jinja2 looking like HTML. Then it wouldn't break stuff like github markdown, would it?

  18. undersound commented on May 25, 2016

    @undersound

    +1

    Is there any update on this?

  19. modified the milestones: Post 1.0, 1.0.0 on May 25, 2016
  20. margagn commented on Oct 1, 2016

    @margagn
    • 1 - Any update?
  21. waylan commented on Oct 1, 2016

    @waylan
    Contributor

    @margagn we are waiting for the Plugin API to happen first (see #206), then this feature will likely be implemented as a plugin.

  22. hellt commented on Aug 22, 2017

    @hellt

    As I see API has been delivered, what about this feature of getting variables into markdown?

  23. waylan commented on Aug 22, 2017

    @waylan
    Contributor

    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).

  24. Ir1d commented on Aug 13, 2018

    @Ir1d

    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.

  25. waylan commented on Aug 13, 2018

    @waylan
    Contributor

    @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.

  26. rkoe commented on Feb 3, 2019

    @rkoe

    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 %}
    
  27. Evidlo commented on Feb 28, 2019

    @Evidlo

    @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 %}
    
    
  28. oprypin commented on Jul 8, 2021

    @oprypin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions