diff --git a/src/commonmark-rules.js b/src/commonmark-rules.js index 77a4c529..23a7390c 100644 --- a/src/commonmark-rules.js +++ b/src/commonmark-rules.js @@ -50,6 +50,12 @@ rules.list = { replacement: function (content, node) { var parent = node.parentNode + if (node.parentNode.nodeName.match(/^(UL|OL)$/i)) { + content = ' ' + content + .replace(/^\n+/, '') // remove leading newlines + .replace(/\n+$/, '\n') // replace trailing newlines with just a single one + .replace(/\n/gm, '\n ') // indent + } if (parent.nodeName === 'LI' && parent.lastElementChild === node) { return '\n' + content } else { @@ -70,7 +76,7 @@ rules.listItem = { var parent = node.parentNode if (parent.nodeName === 'OL') { var start = parent.getAttribute('start') - var index = Array.prototype.indexOf.call(parent.children, node) + var index = Array.prototype.indexOf.call(Array.prototype.filter.call(parent.children, el => el.nodeName === 'LI'), node) prefix = (start ? Number(start) + index : index + 1) + '. ' } return ( diff --git a/test/index.html b/test/index.html index 26fa586c..89b9208f 100644 --- a/test/index.html +++ b/test/index.html @@ -523,6 +523,98 @@ * This is a third item at root level + +
+
+ +
+
*   This is a list item at root level
+*   This is another item at root level
+    1.  This is a nested list item
+    2.  This is another nested list item
+        *   This is a deeply nested list item
+        *   This is another deeply nested list item
+        *   This is a third deeply nested list item
+*   This is a third item at root level
+
+ +
+
+ +
+
*   This is a list item at root level
+*   This is another item at root level
+
+    1.  This is a nested list item
+    2.  This is another nested list item
+    
+        *   This is a deeply nested list item
+        *   This is another deeply nested list item
+        *   This is a third deeply nested list item
+    
+
+*   This is a third item at root level
+
+ +
+
+
    +
  1. item 1
  2. +
  3. item 2
  4. +
      +
    1. item 2.1
    2. +
    3. item 2.2
    4. +
        +
      1. item 2.2.1
      2. +
      3. item 2.2.2
      4. +
      5. item 2.2.3
      6. +
      +
    +
  5. item 3
  6. +
+
+
1.  item 1
+2.  item 2
+
+    1.  item 2.1
+    2.  item 2.2
+    
+        1.  item 2.2.1
+        2.  item 2.2.2
+        3.  item 2.2.3
+    
+
+3.  item 3
+
+