Skip to content

Commit

Permalink
Merge pull request #155 from Sylry/consecutive_lists_with_current_con…
Browse files Browse the repository at this point in the history
…fig_merge

Add option to have different styles for consecutive lists.
  • Loading branch information
colinodell authored May 19, 2018
2 parents e25879d + f29bad3 commit 0323370
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Converter/ListItemConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class ListItemConverter implements ConverterInterface, ConfigurationAwareInterfa
*/
protected $config;

/**
* @var string
*/
protected $listItemStyle;

/**
* @param Configuration $config
*/
Expand Down Expand Up @@ -45,7 +50,16 @@ public function convert(ElementInterface $element)

if ($list_type === 'ul') {
$list_item_style = $this->config->getOption('list_item_style', '-');
return $prefix . $list_item_style . ' ' . $value . "\n";
$list_item_style_alternate = $this->config->getOption('list_item_style_alternate');
if (!isset($this->listItemStyle)) {
$this->listItemStyle = $list_item_style_alternate ? $list_item_style_alternate : $list_item_style;
}

if ($list_item_style_alternate && $level == 0 && $element->getSiblingPosition() === 1) {
$this->listItemStyle = $this->listItemStyle == $list_item_style ? $list_item_style_alternate : $list_item_style;
}

return $prefix . $this->listItemStyle . ' ' . $value . "\n";
}

if ($list_type === 'ol' && $start = $element->getParent()->getAttribute('start')) {
Expand Down
1 change: 1 addition & 0 deletions tests/HtmlConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function test_lists()
$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");
$this->html_gives_markdown('<ol start="120"><li>Item A</li><li>Item B</li></ol>', "120. Item A\n121. Item B");
$this->html_gives_markdown('<ul><li>first item of first list</li><li>second item of first list</li></ul><ul><li>first item of second list</li></ul>', "- first item of first list\n- second item of first list\n\n* first item of second list", array('list_item_style_alternate' => '*'));
}

public function test_nested_lists()
Expand Down

0 comments on commit 0323370

Please sign in to comment.