From 0576090bfcb48876fce666741f4adbe3b3d91e48 Mon Sep 17 00:00:00 2001 From: Silberengel Date: Thu, 30 Apr 2026 12:43:41 +0200 Subject: [PATCH] fix og-image fallback on missing article hero image --- src/Twig/NostrPathExtension.php | 25 +++++++++++++++++++++++++ templates/base.html.twig | 13 ++++++++++++- templates/home.html.twig | 12 +++++++++++- templates/pages/category.html.twig | 12 +++++++++++- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/Twig/NostrPathExtension.php b/src/Twig/NostrPathExtension.php index de83b31..0d8a854 100644 --- a/src/Twig/NostrPathExtension.php +++ b/src/Twig/NostrPathExtension.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace App\Twig; use App\Entity\Article; +use App\Service\NostrKeyHelper; use App\Service\NostrPathHelper; +use Throwable; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; @@ -13,6 +15,7 @@ final class NostrPathExtension extends AbstractExtension { public function __construct( private readonly NostrPathHelper $nostrPathHelper, + private readonly NostrKeyHelper $nostrKeyHelper, ) { } @@ -20,6 +23,7 @@ final class NostrPathExtension extends AbstractExtension { return [ new TwigFunction('npub_from_hex', $this->npubFromHex(...)), + new TwigFunction('pubkey_hex_from_npub', $this->pubkeyHexFromNpub(...)), new TwigFunction('article_path', $this->articlePath(...)), ]; } @@ -33,6 +37,27 @@ final class NostrPathExtension extends AbstractExtension return $this->nostrPathHelper->npubFromPubkeyHex($pubkeyHex); } + /** + * Lowercase 64-hex pubkey for {@see ArticleCardCoverExtension::articleOgImage} / card cover helpers. + */ + public function pubkeyHexFromNpub(string $npub): string + { + $n = trim($npub); + if ($n === '') { + return ''; + } + try { + $hex = $this->nostrKeyHelper->convertToHex($n); + if (64 !== \strlen($hex) || !ctype_xdigit($hex)) { + return ''; + } + + return strtolower($hex); + } catch (Throwable) { + return ''; + } + } + public function articlePath(Article $article): string { return $this->nostrPathHelper->articlePath($article); diff --git a/templates/base.html.twig b/templates/base.html.twig index 96ed545..245c59e 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -39,9 +39,20 @@ {% block ogtags %} + {% set _pubkey_hex = pubkey_hex_from_npub(publisher_npub|default('')) %} + {% set _og = article_og_image(null, _pubkey_hex) %} + {% set _og_image = _og.href %} + {% set _og_default_dims = _og.use_default_dimensions %} - + + {% if _og_image starts with 'https://' %} + + {% endif %} + {% if _og_default_dims %} + + + {% endif %} {% endblock %} {% block javascripts %} diff --git a/templates/home.html.twig b/templates/home.html.twig index d0e0694..0085bfe 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -7,7 +7,10 @@ {% endblock %} {% block ogtags %} - {% set _og_image = absolute_url(asset('og-image.jpg')) %} + {% set _pubkey_hex = pubkey_hex_from_npub(publisher_npub|default('')) %} + {% set _og = article_og_image(null, _pubkey_hex) %} + {% set _og_image = _og.href %} + {% set _og_default_dims = _og.use_default_dimensions %} @@ -15,6 +18,13 @@ + {% if _og_image starts with 'https://' %} + + {% endif %} + {% if _og_default_dims %} + + + {% endif %} diff --git a/templates/pages/category.html.twig b/templates/pages/category.html.twig index 0b45293..2d69b7a 100644 --- a/templates/pages/category.html.twig +++ b/templates/pages/category.html.twig @@ -11,7 +11,10 @@ {% block ogtags %} {% set _title = category.title|default('') %} {% set _summary = category.summary|default('')|striptags|u.truncate(159, '…') %} - {% set _og_image = absolute_url(asset('og-image.jpg')) %} + {% set _pubkey_hex = pubkey_hex_from_npub(publisher_npub|default('')) %} + {% set _og = article_og_image(null, _pubkey_hex) %} + {% set _og_image = _og.href %} + {% set _og_default_dims = _og.use_default_dimensions %} {% set _is_articles_route = app.request.attributes.get('_route') == 'articles' %} {% set _is_category_route = app.request.attributes.get('_route') == 'magazine-category' %} {% set _articles_page = app.request.query.getInt('page', 1) %} @@ -30,6 +33,13 @@ + {% if _og_image starts with 'https://' %} + + {% endif %} + {% if _og_default_dims %} + + + {% endif %}