diff --git a/sources/AppBundle/Twig/MetaDescriptionExtension.php b/sources/AppBundle/Twig/MetaDescriptionExtension.php new file mode 100644 index 000000000..b7f736b34 --- /dev/null +++ b/sources/AppBundle/Twig/MetaDescriptionExtension.php @@ -0,0 +1,52 @@ + : balises retirées, entités décodées, + * espaces normalisés, troncature sur une frontière de mot. + */ +final class MetaDescriptionExtension extends AbstractExtension +{ + public const int MAX_LENGTH = 155; + + private const string ELLIPSIS = '…'; + + #[\Override] + public function getFilters(): array + { + return [ + new TwigFilter('meta_description', $this->metaDescription(...)), + ]; + } + + public function metaDescription(string|\Stringable|null $content, int $maxLength = self::MAX_LENGTH): string + { + $content = (string) $content; + + // Sans ce séparateur, «
Un
Deux
» donnerait « UnDeux ». + $content = preg_replace('#<(br|/p|/div|/li|/h[1-6]|/blockquote|/pre|/tr|/td|/th)\b[^>]*>#i', '$0 ', $content); + $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; + } +} diff --git a/templates/base.html.twig b/templates/base.html.twig index 82195e8b5..0b20f6444 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -7,6 +7,13 @@ content="width=device-width, user-scalable=yes, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> {% endblock %} + {# Texte de la page, converti en texte brut tronqué à 155 caractères. Un champ HTML doit passer + par |meta_description dans le bloc : sinon ses entités, échappées par Twig, resteraient visibles. #} + {% set meta_description_content %}{% block description %}{% endblock %}{% endset %} + {% set meta_description_content = meta_description_content|meta_description %} + {% if meta_description_content is not empty %} + + {% endif %} {% block canonical %} {% if app.request.attributes.get('_route') %} diff --git a/templates/layouts/root.html.twig b/templates/layouts/root.html.twig index 072051995..b3012c67c 100644 --- a/templates/layouts/root.html.twig +++ b/templates/layouts/root.html.twig @@ -6,6 +6,14 @@ {% block metas %}{% endblock %} + {# Texte de la page, converti en texte brut tronqué à 155 caractères. Un champ HTML doit passer + par |meta_description dans le bloc : sinon ses entités, échappées par Twig, resteraient visibles. #} + {% set meta_description_content %}{% block description %}{% endblock %}{% endset %} + {% set meta_description_content = meta_description_content|meta_description %} + {% if meta_description_content is not empty %} + + {% endif %} +