diff --git a/src/Converter/ListItemConverter.php b/src/Converter/ListItemConverter.php index 9c44774..77dc988 100644 --- a/src/Converter/ListItemConverter.php +++ b/src/Converter/ListItemConverter.php @@ -15,21 +15,24 @@ public function convert(ElementInterface $element) { // If parent is an ol, use numbers, otherwise, use dashes $list_type = $element->getParent()->getTagName(); - $value = $element->getValue(); // Add spaces to start for nested list items $level = $element->getListItemLevel($element); - $prefix = str_repeat(' ', $level); + + $prefixForParagraph = str_repeat(' ', $level + 1); + $value = trim(implode("\n" . $prefixForParagraph, explode("\n", trim($element->getValue())))); + // If list item is the first in a nested list, add a newline before it + $prefix = ''; if ($level > 0 && $element->getSiblingPosition() === 1) { - $prefix = "\n" . $prefix; + $prefix = "\n"; } if ($list_type === 'ul') { - $markdown = $prefix . '- ' . trim($value) . "\n"; + $markdown = $prefix . '- ' . $value . "\n"; } else { $number = $element->getSiblingPosition(); - $markdown = $prefix . $number . '. ' . trim($value) . "\n"; + $markdown = $prefix . $number . '. ' . $value . "\n"; } return $markdown; diff --git a/tests/HtmlConverterTest.php b/tests/HtmlConverterTest.php index d8d1aef..d019394 100644 --- a/tests/HtmlConverterTest.php +++ b/tests/HtmlConverterTest.php @@ -107,9 +107,11 @@ public function test_lists() { $this->html_gives_markdown('', "- Item A\n- Item B\n- Item C"); $this->html_gives_markdown('', "- Item A\n- Item B"); + $this->html_gives_markdown('', "- ### Item A\n \n Description\n- Item B"); $this->html_gives_markdown('
  1. Item A
  2. Item B
', "1. Item A\n2. Item B"); $this->html_gives_markdown("
    \n
  1. Item A
  2. \n
  3. Item B
  4. \n
", "1. Item A\n2. Item B"); $this->html_gives_markdown('
  1. Item A
  2. Item B
', "1. Item A\n2. Item B"); + $this->html_gives_markdown('
  1. Item A

    Description

  2. Item B
', "1. ### Item A\n \n Description\n2. Item B"); } public function test_nested_lists()