mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
implement URL auto-linking
This commit is contained in:
parent
5a563008aa
commit
ddc5b7e2dd
@ -733,29 +733,35 @@ class Parsedown
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# automatic link
|
if (strpos($text, '://') !== FALSE)
|
||||||
|
|
||||||
if (strpos($text, '<') !== FALSE and preg_match_all('/<((https?|ftp|dict):[^\^\s]+?)>/i', $text, $matches, PREG_SET_ORDER))
|
|
||||||
{
|
{
|
||||||
foreach ($matches as $matches)
|
switch (TRUE)
|
||||||
{
|
{
|
||||||
$url = $matches[1];
|
case preg_match_all('{<(https?:[/]{2}[^\s]+)>}i', $text, $matches, PREG_SET_ORDER):
|
||||||
|
case preg_match_all('{\b(https?:[/]{2}[^\s]+)\b}i', $text, $matches, PREG_SET_ORDER):
|
||||||
|
|
||||||
strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&', $url);
|
foreach ($matches as $matches)
|
||||||
|
{
|
||||||
|
$url = $matches[1];
|
||||||
|
|
||||||
$element = '<a href=":href">:text</a>';
|
strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&', $url);
|
||||||
$element = str_replace(':text', $url, $element);
|
|
||||||
$element = str_replace(':href', $url, $element);
|
|
||||||
|
|
||||||
# ~
|
$element = '<a href=":href">:text</a>';
|
||||||
|
$element = str_replace(':text', $url, $element);
|
||||||
|
$element = str_replace(':href', $url, $element);
|
||||||
|
|
||||||
$code = "\x1A".'$'.$index;
|
# ~
|
||||||
|
|
||||||
$text = str_replace($matches[0], $code, $text);
|
$code = "\x1A".'$'.$index;
|
||||||
|
|
||||||
$map[$code] = $element;
|
$text = str_replace($matches[0], $code, $text);
|
||||||
|
|
||||||
$index ++;
|
$map[$code] = $element;
|
||||||
|
|
||||||
|
$index ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
tests/data/url_autolinking.html
Normal file
1
tests/data/url_autolinking.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<p>Here's an autolink <a href="http://example.com">http://example.com</a>.</p>
|
1
tests/data/url_autolinking.md
Normal file
1
tests/data/url_autolinking.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
Here's an autolink http://example.com.
|
Loading…
Reference in New Issue
Block a user