Browse Source

fix og-image fallback on missing article hero image

imwald
Silberengel 2 days ago
parent
commit
0576090bfc
  1. 25
      src/Twig/NostrPathExtension.php
  2. 13
      templates/base.html.twig
  3. 12
      templates/home.html.twig
  4. 12
      templates/pages/category.html.twig

25
src/Twig/NostrPathExtension.php

@ -5,7 +5,9 @@ declare(strict_types=1);
namespace App\Twig; namespace App\Twig;
use App\Entity\Article; use App\Entity\Article;
use App\Service\NostrKeyHelper;
use App\Service\NostrPathHelper; use App\Service\NostrPathHelper;
use Throwable;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\TwigFunction; use Twig\TwigFunction;
@ -13,6 +15,7 @@ final class NostrPathExtension extends AbstractExtension
{ {
public function __construct( public function __construct(
private readonly NostrPathHelper $nostrPathHelper, private readonly NostrPathHelper $nostrPathHelper,
private readonly NostrKeyHelper $nostrKeyHelper,
) { ) {
} }
@ -20,6 +23,7 @@ final class NostrPathExtension extends AbstractExtension
{ {
return [ return [
new TwigFunction('npub_from_hex', $this->npubFromHex(...)), new TwigFunction('npub_from_hex', $this->npubFromHex(...)),
new TwigFunction('pubkey_hex_from_npub', $this->pubkeyHexFromNpub(...)),
new TwigFunction('article_path', $this->articlePath(...)), new TwigFunction('article_path', $this->articlePath(...)),
]; ];
} }
@ -33,6 +37,27 @@ final class NostrPathExtension extends AbstractExtension
return $this->nostrPathHelper->npubFromPubkeyHex($pubkeyHex); 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 public function articlePath(Article $article): string
{ {
return $this->nostrPathHelper->articlePath($article); return $this->nostrPathHelper->articlePath($article);

13
templates/base.html.twig

@ -39,9 +39,20 @@
<link rel="manifest" href="{{ path('pwa_manifest') }}"> <link rel="manifest" href="{{ path('pwa_manifest') }}">
{% block ogtags %} {% 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 %}
<meta property="og:type" content="website"> <meta property="og:type" content="website">
<meta property="og:description" content="{{ website_description|e('html_attr') }}"> <meta property="og:description" content="{{ website_description|e('html_attr') }}">
<meta property="og:image" content="{{ absolute_url(asset('og-image.jpg'))|e('html_attr') }}"> <meta property="og:image" content="{{ _og_image|e('html_attr') }}">
{% if _og_image starts with 'https://' %}
<meta property="og:image:secure_url" content="{{ _og_image|e('html_attr') }}">
{% endif %}
{% if _og_default_dims %}
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
{% endif %}
<meta property="og:site_name" content="{{ website_name|e('html_attr') }}"> <meta property="og:site_name" content="{{ website_name|e('html_attr') }}">
{% endblock %} {% endblock %}
{% block javascripts %} {% block javascripts %}

12
templates/home.html.twig

@ -7,7 +7,10 @@
{% endblock %} {% endblock %}
{% block ogtags %} {% 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 %}
<link rel="canonical" href="{{ url('home') }}"> <link rel="canonical" href="{{ url('home') }}">
<link rel="alternate" type="application/atom+xml" title="{{ website_name|e('html_attr') }} — all articles" href="{{ url('feed_magazine') }}"> <link rel="alternate" type="application/atom+xml" title="{{ website_name|e('html_attr') }} — all articles" href="{{ url('feed_magazine') }}">
<meta property="og:type" content="website"> <meta property="og:type" content="website">
@ -15,6 +18,13 @@
<meta property="og:title" content="{{ website_name|e('html_attr') }}"> <meta property="og:title" content="{{ website_name|e('html_attr') }}">
<meta property="og:description" content="{{ website_description|e('html_attr') }}"> <meta property="og:description" content="{{ website_description|e('html_attr') }}">
<meta property="og:image" content="{{ _og_image|e('html_attr') }}"> <meta property="og:image" content="{{ _og_image|e('html_attr') }}">
{% if _og_image starts with 'https://' %}
<meta property="og:image:secure_url" content="{{ _og_image|e('html_attr') }}">
{% endif %}
{% if _og_default_dims %}
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
{% endif %}
<meta property="og:site_name" content="{{ website_name|e('html_attr') }}"> <meta property="og:site_name" content="{{ website_name|e('html_attr') }}">
<meta name="twitter:card" content="summary_large_image"> <meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="{{ website_name|e('html_attr') }}"> <meta name="twitter:title" content="{{ website_name|e('html_attr') }}">

12
templates/pages/category.html.twig

@ -11,7 +11,10 @@
{% block ogtags %} {% block ogtags %}
{% set _title = category.title|default('') %} {% set _title = category.title|default('') %}
{% set _summary = category.summary|default('')|striptags|u.truncate(159, '…') %} {% 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_articles_route = app.request.attributes.get('_route') == 'articles' %}
{% set _is_category_route = app.request.attributes.get('_route') == 'magazine-category' %} {% set _is_category_route = app.request.attributes.get('_route') == 'magazine-category' %}
{% set _articles_page = app.request.query.getInt('page', 1) %} {% set _articles_page = app.request.query.getInt('page', 1) %}
@ -30,6 +33,13 @@
<meta property="og:url" content="{{ _canonical_url }}"> <meta property="og:url" content="{{ _canonical_url }}">
<meta property="og:description" content="{{ (_summary != '' ? _summary : _title)|e('html_attr') }}"> <meta property="og:description" content="{{ (_summary != '' ? _summary : _title)|e('html_attr') }}">
<meta property="og:image" content="{{ _og_image|e('html_attr') }}"> <meta property="og:image" content="{{ _og_image|e('html_attr') }}">
{% if _og_image starts with 'https://' %}
<meta property="og:image:secure_url" content="{{ _og_image|e('html_attr') }}">
{% endif %}
{% if _og_default_dims %}
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
{% endif %}
<meta property="og:site_name" content="{{ website_name|e('html_attr') }}"> <meta property="og:site_name" content="{{ website_name|e('html_attr') }}">
<link rel="canonical" href="{{ _canonical_url }}"> <link rel="canonical" href="{{ _canonical_url }}">
<meta name="twitter:card" content="summary_large_image"> <meta name="twitter:card" content="summary_large_image">

Loading…
Cancel
Save