Writing In Markdown - Lists

April 20, 2012 by Gabe | [mmd] |

This is part two in the Writing in Markdown series. If you prefer, read John Gruber's original guide. I will not be able to add anything new.

Lists come in two flavors in Markdown. There are unordered lists and ordered lists. The first item in a list must be preceded by an empty line. A list item can contain other Markdown formatting, however the list bullet or item number can not. It must be plain text.

An unordered list is a simple bullet list. There's not much to an unordered list. It must be separated from a text block by one blank like and each list item must be preceded by a hyphen, plus or asterisk. I use hyphen characters ("-") exclusively for bullet lists. I have run into problems when using a mix of hyphens and asterisks with lists. Particularly, I have had issues when copying markdown into other applications.

To create nested lists, indent by one tab (or four spaces if you're antediluvian. Markdown processors will automatically vary the bullet character between list levels. That's the main reason it doesn't matter much whether I use an asterisk or dash for bullets.


- Talk to Luke about his father

    * Skip the part where I leave him for dead

    - Don't mention the youngling "thing"

- Dinner with Yoda

    - Bring DEET

    - **Bring Pepto**

    - Dessert?

        - Wookie Pie

- Stop by to see Anakin on Death Star

- Submit restraining order against JarJar

Is converted to this:

  • Talk to Luke about his father
    • Skip the part where I leave him for dead
    • Don't mention the youngling "thing"
  • Dinner with Yoda
    • Bring DEET
    • Bring Pepto
    • Dessert?
      • Wookie Pie
  • Stop by to see Anakin on Death Star
  • Submit restraining order against JarJar

But of course sometimes the order of items is crucial. That's where ordered lists come in. The sequence of an ordered list is defined by the sequence of the list items and not by the number prefix of the item

For example, even though the first line is prefixed as item #3 in the list, the Markdown is converted to display as item #1. The actual number is irrelevant. It is only there to indicate that the line should be considered as an ordered list item. Also note that unordered and ordered items can be commingled in the same list, but not at the same indentation level. An ordered list can contain a nested ordered list. But an unordered list items are converted to numbered list items if they are at the same indentation level as another numbered item.


3. Stop by to see Anakin on Death Star

    2. Get a ride

    1. Wash robe

- Clean blood (and hand) off Anakin's old light saber

1. Talk to Luke about his father

    * Skip the part where I leave him for dead

    - Don't mention the youngling "thing"

2. Dinner with Yoda

    - Bring DEET

    - **Bring Pepto**

    - Dessert?

        - Wookie Pie

4. Submit restraining order against JarJar

Is converted to the list below. The originally drafted sequence would be lost in converting the Markdown to html or PDF.

  1. Stop by to see Anakin on Death Star
    1. Get a ride
    2. Wash robe
  2. Clean blood (and hand) off Anakin's old light saber
  3. Talk to Luke about his father
    • Skip the part where I leave him for dead
    • Don't mention the youngling "thing"
  4. Dinner with Yoda
    • Bring DEET
    • Bring Pepto
    • Dessert?
      • Wookie Pie
  5. Submit restraining order against JarJar

As with all Markdown, these are just tags to interpret the start of a ul or ol html tag block. As long as the first item in a list, all subsequent list items will be interpreted as an ordered list. For example:


#### Pack Suitcase ####

1. Lightsaber

- Leisure brown robe

- Formal brown robe

- Night time brown robe

- Dress Sandals

Generates this formatted list

Pack Suitcase

  1. Lightsaber
  2. Leisure brown robe
  3. Formal brown robe
  4. Night time brown robe
  5. Dress Sandals

Unfortunately, Markdown never implemented an option to start an ordered (numbered) list at an arbitrary number. The only option I have found is to create the list manually by escaping the Markdown using a \<pre> or \<code> block. These blocks tell the Markdown processor to skip interpretation of the list all together.


2. Lightsaber

3. Leisure brown robe

4. Formal brown robe

5. Night time brown robe

6. Dress Sandals

To generate this list:


2. Lightsaber

3. Leisure brown robe

4. Formal brown robe

5. Night time brown robe

6. Dress Sandals

Alternatively, I can skip the \<pre> tag by indenting each list item with one tab (or four spaces). This just makes it a code block which also escapes the Markdown processor.


    2. Lightsaber

    3. Leisure brown robe

    4. Formal brown robe

    5. Night time brown robe

    6. Dress Sandals

There are many more details about extending lists to include multiple paragraphs as well as code blocks. I highly recommend reading from the original source as handed down by Gruber.

blog comments powered by Disqus