]> git.mxchange.org Git - friendica.git/commitdiff
Configurable maximum link length
authorMichael <heluecht@pirati.ca>
Wed, 24 Jul 2024 20:34:26 +0000 (20:34 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 24 Jul 2024 20:34:26 +0000 (20:34 +0000)
src/Content/Text/BBCode.php
src/Util/Strings.php
static/defaults.config.php

index 24e1c426065f43ee532892ddbfe2168b7f3ce178..933d497af2b392f9ad4a8fb8cc9fe927bd4e6e05 100644 (file)
@@ -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]";
                                }
index 4a68e31248d77f275c5dd13ef53af678d9ba47f8..421bed8d5688746786673ae621b1660cb28609ed 100644 (file)
@@ -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;
index eb1b2b01db0f74a4b67c8f7dc2dd4921b808c60c..53922481767bcc090d8d937e8b87127ed40d6886 100644 (file)
@@ -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' => '',