From ecc4f730079aad281922e175186457ed7f74e40d Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 24 Jul 2024 20:34:26 +0000 Subject: [PATCH] Configurable maximum link length --- src/Content/Text/BBCode.php | 18 ++++++++++++------ src/Util/Strings.php | 9 +++++---- static/defaults.config.php | 4 ++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 24e1c42606..933d497af2 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -2090,7 +2090,7 @@ class BBCode $text = preg_replace("/\[zrl\=(.*?)\](.*?)\[\/zrl\]/ism", '[url=$1]$2[/url]', $text); if (in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::DIASPORA, self::OSTATUS, self::MASTODON_API, self::TWITTER_API, self::ACTIVITYPUB])) { - $text = self::shortenLinkDescription($text); + $text = self::shortenLinkDescription($text, $simple_html); } else { $text = self::unifyLinks($text); } @@ -2143,20 +2143,26 @@ class BBCode ); } - private static function shortenLinkDescription(string $text): string + private static function shortenLinkDescription(string $text, int $simple_html): string { + if ($simple_html == self::INTERNAL) { + $max_length = DI::config()->get('system', 'display_link_length'); + } else { + $max_length = 30; + } + $text = preg_replace_callback( "/\[url\](.*?)\[\/url\]/ism", - function ($match) { - return "[url=" . self::escapeUrl($match[1]) . "]" . Strings::getStyledURL($match[1]) . "[/url]"; + function ($match) use ($max_length) { + return "[url=" . self::escapeUrl($match[1]) . "]" . Strings::getStyledURL($match[1], $max_length) . "[/url]"; }, $text ); $text = preg_replace_callback( "/\[url\=(.*?)\](.*?)\[\/url\]/ism", - function ($match) { + function ($match) use ($max_length) { if ($match[1] == $match[2]) { - return "[url=" . self::escapeUrl($match[1]) . "]" . Strings::getStyledURL($match[2]) . "[/url]"; + return "[url=" . self::escapeUrl($match[1]) . "]" . Strings::getStyledURL($match[2], $max_length) . "[/url]"; } else { return "[url=" . self::escapeUrl($match[1]) . "]" . $match[2] . "[/url]"; } diff --git a/src/Util/Strings.php b/src/Util/Strings.php index 4a68e31248..421bed8d56 100644 --- a/src/Util/Strings.php +++ b/src/Util/Strings.php @@ -565,10 +565,11 @@ class Strings /** * Converts an URL in a nicer format (without the scheme and possibly shortened) * - * @param string $url URL that is about to be reformatted + * @param string $url URL that is about to be reformatted + * @param int $max_length Maximum length of an url before it is shortened * @return string reformatted link */ - public static function getStyledURL(string $url): string + public static function getStyledURL(string $url, int $max_length = 30): string { $parts = parse_url($url); if (empty($parts['scheme'])) { @@ -578,8 +579,8 @@ class Strings $scheme = [$parts['scheme'] . '://www.', $parts['scheme'] . '://']; $styled_url = str_replace($scheme, '', $url); - if (strlen($styled_url) > 30) { - $styled_url = substr($styled_url, 0, 30) . "…"; + if (!empty($max_length) && strlen($styled_url) > $max_length) { + $styled_url = substr($styled_url, 0, $max_length) . "…"; } return $styled_url; diff --git a/static/defaults.config.php b/static/defaults.config.php index eb1b2b01db..5392248176 100644 --- a/static/defaults.config.php +++ b/static/defaults.config.php @@ -259,6 +259,10 @@ return [ // Display the first resharer as icon and text on a reshared item. 'display_resharer' => false, + // display_link_length (integer) + // Maximum length of displayed links. Default value is 30, 0 deactivates the functionality. + 'display_link_length' => 30, + // dlogfile (Path) // location of the developer log file. 'dlogfile' => '',