Improved description extras

This commit is contained in:
Pablo Ferreiro 2023-09-29 13:31:31 +02:00
parent 200a6fc1f8
commit 80f3ca4ebb

View file

@ -96,13 +96,13 @@ class Wrappers {
}); });
// Add URLs to video descriptions // Add URLs to video descriptions
// TODO: Make it work with unicode characters such as emojis
$latte->addFunction('render_desc', function (string $desc, array $textExtras = []): string { $latte->addFunction('render_desc', function (string $desc, array $textExtras = []): string {
$sanitizedDesc = htmlspecialchars($desc); $sanitizedDesc = htmlspecialchars($desc);
$out = $sanitizedDesc; $out = $sanitizedDesc;
foreach ($textExtras as $extra) { foreach ($textExtras as $extra) {
$url = ''; $url = '';
$text = mb_substr($desc, $extra->start, $extra->end - $extra->start, 'UTF-8'); // We do -1 to also take the # or @
$text = mb_substr($desc, $extra->start - 1, $extra->end - $extra->start, 'UTF-8');
switch ($extra->type) { switch ($extra->type) {
// User URL // User URL
case TextExtras::USER: case TextExtras::USER:
@ -114,7 +114,8 @@ class Wrappers {
break; break;
} }
$out = str_replace($text, "<a href=\"$url\">$text</a>", $out); // We do \b to avoid non-strict matches ('#hi' would match with '#hii' and we don't want that)
$out = preg_replace("/$text\b/", "<a href=\"$url\">$text</a>", $out);
} }
return $out; return $out;
}); });