4

If you write code you usually start by making 4 spaces which starts a code block. This does not work when the first line is empty.

The line above this one should be empty and gray like the one below

But that does not work. Also not for the line below.

Is there a feature for this. I need to post a bit of code which has no text on line 1

2
  • 2
    What programming language? Is an empty line at the beginning or end of a code block significant? Commented Jun 2, 2014 at 16:30
  • Vim because the line numbering matters see this example Commented Jun 2, 2014 at 16:47

1 Answer 1

8

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 (&lt; and &amp;) 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 &lt;stdio.h&gt;
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.

1
  • 1
    It's not really a side-effect, it's more of a misconception from the ages of indenting and breaking lines for code readability. People are used to adding that line break and indentation in their code between the <pre> and where the pre-formatted text actually starts, so that it's easy to read in the raw HTML. But they don't realize that it also adds that line break and spacing to the actual rendering of the pre-formatted text. It looks ugly, but that's how it's supposed to work.
    – animuson StaffMod
    Commented Jun 2, 2014 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .