1

Code following a list, though not to be included in the list itself, is not correctly rendered, as shown below.

I have tried inserting linebreaks, to no avail. Inserting "dummy" text will help the renderer. Have I misunderstood the formatting?


The downvote as a duplicate is fair, as the original question is definitely the same. I should have searched meta for this problem.

However-- many (appreciated) answers have explained various workarounds or manual spacing adjustments. These suggestions overlook a problem I mentioned (in the comment in the sample code snippet): the {} (code) widget does not format the code (either as a list item or following the list) in this context. This is, IMO, a UI/UX bug (confusion which lead to this post, obviously). By comparison, if the B (bold) toolbar widget failed to bold the text in certain contexts, I think it would be recognized as a bug without much contention.

Here is a list.

  1. One

  2. Two

  3. Three

    module Foo

    Not formatted as code, despite four space indentation and trying the {} widget.

    end

Here is another list.

  1. One
  2. Two

Inserting this text fixes the renderer.

module Foo
  # Now formatted as code.
end
6
  • Thanks Jeff, I did want to specify the language, so this is a fine workaround; it also means I don't need to indent when pasting +1. I still think there is a bug lurking, because highlighting the text and using the {} toolbar widget should correctly format the code (however it wants to accomplish that, insert ```, or otherwise). May 6, 2021 at 13:01
  • Does inserting a <br> fix the problem for you? That's HTML for a line break.
    – Mast
    May 6, 2021 at 13:06
  • 1
    Does this answer your question? Markdown: Code directly after a list (and not as part of that list)
    – jonrsharpe
    May 6, 2021 at 13:13
  • @Mast It does, as does <p>, suggested elsewhere. Although either must be preceded and followed by an empty line. Lots of explanation provided in other answers and the FAQ, all of which I missed. :) May 6, 2021 at 13:18
  • 1
    @jonrsharpe It's helpful, thank you, lots of good formatting information in the answers there. I wish the {} widget button would simply do what it purports to do, however. May 6, 2021 at 13:19
  • 3
    The {} button is really just an "indent everything in the selection by four spaces" button, which is sometimes but not always the "code block" button.
    – jonrsharpe
    May 6, 2021 at 14:11

3 Answers 3

7

The system is unintuitive but I believe it works as designed. There’s a second meaning to indentation: “continue the list above” and this takes priority over indentation being interpreted as starting code.

You need to indent 8 spaces to get code in a list:

  1. One

  2. Two

  3. Three

    module Foo
    

Alternatively, use a code fence (indent it all 4 spaces to make it part of the list, otherwise it’s just a regular code block):

  1. One

  2. Two

  3. Three

    module Foo
    

Method 1:

 1. One
 2. Two
 3. Three

        module Foo

Method 2:

 1. One
 2. Two
 3. Three

    ~~~
    module Foo
    ~~~
1
  • 1
    Thanks for the effort. Jeff provided a workaround elsewhere, which is enough I suppose. I'm not going to play with all the spacing permutations. I agree there is a question of whether or not the code is to be included in the list and the meaning of the spacing as attempting to decide that. However, using the {} widget resulted in no code formatting whatsoever, either as a last list item or after the list. IMO, I shouldn't need to twiddle spaces. May 6, 2021 at 13:06
1

Try using 7 spaces:

  1. One

  2. Two

  3. Three

    code
    

Or, as raw:

Try using 7 spaces:

1. One
2. Two
3. Three

       code

This lines the code up with the list. To "reset" it back to the left border, use a <p> element:

  1. One
  2. Two
  3. Three

code

As raw:

1. One
2. Two
3. Three

<p>

    code

Alternatively, use three backticks at the start and end:

  1. One
  2. Two
  3. Three
code

And, as raw:

1. One
2. Two
3. Three

```
code
```
1

Bracket your code with lines of three backticks. If you specify the language on the first such line, you will also get syntax-based coloring for that language (in Stack Overflow).

  1. List item One
  2. List Item Two
Write-Host "This is an example of PowerShell code. In Stack Overflow, you'll get syntax coloring."
Write-Host "There is nothing but a single blank line between the list and ```powershell"

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