From bb1cf8464a5860f43390b812594f5e9606959e52 Mon Sep 17 00:00:00 2001 From: appel Date: Tue, 25 Oct 2022 18:13:52 -0400 Subject: [PATCH] Added a switch to enable autolinking of links. As discussed in https://github.com/michelf/php-markdown/issues/326. It's not pretty but it seems to work. --- Michelf/Markdown.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Michelf/Markdown.php b/Michelf/Markdown.php index 746a2d0..5dbe490 100644 --- a/Michelf/Markdown.php +++ b/Michelf/Markdown.php @@ -65,6 +65,11 @@ public static function defaultTransform(string $text): string { public bool $no_markup = false; public bool $no_entities = false; + /** + * Change to `true` to enable autolinking without the need for angle brackets + * @var boolean + */ + public bool $autolinks = false; /** * Change to `true` to enable line breaks on \n without two trailling spaces @@ -1606,7 +1611,16 @@ protected function encodeAmpsAndAngles($text) { * @return string */ protected function doAutoLinks($text) { - $text = preg_replace_callback('{<((https?|ftp|dict|tel):[^\'">\s]+)>}i', + if ($this->autolinks) + { + $regex = '{((https?|ftp|dict|tel):[^\'">\s]+)}i'; + } + else + { + $regex = '{<((https?|ftp|dict|tel):[^\'">\s]+)>}i'; + } + + $text = preg_replace_callback($regex, array($this, '_doAutoLinks_url_callback'), $text); // Email addresses: