zephraph / nunjucks-markdown

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

Prevents starting spaces from being stripped #23

Closed niftylettuce closed 2 years ago

internalfx commented 7 years ago

This may be ok...

@zephraph I'm not sure if this will break existing users templates...It shouldn't if your using the library "correctly".

zephraph commented 7 years ago

What about the failing test?

niftylettuce commented 7 years ago

I'll look if I get time!

On Nov 20, 2016 2:58 PM, "Zephraph" notifications@github.com wrote:

What about the failing test?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/zephraph/nunjucks-markdown/pull/23#issuecomment-261801223, or mute the thread https://github.com/notifications/unsubscribe-auth/AAf7hc8aUI0CUaxuxfUn0G00uBHOcGEeks5rAKZsgaJpZM4K3XQl .

niftylettuce commented 7 years ago

@internalfx @zephraph Okay we have an issue here that we need to standardize.

Either people need to do it like this:

<div>
  {% markdown %}
  foo bar baz
  {% endmarkdown %}
</div>

Or like this:

<div>
  {% markdown %}
foo bar baz
  {% endmarkdown %}
</div>

And we need to enforce strictly one or the other in our tests and Readme. We should enforce the first.

It's impossible to support both - why? Because if you have variables that have indentation in them, the indentation is not preserved, it is all stripped. Imagine you have a JSON object stored as a variable named foo, and it looks like this:

{
  "statusCode": 401,
  "error": "Unauthorized",
  "message": "Invalid token"
} 

When rendered (with the old approach before this pull request was submitted):

{
"statusCode": 401,
"error": "Unauthorized",
"message": "Invalid token"
} 

Especially with async rendered variables - the spacing in the variables when rendered is lost.

niftylettuce commented 7 years ago

Actually, I think the variable rendering issue is due to the parser not preserving indentation with rendered variables that have line breaks.

    // Otherwise parse until the close block and move the parser to the next position
    var body = parser.parseUntilBlocks('endmarkdown'); 
niftylettuce commented 7 years ago

https://github.com/mozilla/nunjucks/issues/734

niftylettuce commented 7 years ago

Input:

var jsonSpaces = JSON.stringify({
  "baz": "beep"
}, null, 2);
    <div class="col-xs-12">
      {% markdown %}
      ```json
      {{ jsonWithSpaces }}
  {% endmarkdown %}
</div>

Expected:

```html
    <div class="col-xs-12">
      <pre><code class="lang-json">{
        &amp;quot;baz&amp;quot;: &amp;quot;beep&amp;quot;
      }
      </code></pre>
    </div>

Actual:

    <div class="col-xs-12">
      <pre><code class="lang-json">{
&amp;quot;baz&amp;quot;: &amp;quot;beep&amp;quot;
}
</code></pre>

    </div>
zephraph commented 7 years ago

I agree with supporting only the first, but yeah, seems like the other issue will need to be fixed first. Thanks for your work so far. Maybe we can get some traction. This has been a lingering issue for a bit.

kmohrf commented 6 years ago

is anyone still working on this? I’ve been using raml2html which in turn uses nunjucks and this plugin to render rest api documentation. unfortunately all my json example objects are stripped of indentation :). sooo… why strip indentation in the first place? html doesn’t care so is there any reason why it was implemented this way?