3 namespace League\HTMLToMarkdown\Converter;
5 use League\HTMLToMarkdown\Configuration;
6 use League\HTMLToMarkdown\ConfigurationAwareInterface;
7 use League\HTMLToMarkdown\ElementInterface;
9 class HeaderConverter implements ConverterInterface, ConfigurationAwareInterface
11 const STYLE_ATX = 'atx';
12 const STYLE_SETEXT = 'setext';
20 * @param Configuration $config
22 public function setConfig(Configuration $config)
24 $this->config = $config;
28 * @param ElementInterface $element
32 public function convert(ElementInterface $element)
34 $level = (int) substr($element->getTagName(), 1, 1);
35 $style = $this->config->getOption('header_style', self::STYLE_SETEXT);
37 if (($level === 1 || $level === 2) && !$element->isDescendantOf('blockquote') && $style === self::STYLE_SETEXT) {
38 return $this->createSetextHeader($level, $element->getValue());
41 return $this->createAtxHeader($level, $element->getValue());
47 public function getSupportedTags()
49 return array('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
54 * @param string $content
58 private function createSetextHeader($level, $content)
60 $length = function_exists('mb_strlen') ? mb_strlen($content, 'utf-8') : strlen($content);
61 $underline = ($level === 1) ? '=' : '-';
63 return $content . "\n" . str_repeat($underline, $length) . "\n\n";
68 * @param string $content
72 private function createAtxHeader($level, $content)
74 $prefix = str_repeat('#', $level) . ' ';
76 return $prefix . $content . "\n\n";