Skip to content

Commit

Permalink
Added hard_break option for GFM line breaks (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
IllyaMoskvin committed Dec 9, 2016
1 parent 20d2e21 commit f683899
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -108,6 +108,21 @@ $html = '<em>Italic</em> and a <strong>bold</strong>';
$markdown = $converter->convert($html); // $markdown now contains "_Italic_ and a __bold__"
```

### Line break options

By default, `br` tags are converted to two spaces followed by a newline character as per [traditional Markdown](https://daringfireball.net/projects/markdown/syntax#p). Set `hard_break` to `true` to omit the two spaces, as per GitHub Flavored Markdown (GFM).

```php
$converter = new HtmlConverter();
$html = '<p>test<br>line break</p>';

$converter->getConfig()->setOption('hard_break', true);
$markdown = $converter->convert($html); // $markdown now contains "test\nline break"

$converter->getConfig()->setOption('hard_break', false); // default
$markdown = $converter->convert($html); // $markdown now contains "test \nline break"
```

### Limitations

- Markdown Extra, MultiMarkdown and other variants aren't supported – just Markdown.
Expand Down
19 changes: 17 additions & 2 deletions src/Converter/HardBreakConverter.php
Expand Up @@ -2,18 +2,33 @@

namespace League\HTMLToMarkdown\Converter;

use League\HTMLToMarkdown\Configuration;
use League\HTMLToMarkdown\ConfigurationAwareInterface;
use League\HTMLToMarkdown\ElementInterface;

class HardBreakConverter implements ConverterInterface
class HardBreakConverter implements ConverterInterface, ConfigurationAwareInterface
{
/**
* @var Configuration
*/
protected $config;

/**
* @param Configuration $config
*/
public function setConfig(Configuration $config)
{
$this->config = $config;
}

/**
* @param ElementInterface $element
*
* @return string
*/
public function convert(ElementInterface $element)
{
return " \n";
return $this->config->getOption('hard_break') ? "\n" : " \n";
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/HtmlConverter.php
Expand Up @@ -35,6 +35,7 @@ public function __construct(array $options = array())
'bold_style' => '**', // Set to '__' if you prefer the underlined style
'italic_style' => '_', // Set to '*' if you prefer the asterisk style
'remove_nodes' => '', // space-separated list of dom nodes that should be removed. example: 'meta style script'
'hard_break' => false, // Set to true to turn <br> into `\n` instead of ` \n`
);

$this->environment = Environment::createDefaultEnvironment($defaults);
Expand Down
4 changes: 4 additions & 0 deletions tests/HtmlConverterTest.php
Expand Up @@ -38,6 +38,10 @@ public function test_line_breaks()
$this->html_gives_markdown('<p>test<br/>another line</p>', "test \nanother line");
$this->html_gives_markdown('<p>test<br />another line</p>', "test \nanother line");
$this->html_gives_markdown('<p>test<br />another line</p>', "test \nanother line");
$this->html_gives_markdown('<p>test<br>another line</p>', "test\nanother line", array('hard_break' => true));
$this->html_gives_markdown('<p>test<br/>another line</p>', "test\nanother line", array('hard_break' => true));
$this->html_gives_markdown('<p>test<br />another line</p>', "test\nanother line", array('hard_break' => true));
$this->html_gives_markdown('<p>test<br />another line</p>', "test\nanother line", array('hard_break' => true));
}

public function test_headers()
Expand Down

0 comments on commit f683899

Please sign in to comment.