Skip to content

Commit

Permalink
Make autolinking configurable (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tar-Minyatur authored Jul 1, 2020
1 parent a1ddbf1 commit 2742fe3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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:[email protected]">[email protected]</a>', '<[email protected]>');
$this->html_gives_markdown('<a href="mailto:[email protected]">[email protected]</a>', '<[email protected]>');

// 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 2742fe3

Please sign in to comment.