From 94062cea691e39364a69761f4e86ee71dca1c080 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Sat, 3 Oct 2026 02:27:25 +0200 Subject: [PATCH] fix: use autolinks for URLs that contain an ampersand The link text goes through TextConverter, which escapes `&` to `&`, while the href is the decoded attribute value, so `$href === $text` never held for a URL with a query string like `?a=1&b=2`. Undo that escaping before comparing. Only `&` is undone: `<` and `>` cannot appear in an autolink, so those links keep the inline form. Fixes #248 --- src/Converter/LinkConverter.php | 2 +- tests/HtmlConverterTest.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Converter/LinkConverter.php b/src/Converter/LinkConverter.php index f0ba157..2b650a5 100644 --- a/src/Converter/LinkConverter.php +++ b/src/Converter/LinkConverter.php @@ -26,7 +26,7 @@ class LinkConverter implements ConverterInterface, ConfigurationAwareInterface if ($title !== '') { $markdown = '[' . $text . '](' . $href . ' "' . $title . '")'; - } elseif ($href === $text && $this->isValidAutolink($href)) { + } elseif ($href === \str_replace('&', '&', $text) && $this->isValidAutolink($href)) { $markdown = '<' . $href . '>'; } elseif ($href === 'mailto:' . $text && $this->isValidEmail($text)) { $markdown = '<' . $text . '>'; diff --git a/tests/HtmlConverterTest.php b/tests/HtmlConverterTest.php index e62247e..dd9bd6d 100644 --- a/tests/HtmlConverterTest.php +++ b/tests/HtmlConverterTest.php @@ -163,6 +163,8 @@ class HtmlConverterTest extends TestCase $this->assertHtmlGivesMarkdown('google.com', '[google.com](google.com)'); $this->assertHtmlGivesMarkdown('https://www.google.com', ''); $this->assertHtmlGivesMarkdown('ftp://files.example.com', ''); + $this->assertHtmlGivesMarkdown('https://example.com/?a=1&b=2', ''); + $this->assertHtmlGivesMarkdown('https://example.com/<b>', '[https://example.com/<b>](https://example.com/)'); $this->assertHtmlGivesMarkdown('test@example.com', ''); $this->assertHtmlGivesMarkdown('test+foo@example.bar-baz.com', '');