diff --git a/src/markdown.ts b/src/markdown.ts index 3978227..4644699 100644 --- a/src/markdown.ts +++ b/src/markdown.ts @@ -1,13 +1,20 @@ function indexInList(li: Element): number { - if (li.parentNode === null || !(li.parentNode instanceof HTMLElement)) throw new Error() + const parent = li.parentNode - const ref = li.parentNode.children + if (parent === null || !(parent instanceof HTMLElement)) throw new Error() + + let start = 0 + if (parent instanceof HTMLOListElement && parent.start !== 1) { + start = parent.start - 1 + } + + const ref = parent.children for (let i = 0; i < ref.length; ++i) { if (ref[i] === li) { - return i + return start + i } } - return 0 + return start } // Skip processing links that only link to the src of image within. diff --git a/test/test.js b/test/test.js index 570ffe5..9089c7b 100644 --- a/test/test.js +++ b/test/test.js @@ -81,10 +81,10 @@ describe('quote-selection', function () {

This should not appear as part of the quote.

-
+

This is beautifully formatted text that even has some inline code.

This is a simple p line

-

some escaped html tags to ignore <pre> <strong> <weak> <em> </pre> </strong> </weak> </em>

+

some escaped html tags to ignore <pre> <strong> <weak> <em> </pre> </strong> </weak> </em>

foo(true)

Links and :emoji: are preserved.

Music changes, and I'm gonna change right along with it.
--Aretha Franklin

@@ -139,5 +139,62 @@ describe('quote-selection', function () { assert.match(textarea.value, /^> @links and :emoji: are preserved\./m) }) + + it('preserves list order', function () { + document.getElementById('comment-body').innerHTML = ` +
    +
  1. Top level list one +
      +
    • +
        +
      1. sublist one
      2. +
      +
    • +
    • +
        +
      1. sublist two
      2. +
      +
    • +
    • +
        +
      1. sublist three
      2. +
      +
    • +
    +
  2. +
  3. Top level list two
  4. +
  5. Top level list three +
      +
    1. sublist one
    2. +
    3. sublist two
    4. +
    5. sublist three
    6. +
    +
  6. +
+` + + const quote = new MarkdownQuote('.comment-body') + quote.select(document.querySelector('.comment-body')) + assert.ok(quote.closest('[data-quote]')) + const textarea = document.querySelector('textarea') + quote.insert(textarea) + + assert.equal( + textarea.value.replace(/ +\n/g, '\n'), + `> 1. Top level list one +> +> * 1. sublist one +> * 2. sublist two +> * 5. sublist three +> 2. Top level list two +> 3. Top level list three +> +> 1. sublist one +> 2. sublist two +> 3. sublist three + +` + ) + }) }) })