Skip to content

Commit

Permalink
Issue friendica#14079: Shorten the displayed URL
Browse files Browse the repository at this point in the history
  • Loading branch information
annando committed Apr 8, 2024
1 parent 02d8cc2 commit ec19b2a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
31 changes: 30 additions & 1 deletion src/Content/Text/BBCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
class BBCode
{
// Update this value to the current date whenever changes are made to BBCode::convert
const VERSION = '2021-07-28';
const VERSION = '2024-04-07';

const INTERNAL = 0;
const EXTERNAL = 1;
Expand Down Expand Up @@ -1988,6 +1988,10 @@ function ($match) {
return $text;
});

if (!$for_plaintext && in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::DIASPORA])) {
$text = self::shortenLinkDescription($text);
}

// We need no target="_blank" rel="noopener noreferrer" for local links
// convert links start with DI::baseUrl() as local link without the target="_blank" rel="noopener noreferrer" attribute
$escapedBaseUrl = preg_quote(DI::baseUrl(), '/');
Expand Down Expand Up @@ -2112,6 +2116,31 @@ function ($matches) {
return trim($text);
}

private static function shortenLinkDescription(string $text): string
{
$text = preg_replace_callback(
"/\[url\](.*?)\[\/url\]/ism",
function ($match) {
$url = str_replace(['[', ']'], ['[', ']'], $match[1]);
return "[url=" . $url . "]" . Strings::getStyledURL($match[1]) . "[/url]";
},
$text
);
$text = preg_replace_callback(
"/\[url\=(.*?)\](.*?)\[\/url\]/ism",
function ($match) {
$url = str_replace(['[', ']'], ['[', ']'], $match[1]);
if ($match[1] == $match[2]) {
return "[url=" . $url . "]" . Strings::getStyledURL($match[2]) . "[/url]";
} else {
return "[url=" . $url . "]" . $match[2] . "[/url]";
}
},
$text
);
return $text;
}

/**
* Strips the "abstract" tag from the provided text
*
Expand Down
4 changes: 4 additions & 0 deletions src/Util/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ public static function getBytesFromShorthand(string $shorthand): int
public static function getStyledURL(string $url): string
{
$parts = parse_url($url);
if (empty($parts['scheme'])) {
return $url;
}

$scheme = [$parts['scheme'] . '://www.', $parts['scheme'] . '://'];
$styled_url = str_replace($scheme, '', $url);

Expand Down
11 changes: 6 additions & 5 deletions tests/src/Content/Text/BBCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Friendica\DI;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Test\FixtureTest;
use Friendica\Util\Strings;

class BBCodeTest extends FixtureTest
{
Expand Down Expand Up @@ -148,7 +149,7 @@ public function dataLinks()
public function testAutoLinking(string $data, bool $assertHTML)
{
$output = BBCode::convert($data);
$assert = $this->HTMLPurifier->purify('<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>');
$assert = $this->HTMLPurifier->purify('<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . Strings::getStyledURL($data) . '</a>');
if ($assertHTML) {
self::assertEquals($assert, $output);
} else {
Expand All @@ -160,21 +161,21 @@ public function dataBBCodes()
{
return [
'bug-7271-condensed-space' => [
'expectedHtml' => '<ol><li> <a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ol>',
'expectedHtml' => '<ol><li> <a href="http://example.com/" target="_blank" rel="noopener noreferrer">example.com/</a></li></ol>',
'text' => '[ol][li] http://example.com/[/ol]',
],
'bug-7271-condensed-nospace' => [
'expectedHtml' => '<ol><li><a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ol>',
'expectedHtml' => '<ol><li><a href="http://example.com/" target="_blank" rel="noopener noreferrer">example.com/</a></li></ol>',
'text' => '[ol][li]http://example.com/[/ol]',
],
'bug-7271-indented-space' => [
'expectedHtml' => '<ul><li> <a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ul>',
'expectedHtml' => '<ul><li> <a href="http://example.com/" target="_blank" rel="noopener noreferrer">example.com/</a></li></ul>',
'text' => '[ul]
[li] http://example.com/
[/ul]',
],
'bug-7271-indented-nospace' => [
'expectedHtml' => '<ul><li><a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ul>',
'expectedHtml' => '<ul><li><a href="http://example.com/" target="_blank" rel="noopener noreferrer">example.com/</a></li></ul>',
'text' => '[ul]
[li]http://example.com/
[/ul]',
Expand Down

0 comments on commit ec19b2a

Please sign in to comment.