Skip to content

Commit

Permalink
Merge pull request #129 from thephpleague/fix-autolinks
Browse files Browse the repository at this point in the history
Only autolink if href meets CommonMark spec
  • Loading branch information
colinodell authored Mar 16, 2017
2 parents 1810d3a + df5fbfe commit 82ea375
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

## [Unreleased][unreleased]

## [4.4.1]

### Fixed
- Fixed autolinking of invalid URLs (#129)

## [4.4.0]

### Added
Expand Down Expand Up @@ -183,7 +188,8 @@ not ideally set, so this releases fixes that. Moving forwards this should reduce
### Added
- Initial release

[unreleased]: https://github.com/thephpleague/html-to-markdown/compare/4.4.0...master
[unreleased]: https://github.com/thephpleague/html-to-markdown/compare/4.4.1...master
[4.4.1]: https://github.com/thephpleague/html-to-markdown/compare/4.4.0...4.4.1
[4.4.0]: https://github.com/thephpleague/html-to-markdown/compare/4.3.1...4.4.0
[4.3.1]: https://github.com/thephpleague/html-to-markdown/compare/4.3.0...4.3.1
[4.3.0]: https://github.com/thephpleague/html-to-markdown/compare/4.2.2...4.3.0
Expand Down
12 changes: 11 additions & 1 deletion src/Converter/LinkConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function convert(ElementInterface $element)

if ($title !== '') {
$markdown = '[' . $text . '](' . $href . ' "' . $title . '")';
} elseif ($href === $text) {
} elseif ($href === $text && $this->isValidAutolink($href)) {
$markdown = '<' . $href . '>';
} else {
$markdown = '[' . $text . '](' . $href . ')';
Expand All @@ -39,4 +39,14 @@ public function getSupportedTags()
{
return array('a');
}

/**
* @param string $href
*
* @return bool
*/
private function isValidAutolink($href)
{
return preg_match('/^[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*/i', $href) === 1;
}
}
6 changes: 6 additions & 0 deletions tests/HtmlConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public function test_anchor()
$this->html_gives_markdown('<a href="">Test</a>', '<a href="">Test</a>');
$this->html_gives_markdown('<a href="#nerd" title="Title">Test</a>', '[Test](#nerd "Title")');
$this->html_gives_markdown('<a href="#nerd">Test</a>', '[Test](#nerd)');

// Autolinking
$this->html_gives_markdown('<a href="test">test</a>', '[test](test)');
$this->html_gives_markdown('<a href="google.com">google.com</a>', '[google.com](google.com)');
$this->html_gives_markdown('<a href="https://www.google.com">https://www.google.com</a>', '<https://www.google.com>');
$this->html_gives_markdown('<a href="ftp://files.example.com">ftp://files.example.com</a>', '<ftp://files.example.com>');
}

public function test_horizontal_rule()
Expand Down

0 comments on commit 82ea375

Please sign in to comment.