4

Why does Markdown use a four-space indent, instead of HTML5's <code> or BBCode's [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.. Consequently, is there a reason for using the indent?

Tests

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];

Indentation:

Class *instance = [[Class alloc] init];
[instance method];
3
  • 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
    Commented Nov 7, 2011 at 11:06
  • 10
    Markdown generally doesn't use named tags like that. It's not HTML. Commented Nov 7, 2011 at 11:09
  • OP, do know that you're not the sole person who despises that CommonMark defines indentation as renderable syntax - I do, too. Commented Aug 13 at 13:37

5 Answers 5

20

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.

7

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.

5

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.

1
  • Your answer inspired me to fix my problem - I just replace all newlines with <br> - that way, none of the "indentation spaces" can be misinterpreted as a four-space code block. Thanks!
    – rinogo
    Commented Mar 20 at 1:34
3

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

2

To answer the title's question, CommonMark contributors attempt to not include language-specific syntax declarations wherever possible.


Additionally, you've conflated (in the title) the inline <code> tag with the block <pre> tag: code is equivalent to <code>code</code>.

If you want to more explicitly declare a code block, use fences:

  1. ```MD
    Markdown `code`.
    ```
    
  2. ~~~MD
    Markdown `code`.
    ~~~
    

This shall render as 4 spaces does:

Markdown `code`.

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