From c2616e726dbd4364c9a79ed0811df081dd52e0c9 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Wed, 15 Mar 2017 20:22:26 -0400 Subject: [PATCH 1/3] Only autolink if href pmeets CommonMark spec --- src/Converter/LinkConverter.php | 12 +++++++++++- tests/HtmlConverterTest.php | 6 ++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Converter/LinkConverter.php b/src/Converter/LinkConverter.php index d2f6da0..f0765f3 100644 --- a/src/Converter/LinkConverter.php +++ b/src/Converter/LinkConverter.php @@ -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 . ')'; @@ -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; + } } diff --git a/tests/HtmlConverterTest.php b/tests/HtmlConverterTest.php index 14be360..e7cc82a 100644 --- a/tests/HtmlConverterTest.php +++ b/tests/HtmlConverterTest.php @@ -107,6 +107,12 @@ public function test_anchor() $this->html_gives_markdown('Test', 'Test'); $this->html_gives_markdown('Test', '[Test](#nerd "Title")'); $this->html_gives_markdown('Test', '[Test](#nerd)'); + + // Autolinking + $this->html_gives_markdown('test', '[test](test)'); + $this->html_gives_markdown('google.com', '[google.com](google.com)'); + $this->html_gives_markdown('https://www.google.com', ''); + $this->html_gives_markdown('ftp://files.example.com', ''); } public function test_horizontal_rule() From 7795675df3044e2697f39a1cc8963cd8b1b82b44 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Wed, 15 Mar 2017 20:25:53 -0400 Subject: [PATCH 2/3] Update CHANGELOG in preparation for 4.4.1 release --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 453cde8..55d5b97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From df5fbfe780abaaf84a618673ee7ae43c8e52fabf Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Wed, 15 Mar 2017 20:34:32 -0400 Subject: [PATCH 3/3] Fix missing link --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55d5b97..0678644 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -188,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