Copie de Tumblr sur Shaarli
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.2 KiB

  1. <?php
  2. namespace League\HTMLToMarkdown\Converter;
  3. use League\HTMLToMarkdown\ElementInterface;
  4. class ListItemConverter implements ConverterInterface
  5. {
  6. /**
  7. * @param ElementInterface $element
  8. *
  9. * @return string
  10. */
  11. public function convert(ElementInterface $element)
  12. {
  13. // If parent is an ol, use numbers, otherwise, use dashes
  14. $list_type = $element->getParent()->getTagName();
  15. $value = $element->getValue();
  16. // Add spaces to start for nested list items
  17. $level = $element->getListItemLevel($element);
  18. $prefix = str_repeat(' ', $level);
  19. // If list item is the first in a nested list, add a newline before it
  20. if ($level > 0 && $element->getSiblingPosition() === 1) {
  21. $prefix = "\n" . $prefix;
  22. }
  23. if ($list_type === 'ul') {
  24. $markdown = $prefix . '- ' . trim($value) . "\n";
  25. } else {
  26. $number = $element->getSiblingPosition();
  27. $markdown = $prefix . $number . '. ' . trim($value) . "\n";
  28. }
  29. return $markdown;
  30. }
  31. /**
  32. * @return string[]
  33. */
  34. public function getSupportedTags()
  35. {
  36. return array('li');
  37. }
  38. }