738

In the following markdown code I want item 3 to start with list number 3. But because of the code block in between markdown starts this list item as a new list. Is there any way to prevent that behaviour?

Desired output:

1. item 1
2. item 2

```
Code block
```

3. item 3

Produced output:

  1. item 1
  2. item 2

Code block

  1. item 3
orschiro
  • 19,847
  • 19
  • 64
  • 95
  • 6
    Here's the [correct solution](http://stackoverflow.com/a/22138846/1269037). The triple backticks in the Macmade's answer actually just produce a `` HTML element with newlines in it, which is not syntax-highlightable, and has an extra blank line above visibly highlighted as code. – Dan Dascalescu Mar 03 '14 at 04:55
  • 1
    If you're really desperate, you could look up the HTML character codes for the numbers you wish to use (and for every subsequent item)... as in `29. this is list item number 29`. This may not work in all markdown parsers though. – Roy Tinker Oct 24 '16 at 23:35

16 Answers16

952

Use four spaces to indent content between bullet points

1. item 1
2. item 2

    ```
    Code block
    ```
3. item 3

Produces:

  1. item 1
  2. item 2

    Code block

  3. item 3
Levi Fuller
  • 13,631
  • 4
  • 38
  • 44
Macmade
  • 52,708
  • 13
  • 106
  • 123
  • 7
    Yes, indentation makes it clear to the parser that the code block belongs to the list item 2. Otherwise, it's just a normal paragraph, and will end the list. – Macmade Aug 06 '13 at 19:53
  • I believe I have found a slightly [more elegant solution](http://stackoverflow.com/a/22138846/1269037). The triple backticks actually just produce a `` with newlines in it, which is not syntax-highlightable, and show a blank newline above. – Dan Dascalescu Mar 03 '14 at 04:54
  • It even works with escaped backticks in the code block! I can't believe, though, that there is no "normal" way of doing this, without hacks. – Sergey Orshanskiy Aug 15 '14 at 22:32
  • 88
    But ... what if you don't want indentation there? Markdown has no solution, as far as I know. It's often a natural thing to start a list, then stop and provide some text that's a meta comment about the list--what we just did, and what's coming next--without the text being, logically, part of the list--and then continue the list. Markdown does not want us to do that. It's a form of thought that Markdown does not want people to express--that Markdown does not know how to express, that Markdown thinks is too ... free. Tools should follow thought. Sigh. I could write in HTML, or make PDFs. – Mars Oct 29 '14 at 04:24
  • Note that I had to use 4 spaces and couldn't use tab in my specific case for this to work. – Adam Johns Aug 23 '15 at 22:51
  • A link related to this question: https://gist.github.com/clintel/1155906#file-gistfile1-md – Emer Nov 28 '15 at 17:40
  • 2
    Doesn't seem to work at all for me. I have a simple numbered list, and indented and new-lined as shown above. Still restarts at 1. – Ray Feb 29 '16 at 10:56
  • 13
    In the few Markdown editors that I've tried (Bitbucket, Tumblr), this doesn't work -- it renders inline code instead of a properly indented code block. – thdoan Aug 19 '16 at 12:07
  • 2
    @10basetom for Bitbucket, I've found that intenting the code twice and removing the triple ticks more or less accomplishes the desired effect. The only issue is that then you can't specify language formatting, but apparently that's not part of the "official" markdown implementation anyhow. – Daniel Schaffer Jun 22 '17 at 20:41
  • 1
    Thanks, this work well on GitLab too! But the Eclipse Markdown Previewer is buggy (Oxygen Release v4.7.0). – SiZiOUS Jul 06 '18 at 07:35
  • Works in wordpress – Syaiful Nizam Yahya Jan 11 '19 at 16:11
  • 3
    In IntelliJ it makes a difference how many blank lines are at the bottom of each numbered block. If you leave 2 blank lines, it restarts numbering; leave only one and it continues. – Rhubarb Aug 21 '19 at 10:28
  • Same issue with standard HTML ol elements. – Andrew Truckle Mar 05 '20 at 11:19
  • good solution, but you only have to write "1." , "1." , "1." , "1." ... (never "2.","3.", "4."....) – Ryan Loggerythm Mar 20 '23 at 17:56
59

As an extension to existing answers. For those trying to continue a numbered list after something other than a code block. For example a second paragraph. Just indent the second paragraph by at least 1 space.

Markdown:

1. one
2. two

 three
3. four

Output:

  1. one

  2. two

    three

  3. four

Community
  • 1
  • 1
DavidT
  • 2,341
  • 22
  • 30
  • 1
    Sweet! I had a series of pseudo code blocks and MathJax equations. This was exactly what I needed. – xtian Nov 13 '16 at 20:53
  • Doesn't work on GitHub Wiki. Adding more spaces will at least make the numbering the same type as the numbers above tho. :rofl: – Chaim Eliyah Jun 05 '18 at 22:22
  • 1
    @ChaimEliyah Thats because im 99% sure the wiki doesn't use GitHub flavoured markdown (im sure I've come across this curiosity previously) – DavidT Jun 08 '18 at 09:19
  • This will also take care of situations where you have a list entry containing some text, some code, and finally more text before the end of the list entry. – Frotz Jan 30 '19 at 07:34
  • It's not working for me on IntelliJ but I don't know if it'll work on GitHub. – Alonso del Arte May 20 '21 at 21:44
  • It works at top level in GitLab/CommonMark, but not for nested lists – mike Jul 15 '22 at 23:06
  • 1
    This answer worked for me for a list with inline images, whereas the accepted answer did not. – Mary Hamlin Feb 27 '23 at 15:20
45

Notice how in Macmade's solution, you can see an extra line of code above the "Code block".

Here are two better solutions:

  1. Indent the code block by an extra 4 spaces (so usually 8, in this nested list example, 12). This will put the code in a <pre> element. On SO, you can even specify syntax highlight with a
    <!-- language: lang-js --> indented by 4 spaces (+1 here due to the nested list).

    1. item 1
    2. item 2

      Code.block('JavaScript', maybe)?
      
    3. item 3

  2. Or, just put the Code block within backticks and indent by 4 spaces (here, 1 extra because of the nested list). You'll get a regular indented text paragraph, with a <code> element inside it. This one you can't syntax-highlight:

    1. item 1
    2. item 2

      Code block

    3. item 3

Note: you can click "edit" on this answer to see the underlying Markdown code. No need to save ;)

Community
  • 1
  • 1
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • 41
    This is a solution for this particular problem, but it's not a general solution to the problem of restarting a Markdown list at the next number after some intervening text. It appears that there is no way to do this, which drives me crazy. Everything else about Markdown is great. – Mars Oct 29 '14 at 04:22
  • 1
    @Mars as you can see from [DavidT's answer](https://stackoverflow.com/a/36337068/5179394), the general solution is to indent the text with any number of spaces. For instance, adding one space instead of four will allow you to insert any intervening text without having to create a code block. – ClydeTheGhost Aug 30 '17 at 14:04
32

Macmade's solution doesn't work for me anymore on my Jekyll instance on Github Pages anymore but I found this solution on an issue for the kramdown github repo. For OP's example it would look like this:

1. item 1
2. item 2

```
Code block
```

{:start="3"}
3. item 3

Solved my issues handily.

KhalilRavanna
  • 5,768
  • 3
  • 28
  • 24
  • 6
    Unfortunately, this does not work with GitHub readme files. :( – Nostalg.io Feb 21 '17 at 04:15
  • Was excited to read this, but in my Fastpages (Jekyll Github Pages) blog that uses kramdown, that start code is simply ignored. I suspect it gets mangled by Liquid or nbdev. – sh37211 Sep 22 '20 at 23:41
26

If you use tab to indent the code block it will shape the entire block into one line. To avoid this you need to use html ordered list.

  1. item 1
  2. item 2

Code block

<ol start="3">
  <li>item 3</li>
  <li>item 4</li>
</ol>
user3505838
  • 351
  • 3
  • 8
20

If you happen to be using the Ruby gem redcarpet to render Markdown, you may still have this problem.

You can escape the numbering, and redcarpet will happily ignore any special meaning:

1\. Some heading

text text
text text

text text

2\. Some other heading

blah blah

more blah blah
fqxp
  • 7,680
  • 3
  • 24
  • 41
  • 2
    This is the only solution works for me: multiple line code blocks in ordered list, and the code still formatted correctly with newlines, and works on Github. – xjlin0 Feb 01 '22 at 14:15
  • Just opened a [PR](https://github.com/vmg/redcarpet/pull/741) with redcarpet to support broken-up ordered lists. Fingers crossed. – Dennis Hackethal Aug 25 '23 at 23:47
8

Source:

<span>1.</span> item 1<br/>
<span>2.</span> item 2
```
Code block
```
<span>3.</span> item 3

Result:

1. item 1
2. item 2

Code block

3. item 3

brillout
  • 7,804
  • 11
  • 72
  • 84
5

If you don't want the lines in between the list items to be indented, like user Mars mentioned in his comment, you can use pandoc's example_lists feature. From their docs:

(@)  My first example will be numbered (1).
(@)  My second example will be numbered (2).

Explanation of examples.

(@)  My third example will be numbered (3).
iuvbio
  • 600
  • 6
  • 23
  • This is the most universal solution to this problem. There's no need to indent and It even works if you have headings between the list elements. – lillemets Sep 26 '19 at 13:52
  • 1
    The `(@)` corresponds to a global continuing list (so there can be only one. Using `pandoc`'s `startnum` extension you can start an ordered list with the number you want and it just works. – Abid H. Mujtaba Jan 06 '20 at 21:15
  • This gets rendered as a literal '(@)' by kramdown, nothing more. – sh37211 Sep 22 '20 at 23:45
4

You can try to add a backslash (\) before the period (1\. item 1), which disables the list auto-numbering. Note: this will remove left side indentation.

1. item 1

def call_of_duty()
   return press_f()

3. item 3

print("fus ro dah")

7. item 7

print("Omae Wa Mou Shindeiru")

10. item 10


From the link source:

3\. Put on shoes
2\. Open door
1\. Step outside

renders

3. Put on shoes
2. Open door
1. Step outside
Dmitriy Zub
  • 1,398
  • 8
  • 35
4

In CommonMark Spec has a rules about this

1. foo
2. bar
3) baz

Generate this HTML

<ol>
<li>foo</li>
<li>bar</li>
</ol>
<ol start="3">
<li>baz</li>
</ol>
tarleb
  • 19,863
  • 4
  • 51
  • 80
Damon Abdiel
  • 3,313
  • 2
  • 18
  • 15
3

I solved this problem on Github separating the indented sub-block with a newline, for instance, you write the item 1, then hit enter twice (like if it was a new paragraph), indent the block and write what you want (a block of code, text, etc). More information on Markdown lists and Markdown line breaks.

Example:

  1. item one
  2. item two

    this block acts as a new paragraph, above there is a blank line

  3. item three

    some other code

  4. item four
2

Put the list numbers in parentheses instead of followed by a period.

(1) item 1
(2) item 2 code block (3) item 3

lomzher
  • 61
  • 1
  • 7
    This destroys the `
      ` and `
    1. ` elems and instead just wraps them in `

      ` tags. Additionally, you literally get `(1)`.

    – jmargolisvt Feb 12 '16 at 22:24
  • 2
    I think that was the intent. – Gal Jul 12 '16 at 17:26
  • It keeps `
      ` and `
    1. ` for me (using Pandoc via Hakyl)l. It seems to be the switch from `x.` form to `(x)` form that causes a new `start` parameter to be inserted into the `
    2. `.
    – Tom Ellis Mar 12 '22 at 18:11
2

Note that there are also a number of extensions available that will fix this behaviour for specific contexts of Markdown use.

For example, sane_lists extension of python-markdown (used in mkdocs, for example), will recognize numbers used in Markdown lists. You just need to enable this extension markdown.markdown(some_text, extensions=['sane_lists'])

Alex
  • 2,784
  • 2
  • 32
  • 46
2

If you want to have text aligned to preceding list item but avoid having "big" line break, use two spaces at the end of a list item and indent the text with some spaces.

Source: (dots are spaces ;-) of course)

1.·item1··
····This is some text
2.item2

Result:

  1. item1
    This is some text
  2. item2
Jarda
  • 566
  • 1
  • 9
  • 33
  • Thanks--although this is not strictly speaking an answer to the original question, it is an answer to what some people want to know when they find their way to this page. – Mars Feb 10 '22 at 18:46
2

When you indent with 4 spaces, you never have to increment your numbers from "1." to "2." to "3."...

Markdown:

1. item 1
1. item 2

    ```
    Code block
    ```
1. item 3

Produces this output:

1. item 1
2. item 2

    ```
    Code block
    ```
3. item 3
Ryan Loggerythm
  • 2,877
  • 3
  • 31
  • 39
1

My solution is very simple: don't use the dot space.

e.g.

1.apple

2.banana

3.cherry

4.drone

which produces:

1.apple

2.banana

3.cherry

4.drone

Siwei
  • 19,858
  • 7
  • 75
  • 95
  • This doesn't seems like a "very simple" solution to me, lets say you want to delete the 2nd item ( banana) now you have to rename all the items below it isn't it? – Nivethan Feb 18 '23 at 09:08
  • No, since it markdown, it will solve it for you and do an ordered list: https://carpentries-incubator.github.io/markdown-intro/06-lists/index.html – Lovisa Johansson Aug 29 '23 at 06:40