mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Better autolinks
This doesn't follow gfm spec yet, work lifted from my code over
at
8505e2737e/src/Parsers/GitHubFlavor/Inlines/AutoLink.php
Fixes #505
Fixes #717
This commit is contained in:
parent
cc5614bc5c
commit
7d1b9ca562
@ -14,6 +14,9 @@ final class Url implements BacktrackingInline
|
||||
{
|
||||
use WidthTrait;
|
||||
|
||||
private const ABSOLUTE_URI = '[a-z][a-z0-9+.-]{1,31}:[^\s[:cntrl:]<>]*';
|
||||
private const NO_TRAILING_PUNCT = '(?<![?!.,:*_~])';
|
||||
|
||||
/** @var string */
|
||||
private $url;
|
||||
|
||||
@ -38,14 +41,31 @@ final class Url implements BacktrackingInline
|
||||
*/
|
||||
public static function build(Excerpt $Excerpt, State $State)
|
||||
{
|
||||
if (\preg_match(
|
||||
'/(?<=^|\s|[*_~(])https?+:[\/]{2}[^\s<]+\b\/*+/ui',
|
||||
$Excerpt->context(),
|
||||
$matches,
|
||||
\PREG_OFFSET_CAPTURE
|
||||
)) {
|
||||
// this needs some work to follow spec
|
||||
if (
|
||||
\preg_match(
|
||||
'/'.self::ABSOLUTE_URI.self::NO_TRAILING_PUNCT.'/iu',
|
||||
$Excerpt->context(),
|
||||
$matches,
|
||||
\PREG_OFFSET_CAPTURE
|
||||
)
|
||||
) {
|
||||
/** @var array{0: array{string, int}} $matches */
|
||||
return new self($matches[0][0], \intval($matches[0][1]));
|
||||
$url = $matches[0][0];
|
||||
$position = \intval($matches[0][1]);
|
||||
|
||||
if (\preg_match('/[)]++$/', $url, $matches)) {
|
||||
$trailingParens = \strlen($matches[0]);
|
||||
|
||||
$openingParens = \substr_count($url, '(');
|
||||
$closingParens = \substr_count($url, ')');
|
||||
|
||||
if ($closingParens > $openingParens) {
|
||||
$url = \substr($url, 0, -\min($trailingParens, $closingParens - $openingParens));
|
||||
}
|
||||
}
|
||||
|
||||
return new self($url, $position);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -1,3 +1,9 @@
|
||||
<p>an autolink <a href="http://example.com">http://example.com</a></p>
|
||||
<p>inside of parentheses (<a href="http://example.com">http://example.com</a>)</p>
|
||||
<p>trailing slash <a href="http://example.com/">http://example.com/</a> and <a href="http://example.com/path/">http://example.com/path/</a></p>
|
||||
<p><a href="http://www.google.com/search?q=Markup+(business)">http://www.google.com/search?q=Markup+(business)</a></p>
|
||||
<p><a href="http://www.google.com/search?q=Markup+(business)">http://www.google.com/search?q=Markup+(business)</a>))</p>
|
||||
<p>(<a href="http://www.google.com/search?q=Markup+(business)">http://www.google.com/search?q=Markup+(business)</a>)</p>
|
||||
<p>(<a href="http://www.google.com/search?q=Markup+(business)">http://www.google.com/search?q=Markup+(business)</a></p>
|
||||
<p>trailing slash <a href="http://example.com/">http://example.com/</a> and <a href="http://example.com/path/">http://example.com/path/</a></p>
|
||||
<p>trailing paren <a href="https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)">https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)</a></p>
|
||||
<p>complex link <a href="http://elk.canda.biz/app/kibana#/discover?_g=()&_a=(columns:!(_source),index:'deve-*',interval:auto,query:(query_string:(analyze_wildcard:!t,query:'*')),sort:!('@timestamp',desc))">http://elk.canda.biz/app/kibana#/discover?_g=()&_a=(columns:!(_source),index:'deve-*',interval:auto,query:(query_string:(analyze_wildcard:!t,query:'*')),sort:!('@timestamp',desc))</a></p>
|
@ -2,4 +2,16 @@ an autolink http://example.com
|
||||
|
||||
inside of parentheses (http://example.com)
|
||||
|
||||
trailing slash http://example.com/ and http://example.com/path/
|
||||
http://www.google.com/search?q=Markup+(business)
|
||||
|
||||
http://www.google.com/search?q=Markup+(business)))
|
||||
|
||||
(http://www.google.com/search?q=Markup+(business))
|
||||
|
||||
(http://www.google.com/search?q=Markup+(business)
|
||||
|
||||
trailing slash http://example.com/ and http://example.com/path/
|
||||
|
||||
trailing paren https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)
|
||||
|
||||
complex link http://elk.canda.biz/app/kibana#/discover?_g=()&_a=(columns:!(_source),index:'deve-*',interval:auto,query:(query_string:(analyze_wildcard:!t,query:'*')),sort:!('@timestamp',desc))
|
Loading…
Reference in New Issue
Block a user