Indeed, the Markdown parser seems to treat any lines that only contain spaces and tabs as empty. Normally, I suspect this would be considered a feature: having the formatting of your post depend on whether or not a blank-looking line is actually empty or has spaces in it could get really annoying.
For the rare occasions where you do need to start or end a code block with a blank line, there are a couple of workarounds you can use:
Code fences
With the addition of code fences to the Markdown syntax available on Stack Exchange this problem is now very easy to solve. Writing either:
```
This line is in a fenced code block.
```
or:
~~~
This line is in a fenced code block.
~~~
produces:
This line is in a fenced code block.
HTML <pre>
tags:
Writing:
<pre>
This line is surrounded by blank lines.
</pre>
produces:
This line is surrounded by blank lines.
The main disadvantage of using <pre>
tags like this is that it does not support the syntax highlighting applied to normal code blocks. Of course, sometimes that may also be considered a good thing.
Also, if your code contains any HTML metacharacters (like <
or &
), you will have to replace them with the corresponding HTML character entities (<
and &
) inside <pre>
blocks.
Invisible characters:
Putting an invisible character on a line means it's no longer considered blank by the Markdown parser. For example:
This line is surrounded by lines containing a non-breaking space (U+00A0).
and:
This line is surrounded by lines containing a zero-width space (U+200B).
Ps. Actually, it seems that you can get syntax highlighting to work by wrapping your code in <pre><code>
and </code></pre>
, like this:
<!-- language: lang-c -->
<pre><code>#include <stdio.h>
int main (int argc, char *argv[]) {
printf("Hello, %s!\n", (argc > 1 ? argv[1] : "World"));
return 0;
}</code></pre>
which produces:
#include <stdio.h>
int main (int argc, char *argv[]) {
printf("Hello, %s!\n", (argc > 1 ? argv[1] : "World"));
return 0;
}
(The explicit language hint is necessary here on Meta, because the question has no tags that the highlighter could use to guess the correct language.) A curious side effect of doing this is that, to avoid an initial blank line, you have to start your code on the same line as the opening <code>
tag.