Configurable maximum link length

This commit is contained in:
Michael 2024-07-24 20:34:26 +00:00
parent ca5f93a4d5
commit ecc4f73007
3 changed files with 21 additions and 10 deletions

View 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]";
}

View 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;

View 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' => '',