diff --git a/sources/AppBundle/Text/Truncator.php b/sources/AppBundle/Text/Truncator.php new file mode 100644 index 000000000..36cbd91c9 --- /dev/null +++ b/sources/AppBundle/Text/Truncator.php @@ -0,0 +1,31 @@ + des talks et aux meta descriptions. + */ +final class Truncator +{ + private const string ELLIPSIS = '…'; + + public static function truncate(string $text, int $maxLength): string + { + if (mb_strlen($text) <= $maxLength) { + return $text; + } + + // Un caractère de plus que la place disponible : s'il s'agit d'un espace, le dernier mot est entier. + $truncated = mb_substr($text, 0, $maxLength - mb_strlen(self::ELLIPSIS) + 1); + $lastSpace = mb_strrpos($truncated, ' '); + $truncated = false !== $lastSpace && $lastSpace > 0 + ? mb_substr($truncated, 0, $lastSpace) + : mb_substr($truncated, 0, -1); + + // Pas d'espace ni de ponctuation devant « … ». En mode /u : rtrim() couperait un caractère multi-octet. + return (string) preg_replace('/[\s,;:.\-–—]+$/u', '', $truncated) . self::ELLIPSIS; + } +} diff --git a/sources/AppBundle/Twig/MetaDescriptionExtension.php b/sources/AppBundle/Twig/MetaDescriptionExtension.php index b7f736b34..81c7759e9 100644 --- a/sources/AppBundle/Twig/MetaDescriptionExtension.php +++ b/sources/AppBundle/Twig/MetaDescriptionExtension.php @@ -4,6 +4,7 @@ namespace AppBundle\Twig; +use AppBundle\Text\Truncator; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; @@ -16,8 +17,6 @@ final class MetaDescriptionExtension extends AbstractExtension { public const int MAX_LENGTH = 155; - private const string ELLIPSIS = '…'; - #[\Override] public function getFilters(): array { @@ -35,18 +34,6 @@ public function metaDescription(string|\Stringable|null $content, int $maxLength $text = html_entity_decode(strip_tags((string) $content), ENT_QUOTES | ENT_HTML5, 'UTF-8'); $text = trim((string) preg_replace('/\s+/u', ' ', $text)); - if (mb_strlen($text) <= $maxLength) { - return $text; - } - - $truncated = mb_substr($text, 0, $maxLength - mb_strlen(self::ELLIPSIS) + 1); - $lastSpace = mb_strrpos($truncated, ' '); - if ($lastSpace !== false) { - $truncated = mb_substr($truncated, 0, $lastSpace); - } else { - $truncated = mb_substr($truncated, 0, -1); - } - - return rtrim($truncated, ' ,;:.-–—') . self::ELLIPSIS; + return Truncator::truncate($text, $maxLength); } } diff --git a/sources/AppBundle/Twig/TalkPageTitleExtension.php b/sources/AppBundle/Twig/TalkPageTitleExtension.php new file mode 100644 index 000000000..e672af71c --- /dev/null +++ b/sources/AppBundle/Twig/TalkPageTitleExtension.php @@ -0,0 +1,46 @@ + d'une page talk en restant sous la limite affichée par les moteurs de recherche. + */ +final class TalkPageTitleExtension extends AbstractExtension +{ + public const int MAX_LENGTH = 65; + + private const string SEPARATOR = ' – '; + + #[\Override] + public function getFunctions(): array + { + return [ + new TwigFunction('talk_page_title', $this->talkPageTitle(...)), + ]; + } + + /** + * « {titre} – {événement} » si l'ensemble tient dans la limite, sinon le titre seul, + * tronqué sur une frontière de mot avec « … » s'il la dépasse encore. + */ + public function talkPageTitle(string $talkTitle, string $eventTitle): string + { + $talkTitle = trim($talkTitle); + $eventTitle = trim($eventTitle); + + if ($eventTitle !== '') { + $withEvent = $talkTitle . self::SEPARATOR . $eventTitle; + if (mb_strlen($withEvent) <= self::MAX_LENGTH) { + return $withEvent; + } + } + + return Truncator::truncate($talkTitle, self::MAX_LENGTH); + } +} diff --git a/templates/site/talks/show.html.twig b/templates/site/talks/show.html.twig index 375d75846..8ac98549f 100644 --- a/templates/site/talks/show.html.twig +++ b/templates/site/talks/show.html.twig @@ -7,7 +7,7 @@ { name: talk.title, url: url('talks_show', {id: talk.id, slug: talk.slug}) }, ] %} -{% block title %}{{ talk.title }} - {{ event.title }}{% endblock %} +{% block title %}{{ talk_page_title(talk.title, event.title) }}{% endblock %} {% block description %}{{ talk.description|meta_description }}{% endblock %} diff --git a/tests/behat/features/PublicSite/Talks.feature b/tests/behat/features/PublicSite/Talks.feature index b8b79b04a..2934816b3 100644 --- a/tests/behat/features/PublicSite/Talks.feature +++ b/tests/behat/features/PublicSite/Talks.feature @@ -396,3 +396,13 @@ Feature: Site Public - Talks Then the page should have a meta description When I go to "/talks/evenements/" Then the page should have a meta description + + @reloadDbWithTestData + Scenario: Le title d'un talk au titre court contient l'évènement + When I go to "/talks/1-jouons-tous-ensemble-a-un-petit-jeu" + Then the response should contain "