Add shortened URL link label stripping to PageInfo::stripTrailingUrlFromBody
- Add test cases for shortened URL link labels
This commit is contained in:
parent
da50456675
commit
8de66c0274
2 changed files with 33 additions and 5 deletions
|
@ -252,22 +252,35 @@ class PageInfo
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the provided URL from the body if it is at the end of it.
|
* Remove the provided URL from the body if it is at the end of it.
|
||||||
* Keep the link label if it isn't the full URL.
|
* Keep the link label if it isn't the full URL or a shortened version of it.
|
||||||
*
|
*
|
||||||
* @param string $body
|
* @param string $body
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @return string|string[]|null
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected static function stripTrailingUrlFromBody(string $body, string $url)
|
protected static function stripTrailingUrlFromBody(string $body, string $url)
|
||||||
{
|
{
|
||||||
$quotedUrl = preg_quote($url, '#');
|
$quotedUrl = preg_quote($url, '#');
|
||||||
$body = preg_replace("#(?:
|
$body = preg_replace_callback("#(?:
|
||||||
\[url]$quotedUrl\[/url]|
|
\[url]$quotedUrl\[/url]|
|
||||||
\[url=$quotedUrl]$quotedUrl\[/url]|
|
\[url=$quotedUrl]$quotedUrl\[/url]|
|
||||||
\[url=$quotedUrl]([^[]*?)\[/url]|
|
\[url=$quotedUrl]([^[]*?)\[/url]|
|
||||||
$quotedUrl
|
$quotedUrl
|
||||||
)$#isx", '$1', $body);
|
)$#isx", function ($match) use ($url) {
|
||||||
|
// Stripping URLs with no label
|
||||||
|
if (!isset($match[1])) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
return $body;
|
// Stripping link labels that include a shortened version of the URL
|
||||||
|
if (strpos($url, trim($match[1], '.…')) !== false) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep all other labels
|
||||||
|
return $match[1];
|
||||||
|
}, $body);
|
||||||
|
|
||||||
|
return rtrim($body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,6 +108,21 @@ class PageInfoTest extends MockedTest
|
||||||
'body' => '[url=https://example.com]link label[/url]',
|
'body' => '[url=https://example.com]link label[/url]',
|
||||||
'url' => 'https://example.com',
|
'url' => 'https://example.com',
|
||||||
],
|
],
|
||||||
|
'task-8797-shortened-link-label' => [
|
||||||
|
'expected' => 'content',
|
||||||
|
'body' => 'content [url=https://example.com/page]example.com/[/url]',
|
||||||
|
'url' => 'https://example.com/page',
|
||||||
|
],
|
||||||
|
'task-8797-shortened-link-label-ellipsis' => [
|
||||||
|
'expected' => 'content',
|
||||||
|
'body' => 'content [url=https://example.com/page]example.com…[/url]',
|
||||||
|
'url' => 'https://example.com/page',
|
||||||
|
],
|
||||||
|
'task-8797-shortened-link-label-dots' => [
|
||||||
|
'expected' => 'content',
|
||||||
|
'body' => 'content [url=https://example.com/page]example.com...[/url]',
|
||||||
|
'url' => 'https://example.com/page',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue