Skip to content

Commit

Permalink
Merge pull request #110 from ChubV/47_list_item_paragraphs
Browse files Browse the repository at this point in the history
Add support of paragraphs in list item elements (#47)
  • Loading branch information
colinodell committed Dec 9, 2016
2 parents 8179899 + 843ee9b commit 5283e61
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Converter/ListItemConverter.php
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/HtmlConverterTest.php
Expand Up @@ -119,9 +119,11 @@ public function test_lists()
{
$this->html_gives_markdown('<ul><li>Item A</li><li>Item B</li><li>Item C</li></ul>', "- Item A\n- Item B\n- Item C");
$this->html_gives_markdown('<ul><li> Item A</li><li> Item B</li></ul>', "- Item A\n- Item B");
$this->html_gives_markdown('<ul><li> <h3> Item A</h3><p>Description</p></li><li> Item B</li></ul>', "- ### Item A\n \n Description\n- Item B");
$this->html_gives_markdown('<ol><li>Item A</li><li>Item B</li></ol>', "1. Item A\n2. Item B");
$this->html_gives_markdown("<ol>\n <li>Item A</li>\n <li>Item B</li>\n</ol>", "1. Item A\n2. Item B");
$this->html_gives_markdown('<ol><li> Item A</li><li> Item B</li></ol>', "1. Item A\n2. Item B");
$this->html_gives_markdown('<ol><li> <h3> Item A</h3><p>Description</p></li><li> Item B</li></ol>', "1. ### Item A\n \n Description\n2. Item B");
}

public function test_nested_lists()
Expand Down

0 comments on commit 5283e61

Please sign in to comment.