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.

Consider the following piece of Markdown code:

The is some regular text.

    >>> def factorial(n):
    ...     return 1 if n < 2 else n * factorial(n - 1)
    ... 

* This is a list item.

    >>> def factorial(n):
    ...     return 1 if n < 2 else n * factorial(n - 1)
    ... 

Notice that the second code block is preceded by a list item.

Why is the factorial code not properly formatted under these circumstances? (At least not during preview; when I hit the Ask Question button in a minute we'll know how it shows up in practice.)

To demonstrate the problem, I'll insert the Markdown code show above:


The is some regular text.

>>> def factorial(n):
...     return 1 if n < 2 else n * factorial(n - 1)
... 
  • This is a list item

    def factorial(n): ... return 1 if n < 2 else n * factorial(n - 1) ...


This applies to numbered as well as unnumbered lists.

share|improve this question
    
I think you need to revisit this question and change the accepted answer. –  Mark Ransom Oct 3 '12 at 15:12
    
@Mark: You're right. Balpha's answer is more informative and provides a more elegant work-around. So, changed it :) –  Stephan202 Oct 3 '12 at 16:27
    
Otiel's answer reflects the current documentation, so it should probably be the accepted answer, regardless of the current votes. –  Mogsdad May 13 '13 at 1:53

6 Answers 6

up vote 97 down vote accepted

Unfortunately, this is an ambiguity in the Markdown specification. This markdown source:

42. list item

    more text

can mean two things. One of them is the one you're expecting: more text is following a blank line and indented by four spaces, making it a top-level (i.e. not part of the list) code block.

The second possible meaning is this:

List items may consist of multiple paragraphs. Each subsequent paragraph in a list item must be indented by either 4 spaces or one tab:

1.  This is a list item with two paragraphs. Lorem ipsum dolor
    sit amet, consectetuer adipiscing elit. Aliquam hendrerit
    mi posuere lectus.

    Vestibulum enim wisi, viverra nec, fringilla in, laoreet
    vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
    sit amet velit.

– in other words, more text is a regular second paragraph within the list item.

It's indistinguishable which of the two meanings is the intended one; both are correct. And what the Markdown converter (actually, all of them) chooses is the second meaning.

The only way to fix this would be adding an additional syntax to Markdown, enabling you to distinguish the two. I can, however, not think of any intutitive and unobtrusive thing to do, so using one of the workarounds here is as good as anything. My preferred workaround, by the way, would be an HTML comment like this:

* list item

<!-- -->

    code

turns into this:

  • list item

code

– but it doesn't really matter which one you use. Bottom line is: As sad as it is, this is unfixable.

share|improve this answer
5  
Great explanation! –  Hendrik Vogt Jul 24 '11 at 9:19
2  
I've been throwing in &nbsp; inbetween when this became an issue. I'll be doing this from now on. –  Jeff Mercado Jul 24 '11 at 10:26
2  
I guess Markdown converters choosing the second meaning is the best of the two, as then one could still use the above-mentioned workarounds to force it to not do that. If a converter would NOT add it to the list item itself, there would be no way to force it to do so? (Even two spaces at the end of the first line would not help, as then the next lines would not be formatted as code.) –  Arjan Jul 24 '11 at 14:08
3  
The <!-- --> fix was very helpful, thanks. :) –  Shmiddty Jan 18 '13 at 22:20
    
This don't work when I put two ordered list items and code for them, even if you put number 1. to first and 2. to second, you got two items 1. –  jcubic Dec 4 '13 at 7:18
3  
@jcubic In that case (which is the usual case), you want the code to appear inside the list, not after it. That's absolutely possible by indenting the code eight spaces (four for the list and four to make it code). This question is only about the rare case that you want code to appear immediately below the list, but outside it. –  balpha Dec 4 '13 at 7:22

I've noticed that I usually have to add 8 spaces before any code instead of 4 spaces when it's code that appears as part of a list item.

<- 4 spaces
  • A list item (one blank line after this):

    <- 8 spaces
    
share|improve this answer
6  
Aha. So then Markdown treats the code as part of the list item. A slight semantic difference. Also, I suppose then that the functionality of ctrl-k should be upgraded such that it cycles between 0, 4 and 8 spaces (instead of only 0 and 4). Maybe also 12 spaces, for nested lists. Perhaps that could be a separate feature request? –  Stephan202 Jul 7 '09 at 19:08
1  
This seems to be true even if you don't intend for the code to be part of a bullet. I was trying to post a question with 2 bulleted questions followed by the code. –  IAbstract Feb 14 '10 at 5:00
1  
It's not just 8 spaces, it's 4 spaces plus 4 per indentation level. (At least, that's how it is currently.) –  badp Jul 25 '10 at 18:56
    
@Stephan202 - did you ever put in a separate feature request for this? –  JoshDM Aug 29 '13 at 15:47
    
@JoshDM: Nope, I did not. Feel free to do so! –  Stephan202 Aug 30 '13 at 9:00
    
This workaround is just perfect. –  Tomas Mar 18 at 20:00
    
It worked perfectly. –  Kamonwat Sangudsub Jul 21 at 9:33

The workaround which is to add 8 spaces instead of simply 4 is actually written in the markdown editing help:

  1. (...)
  2. (...)
  3. (...)
  4. Preformatted text in a list item:

    Skip a line and indent eight spaces.
    That's four spaces for the list
    and four to trigger the code block.
    
share|improve this answer
4  
Adding 8 spaces turns it into a code block within the list. The desire is usually to end the list instead. –  Mark Ransom Oct 3 '12 at 15:09

It's a crappy work-around, but you can do insert a backticked space to trick it. Put a space into those backticks and it should work:

  • This is a list item.

``

>>> def factorial(n):
...     return 1 if n < 2 else n * factorial(n - 1)
...

See?

  • This is a list item.

>>> def factorial(n):
...     return 1 if n < 2 else n * factorial(n - 1)
...
share|improve this answer
    
Aha, nice hack :) –  Stephan202 Jul 7 '09 at 19:09
6  
This is only appropriate if the code block doesn't have anything to do with the list item. –  Brad Gilbert Jul 21 '09 at 16:21
    
+1 I was getting ready to ask a question about formatting R code when I saw this. The 8 spaces hack did it. –  DaveParillo Nov 6 '09 at 5:21
2  
@Brad Gilbert - this is a common scenario if you want to have a list of points followed immediately by a code sample that is related to the answer in general and not to the final list item. (i.e. probably most of the time.) –  GalacticCowboy Jun 29 '10 at 15:09
2  
I use a horizontal rule, which has the same effect but actually blends in a little better. –  GalacticCowboy Jun 29 '10 at 15:10
    
balpha's answer is better because it doesn't leave extra spacing or other undesired characters. –  Mark Ransom Oct 3 '12 at 15:11

I'm glad there are workarounds for this, but this is really a bug that should be fixed. I just wasted altogether too much time trying to figure out why I couldn't put a code block after...

  1. Bullet 1.
  2. Bullet 2.
  3. Paste in the following code:

    this.IsCode = true;

So here's my attempt with 8 spaces.

  1. Bullet 1.
  2. Bullet 2.
  3. Paste in the following code:

    this.IsCode = true;
    this.IsAlsoCode = true;
    
  4. Okay, so every line of code needs to be proceded by 8 spaces (instead of the usual 4).

share|improve this answer

I just found a solution for this.

If you put a </ul> after the listed item. The issue will be fixed.

Issue fix

share|improve this answer
4  
You can also put </foobar> there, it will have the same result. Or use a comment, as I said in my answer. Your version has the disadvantage that it can a) cause subtle errors, and b) it's totally non-obvious to someone editing your question. –  balpha Jun 21 '13 at 12:55
    
@balpha: yeah that is correct. Thanks for the comment. –  Midhun MP Jun 21 '13 at 13:09

You must log in to answer this question.

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