What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 130 Stack Exchange communities.

Why does markdown use a four-space indent instead of <code> or [code]?

The four-space indent can be quite irritating sometimes if you are pasting a fairly lengthy section of code and it messes up the indentation... is there a reason for using the indent?


Testing with <code> then <pre>

Class *instance = [[Class alloc] init];
[instance method];

Testing with <pre> then <code>


Class *instance = [[Class alloc] init];
[instance method];

Just <code>...

Class *instance = [[Class alloc] init]; [instance method];

Indent

Class *instance = [[Class alloc] init];
[instance method];
share|improve this question
4  
There's a {} button in the editor to indent a block at once. Even has a keyboard shortcut Ctrl+K (on PCs, not sure about Macs). –  Mat Nov 7 '11 at 11:06
7  
Markdown generally doesn't use named tags like that. It's not HTML. –  Keith Thompson Nov 7 '11 at 11:09

4 Answers 4

up vote 13 down vote accepted

One of the the design goals for Markdown is that the source as well as the rendered version should be in a readable style. For this reason it uses existing conventions for marking up plain text from things like email (such as using stars for bold/italics and using a footnote style notation for links.)

I believe this is the reason why indentation is used for code in preference to using a tag-style approach.

share|improve this answer

I've never had it "mess up the indentation" for me... paste it in, select the code and hit CTRL + K; have you got any examples?

The Markdown syntax is designed to be fluent for writers (i.e. * for bullets) etc, rather than the whole surrounding elements by <tags> malarky); hence the lack of [code] and <code> blocks

SO however, does accept the <pre> and <code> HTML tags, so you can use those to wrap your code, rather than using the Markdown syntax.

share|improve this answer

Your use of the HTML tags on their own lines causes problems

You wrote:

Testing with `<code>` then `<pre>`

<code>
<pre>
Class *instance = [[Class alloc] init];
[instance method];
</pre>
</code>

Testing with `<pre>` then `<code>`

<pre>
<code>
Class *instance = [[Class alloc] init];
[instance method];
</code>
</pre>

which produced:


Testing with <code> then <pre>

Class *instance = [[Class alloc] init];
[instance method];

Testing with <pre> then <code>


Class *instance = [[Class alloc] init];
[instance method];


Fixing the problems by removing line breaks

As you can see, this caused problems. Mostly, that the newline between the tags are being parsed and producing spurious line breaks. Instead, use one of the following methods:

Testing with `<code>` then `<pre>`:
<code><pre>Class *instance = [[Class alloc] init];
[instance method];
</pre></code>

Testing with `<pre>` then `<code>`:

<pre><code>Class *instance = [[Class alloc] init];
[instance method];
</code></pre>

which produces:


Testing with <code> then <pre>:

Class *instance = [[Class alloc] init];
[instance method];

Testing with <pre> then <code>:

Class *instance = [[Class alloc] init];
[instance method];

Additional remarks

Just <code> requires two spaces at the end of each line, so it's probably not useful.

Incedentally, the Markdown method causes the parser to generate the following HTML:

<pre><code>Class *instance = [[Class alloc] init];
[instance method];
</code></pre>

so you should probably use that method.

share|improve this answer

You know you can just highlight/select your longer code samples and push the code button ( {})on the toolbar?

share|improve this answer

You must log in to answer this question.

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