Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiline code block in markdown adds unwanted tabs

today I'm implementing my page in nanoc (haml templates) and I wanted to write some posts in markdown, but when it goes to multiline code blocks something weird is happening - second line in code block has additional tabs. I've tried multiple markdown syntaxes, such as:

//double tab wrapping
    line 1 is fine
    line 2 is wrapping (don't know why!)

and

~~~
//tilde code wrapping
line 1 is fine
line 2 is wrapping
~~~

And both solutions gives me the result something like this:

line 1 is fine
    line 2 is wrapping

Inspecting elements through browser shows that there is no additional padding - this whitespace is made with tabs for sure.
Can someone help me with this? Maybe I'm doing something wrong?

like image 600
lukaszkups Avatar asked May 17 '13 09:05

lukaszkups


People also ask

How do you indent a code block in Markdown?

Indented Code Blocks To format a code block in Markdown, indent every line of the block by at least four spaces. An indented code block cannot interrupt a paragraph, so you must insert at least one blank line between a paragraph the indented code block that follows.

How do I use Python code in Markdown?

To add a Python code chunk to an R Markdown document, you can use the chunk header ```{python} , e.g., ```{python} print("Hello Python!") ```

How do I add a code snippet to MD?

There are two ways to format code in Markdown. You can either use inline code, by putting backticks (`) around parts of a line, or you can use a code block, which some renderers will apply syntax highlighting to.


1 Answers

When you use = in Haml to include the results of a script, Haml will re-indent the inserted text so that it matches the indentation of where it is included. For example, if you have Haml that looks something like this:

%html
  %body
    .foo
      = insert_something

and insert_something returns some HTML like this:

<p>
This is possily generated from Markdown.
</p>

then the resulting HTML will look like this:

<html>
  <body>
    <div class='foo'>
      <p>
      This is possily generated from Markdown.
      </p>
    </div>
  </body>
</html>

Note how the p element is indented to match its position in the document. Normally this doesn’t matter, because of the way whitespace in HTML is collapsed. However there are HTML elements where the whitespace is important, in particular pre.

What it looks like is happening here is that your Markdown is generating something like

<pre><code>line 1 is fine
line 2 is wrapping
</code></pre>

and when it is included in your Haml file (I’m guessing you’re using Haml layouts with = yield to include the Markdown) it is being indented and the whitespace is showing up when you view the page. Note how the first line is immediately after the opening tags so there is no extra whitespace.

There are a couple of ways to fix this. If you set the :ugly option then Haml won’t re-indent blocks like this (sorry I don’t know how you set Haml options in Nanoc).

You could also use the find_and_preserve helper method. This replaces all newlines in whitespace sensitive tags with HTML entity &#x000A;, so that they won’t be affected by the extra whitespace when indented:

= find_and_preserve(yield)

Haml provides an easy way to use find_and_preserve; ~ works the same as =, except that it runs find_and_preserve on the result, so you can do:

~ yield
like image 57
matt Avatar answered Sep 24 '22 23:09

matt