From 843ee9b60b125e1ffb4a0a336cf0800d62404279 Mon Sep 17 00:00:00 2001 From: Vladimir Chub Date: Mon, 31 Oct 2016 11:47:40 +0200 Subject: [PATCH] Add support of paragraphs in list item elements (#47) --- src/Converter/ListItemConverter.php | 13 ++++++++----- tests/HtmlConverterTest.php | 2 ++ 2 files changed, 10 insertions(+), 5 deletions(-) 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()