70

Possible Duplicate:
Code block is not properly formatted when placed immediately after a list item

In this post about nesting code-blocks in lists, it is explained that if you want to nest a code-block in a list, you need to indent your code 8 spaces. This will allow you to include the code block as part of a list item. Is there any way to place code blocks after lists without having them use the same indentation level as the list that came before it?

The only solution I've found is to place a single character somewhere between the list item and the code block so it "switches off" the list formatting mode. Something like this:

  1. list item 1  
  2. list item 2

.

    string myVariable = "hello world"

Which will output like this:

  1. list item 1
  2. list item 2

.

string myVariable = "hello world"

Without the period I'd have to indent my code 8 spaces, which would put the code block at the same level as the list item. This isn't a major problem, but I was just wondering if there was a better way to get Markdown to do what I want it to.

Is there any way to force the "list item" formatting mode to switch off. It seems like the "list mode" will stay enabled until a line is reached that starts with a character other than a space.

2
  • Voting to reopen. This one is about putting code blocks after lists with the knowledge about 4-space indentation (the dupe source), and should not be considered a dupe. Jun 24, 2019 at 12:43
  • Agreed. This question is actually asking the opposite of what the supposed "original" question is asking. That said, this question already has a correct answer. Jun 28, 2019 at 2:57

2 Answers 2

47

A hacky way to get it displayed correctly is to replace the dot in your example with a Html-tag that won't be rendered to anything visible:

- list
- items

<b></b>

    code

This is then rendered like this:

  • list
  • items

code
1
  • 12
    That's a good idea. I would use HTML comments <!-- -->, but that works great! I just wish there was a more intuitive option available... Jan 1, 2010 at 21:00
41

code block after a list:

  • this
  • is a
  • list
this is a code block

markup:

- this
- is a
- list

<!-- well hey there sailor -->

    this is a code block
5
  • 1
    the usual caveat .. you must manually escape any < or > in the code block. Using a HTML comment as a "paragraph break" is probably easier. Jan 2, 2010 at 3:01
  • This is a fine work around for those that know about it, but this is still a bug, any chance on getting it fixed?
    – Sam
    Feb 25, 2011 at 2:10
  • 2
    @sam not a bug, this is part of the Markdown spec. Go look it up if you don't believe me. Feb 25, 2011 at 2:27
  • @Sam: See balpha's explanation here. Jul 24, 2011 at 9:27
  • 2
    Shouldn't there be commas after "well" and "there"? Nov 27, 2014 at 19:33

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