Skip to content

Commit

Permalink
Merge 69e1979 into a1ddbf1
Browse files Browse the repository at this point in the history
  • Loading branch information
Tar-Minyatur committed Jun 30, 2020
2 parents a1ddbf1 + 69e1979 commit 42e16e9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -141,6 +141,21 @@ $converter->getConfig()->setOption('hard_break', false); // default
$markdown = $converter->convert($html); // $markdown now contains "test \nline break"
```

### Autolinking options

By default, `a` tags are converted to the easiest possible link syntax, i.e. if no text or title is available, then the `<url>` syntax will be used rather than the full `[url](url)` syntax. Set `use_autolinks` to `false` to change this behavior to always use the full link syntax.

```php
$converter = new HtmlConverter();
$html = '<p><a href="https://thephpleague.com">https://thephpleague.com</a></p>';

$converter->getConfig()->setOption('use_autolinks', true);
$markdown = $converter->convert($html); // $markdown now contains "<https://thephpleague.com>"

$converter->getConfig()->setOption('use_autolinks', false); // default
$markdown = $converter->convert($html); // $markdown now contains "[https://google.com](https://google.com)"
```

### Passing custom Environment object

You can pass current `Environment` object to customize i.e. which converters should be used.
Expand Down
19 changes: 17 additions & 2 deletions src/Converter/LinkConverter.php
Expand Up @@ -2,10 +2,24 @@

namespace League\HTMLToMarkdown\Converter;

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

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

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

/**
* @param ElementInterface $element
*
Expand Down Expand Up @@ -52,7 +66,8 @@ public function getSupportedTags()
*/
private function isValidAutolink($href)
{
return preg_match('/^[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*/i', $href) === 1;
$useAutolinks = $this->config->getOption('use_autolinks');
return $useAutolinks && (preg_match('/^[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*/i', $href) === 1);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/HtmlConverter.php
Expand Up @@ -41,6 +41,7 @@ public function __construct($options = array())
'hard_break' => false, // Set to true to turn <br> into `\n` instead of ` \n`
'list_item_style' => '-', // Set the default character for each <li> in a <ul>. Can be '-', '*', or '+'
'preserve_comments' => false, // Set to true to preserve comments, or set to an array of strings to preserve specific comments
'use_autolinks' => true, // Set to true to use simple link syntax if possible. Will always use []() if set to false
);

$this->environment = Environment::createDefaultEnvironment($defaults);
Expand Down
5 changes: 5 additions & 0 deletions tests/HtmlConverterTest.php
Expand Up @@ -133,6 +133,11 @@ public function test_anchor()
$this->html_gives_markdown('<a href="ftp://files.example.com">ftp://files.example.com</a>', '<ftp://files.example.com>');
$this->html_gives_markdown('<a href="mailto:test@example.com">test@example.com</a>', '<test@example.com>');
$this->html_gives_markdown('<a href="mailto:test+foo@example.bar-baz.com">test+foo@example.bar-baz.com</a>', '<test+foo@example.bar-baz.com>');

// Autolinking can be toggled off
$this->html_gives_markdown('<a href="https://www.google.com">https://www.google.com</a>', '[https://www.google.com](https://www.google.com)', array('use_autolinks' => false));
$this->html_gives_markdown('<a href="https://www.google.com">Google</a>', '[Google](https://www.google.com)', array('use_autolinks' => false));
$this->html_gives_markdown('<a href="google.com">google.com</a>', '[google.com](google.com)', array('use_autolinks' => false));
}

public function test_horizontal_rule()
Expand Down

0 comments on commit 42e16e9

Please sign in to comment.