showdown icon indicating copy to clipboard operation
showdown copied to clipboard

List with an empty item becomes nested

Open mirka opened this issue 5 years ago • 2 comments

- one
- 
- three

is being rendered as a nested list:

<li>one</li>
<li>
  <ul>
    <li>three</li>
  </ul>
</li>

instead of three lis with the middle one being empty.

Here is the example in the GFM spec: https://github.github.com/gfm/#example-252

Related: #375

mirka avatar Jan 28 '19 11:01 mirka

Shwodown doesn't currently support empty list items, but will probably support in version 2.0

tivie avatar Feb 08 '19 17:02 tivie

As a workaround until there's a supported fix, I've implemented the following extension to remove any empty list items:

export const removeEmptyListItems = () => {
    return [{
        type: "lang",
        regex: /( {0,3}([*+-]|\d+[.])[ \t]+)(?![^\n]+?)/gm,
        replace: "",
    }]
};

This will trim something like this

1. 
2. two
3. 

to

2. two

EDIT: Here's another option if preserving the empty list item is the intention without screwing up styles

export const emptyListItems = () => {
    return [{
        type: "lang",
        filter: function(text) {
            return text.replace(/( {0,3}([*+-]|\d+[.])[ \t]+)(?![^\n]+?)/gm, "$&&nbsp;");;
        }
    }]
};

davidgolden avatar May 27 '20 16:05 davidgolden