diff options
Diffstat (limited to 'vendor/league/html-to-markdown/src/Converter/ListItemConverter.php')
-rw-r--r-- | vendor/league/html-to-markdown/src/Converter/ListItemConverter.php | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/vendor/league/html-to-markdown/src/Converter/ListItemConverter.php b/vendor/league/html-to-markdown/src/Converter/ListItemConverter.php new file mode 100644 index 000000000..dafec077c --- /dev/null +++ b/vendor/league/html-to-markdown/src/Converter/ListItemConverter.php @@ -0,0 +1,47 @@ +<?php + +namespace League\HTMLToMarkdown\Converter; + +use League\HTMLToMarkdown\ElementInterface; + +class ListItemConverter implements ConverterInterface +{ + /** + * @param ElementInterface $element + * + * @return string + */ + public function convert(ElementInterface $element) + { + // If parent is an ol, use numbers, otherwise, use dashes + $list_type = $element->getParent()->getTagName(); + + // Add spaces to start for nested list items + $level = $element->getListItemLevel($element); + + $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"; + } + + if ($list_type === 'ul') { + return $prefix . '- ' . $value . "\n"; + } + + $number = $element->getSiblingPosition(); + + return $prefix . $number . '. ' . $value . "\n"; + } + + /** + * @return string[] + */ + public function getSupportedTags() + { + return array('li'); + } +} |