zephraph / nunjucks-markdown

Markdown extension for Nunjucks. Use your own renderer!
MIT License
49 stars 12 forks source link

Indentation causing clipping when using {% include %} #19

Open kevinmpowell opened 8 years ago

kevinmpowell commented 8 years ago

I'm having a similar issue as reported here: https://github.com/zephraph/nunjucks-markdown/issues/4#issuecomment-169419993

But it only occurs when I use the {% include 'file.md' %} method. I'm actually seeing different bugs with the first and second lines of the included file depending on the indentation of the call.

Example:

{% markdown %}
{% include 'file.md' %}
# YES
{% endmarkdown %}

{% markdown %}
  {% include 'file.md' %}
  # NO, first line of 'file.md' becomes wrapped in <pre><code> tags, 
  # subsequent lines are fine
{% endmarkdown %}

  {% markdown %}
  {% include 'file.md' %}
  # NO, first line of 'file.md' renders fine, second line has first four 
  # characters trimmed
  {% endmarkdown %}

  {% markdown %}
    {% include 'file.md' %}
    # NO, first and second line errors are BOTH present with 
    # this syntax 
  {% endmarkdown %}

I'm using the grunt-nunjucks-2-html package in a project that has 4 spaces as the indentation method.

zephraph commented 8 years ago

I'm definitely open for PRs.

colinsullivan commented 8 years ago

This is happening to me. I am not using the include tag, I am using

{% markdown %}{{textToRender}}{% endmarkdown %}

from within a macro.

Fixed by changing it to:

{% markdown %}
{{textToRender}}
{% endmarkdown %}
internalfx commented 8 years ago

@colinsullivan are you only rendering one line of markdown at a time?

colinsullivan commented 8 years ago

I was experiencing this for both:

In both templates I was attempting the single line approach (this line was indented):

{% markdown %}{{textToRender}}{% endmarkdown %}

In both cases, I was able to work around the issue by adjusting indentation of the {% markdown %} and {{textToRender}} lines relative to one another.

internalfx commented 8 years ago

@zephraph I'm not sure how to fix this problem without breaking the indentation feature.

The indentation normalization feature makes it nice when you are writing markdown directly in your nunjucks files, but messes up when nunjucks-markdown is being used in a more automated fashion.

I think that the best solution is simply to make the indentation feature easy to disable.

internalfx commented 8 years ago

.....or even disabled by default.

zephraph commented 8 years ago

It's a super weird bug. We could pass a flag in to disable the indention feature. I'm sure there's got to be a way to actually fix it. The first step to that is writing tests to replicate the issue.

brad commented 8 years ago

Just ran into this issue myself. Worked around it using a macro:

{% macro markdown() %}
{% set content=
caller() %}
{% markdown %}
{{ content }}
{% endmarkdown %}
{% endmacro %}

Then rendering the markdown like so:

{% call() markdown() %}{{ my_markdown }}{% endcall %}
brundonsmith commented 8 years ago

I think the best solution for this problem, at least for @colinsullivan's case (and my own) would be to allow markdown to be applied as a filter as well, so that for those cases you know you're only feeding it the contents of the model attribute: {{ textToRender | markdown }}

haavardb commented 7 years ago

Ran into this issue myself. As a temp-fix it should be mentioned in the readme maybe?

In my case at least: https://gist.github.com/haavardb/643d78d6914992dcb955a4abac5e370a