From dbbdca504e42243e02635ff448d60dd88102f620 Mon Sep 17 00:00:00 2001
From: Eric Bus
Date: Mon, 23 Nov 2026 14:00:58 +0100
Subject: [PATCH 1/3] SEO: structured data and correct meta tags for news
articles
- NewsArticle + BreadcrumbList JSON-LD (headline, summary, images, ISO
dates, authors, GOOI&meer as NewsMediaOrganization publisher, section)
- Per-article meta description instead of the generic site text
- article:published_time / modified_time in ISO 8601 (was og:article:*
in RFC 2822), article:section / article:tag, og:image:alt, og:locale nl_NL
- Canonical URL, twitter:card, robots max-image-preview:large
- , title without leading whitespace, no keywords meta
- Article wrapped in , dates in
- Metadata is rebuilt after the controller fills in API v2 data (dates,
author, content blocks were not reflected before)
Co-Authored-By: Claude Opus 5.5
---
app/Http/Controllers/NewsController.php | 2 +
app/Models/MetaData.php | 42 +++++--
app/Models/NewsItem.php | 105 +++++++++++++++++-
resources/views/layouts/_head.blade.php | 10 +-
resources/views/layouts/empty.blade.php | 2 +-
resources/views/layouts/master.blade.php | 2 +-
resources/views/layouts/sidebar.blade.php | 5 +-
resources/views/newsitem.blade.php | 14 ++-
resources/views/partial/news_jsonld.blade.php | 66 +++++++++++
9 files changed, 224 insertions(+), 24 deletions(-)
create mode 100644 resources/views/partial/news_jsonld.blade.php
diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php
index 8a1af5b7..85d94302 100644
--- a/app/Http/Controllers/NewsController.php
+++ b/app/Http/Controllers/NewsController.php
@@ -41,6 +41,8 @@ class NewsController extends Controller
} elseif (isset($apiResult->source->blocks)) {
$newsItem->content = $apiResult->source->blocks;
}
+ // Dates, author and content may have changed above
+ $newsItem->buildMetadata();
return view('newsitem', array_merge($this->getSidebareData(), ['type' => $apiResult->type, 'news' => $newsItem, 'metadata' => $newsItem->metadata, 'searchURL' => 'nieuws/zoeken']));
}
}
diff --git a/app/Models/MetaData.php b/app/Models/MetaData.php
index de36d80e..dd77c659 100644
--- a/app/Models/MetaData.php
+++ b/app/Models/MetaData.php
@@ -1,35 +1,63 @@
, except
+ * namespaced ones (e.g. article:published_time), which are rendered as-is.
+ * A value can be an array to render the same property multiple times
+ * (e.g. article:tag).
+ */
class MetaData {
- private $data = ['type' => 'article', 'locale' => 'nl-nl'];
+ private $data = ['type' => 'article', 'locale' => 'nl_NL'];
public function set($prop, $val) {
if($val) {
- $this->data[$prop] = $val;
+ $this->data[$prop] = self::clean($val);
}
return $this;
}
+ public function get($prop, $default = null) {
+ return $this->data[$prop] ?? $default;
+ }
+
public function append($prop, $val) {
- if(array_key_exists($prop, $this->data)) {
+ if(array_key_exists($prop, $this->data) && is_string($this->data[$prop])) {
$this->data[$prop] .= $val;
}
return $this;
}
+ /**
+ * Prefix a relative value (e.g. an image path) with a base URL.
+ * Absolute URLs are left alone.
+ */
public function prepend($prop, $val) {
- if(array_key_exists($prop, $this->data)) {
+ if(array_key_exists($prop, $this->data) && is_string($this->data[$prop])
+ && !preg_match('#^(https?:)?//#i', $this->data[$prop])) {
$this->data[$prop] = $val . $this->data[$prop];
}
return $this;
}
public function metaTags() {
- foreach($this->data as $prop => $val) {
- if($val) {
- echo " \n";
+ foreach($this->data as $prop => $values) {
+ $name = strpos($prop, ':') !== false && strpos($prop, 'image:') !== 0 ? $prop : "og:$prop";
+ foreach((array)$values as $val) {
+ if($val) {
+ echo " \n";
+ }
}
}
}
+
+ /** Collapse whitespace (newlines, tabs) in text values. */
+ private static function clean($val) {
+ if(is_array($val)) {
+ return array_values(array_filter(array_map([self::class, 'clean'], $val)));
+ }
+ return is_string($val) ? trim(preg_replace('/\s+/u', ' ', $val)) : $val;
+ }
}
diff --git a/app/Models/NewsItem.php b/app/Models/NewsItem.php
index 710794a6..55fc50e1 100644
--- a/app/Models/NewsItem.php
+++ b/app/Models/NewsItem.php
@@ -96,15 +96,110 @@ class NewsItem extends Model {
}
$this->url = "/nieuws/{$this->id}/" . parent::url_slug($this->title);
+ $this->buildMetadata();
+ }
+
+ /**
+ * (Re)build the Open Graph / article metadata. Call again after changing
+ * the content, dates or author (e.g. for API v2 articles).
+ */
+ public function buildMetadata() {
+ $images = $this->imageList();
+ $tags = $this->tagTitles();
$this->metadata = (new MetaData())
- ->set('title', $this->title)
- ->set('description', strip_tags($this->excerpt()))
- ->set('image', isset($this->images) && count($this->images) ? $this->images[0]->url : null )
+ ->set('title', $this->plainTitle())
+ ->set('description', $this->summary(200))
+ ->set('image', $images ? $images[0]['url'] : null)
+ ->set('image:alt', $images ? $images[0]['title'] : null)
->set('video', isset($this->video) ? $this->video : null)
->set('audio', isset($this->podcast) ? $this->podcast->url : null)
- ->set('article:published_time', $this->published->format('r'))
- ->set('article:modified_time', $this->edited ? $this->edited->format('r') : null)
+ ->set('article:published_time', $this->published ? $this->published->format(DATE_ATOM) : null)
+ ->set('article:modified_time', $this->edited ? $this->edited->format(DATE_ATOM) : null)
+ ->set('article:section', $tags ? $tags[0] : null)
+ ->set('article:tag', $tags)
;
+ return $this->metadata;
+ }
+
+ /** The title without HTML tags or entities. */
+ public function plainTitle() {
+ return self::normalizeText((string)$this->title);
+ }
+
+ /** Titles of the article's tags (mostly places, e.g. 'Hilversum'). */
+ public function tagTitles() {
+ $titles = [];
+ foreach((is_array($this->tags) ? $this->tags : []) as $tag) {
+ if(isset($tag->titel) && $tag->titel) {
+ $titles[] = $tag->titel;
+ }
+ }
+ return $titles;
+ }
+
+ /** Plain text of the article; works for HTML content and API v2 content blocks. */
+ public function plainText() {
+ if(is_string($this->content)) {
+ return self::normalizeText($this->content);
+ }
+ $parts = [];
+ foreach((is_array($this->content) ? $this->content : []) as $block) {
+ if(in_array($block->type ?? '', ['intro', 'paragraph', 'text', 'header'])) {
+ $parts[] = $block->data->text ?? $block->text ?? '';
+ }
+ }
+ return self::normalizeText(implode(' ', $parts));
+ }
+
+ /** Short plain-text summary (for meta descriptions), cut at a word boundary. */
+ public function summary($maxLength = 160) {
+ $text = $this->plainText();
+ if(mb_strlen($text) <= $maxLength) {
+ return $text;
+ }
+ $cut = mb_substr($text, 0, $maxLength);
+ $space = mb_strrpos($cut, ' ');
+ return rtrim(mb_substr($cut, 0, $space ?: $maxLength), " ,.;:-") . '…';
+ }
+
+ /**
+ * The article's images as ['url' => ..., 'title' => ..., 'own' => bool].
+ * Own images ('own' => true) have a URL relative to the image base
+ * (IMAGE_BASE_URL); embedded (v2) images get an absolute URL.
+ */
+ public function imageList() {
+ $list = [];
+ foreach((is_array($this->images) ? $this->images : []) as $image) {
+ $list[] = ['url' => $image->url, 'title' => $image->title ?? null, 'own' => true];
+ }
+ foreach((is_array($this->content) ? $this->content : []) as $block) {
+ $type = $block->type ?? '';
+ if($type == 'headerRichA' && isset($block->data->image)) {
+ $image = $block->data->image;
+ $url = isset($image->crops) ? ($image->crops->{'16:9'}->{'1600'} ?? null) : ($image->crop ?? null);
+ $list[] = ['url' => $url, 'title' => $image->title ?? null];
+ } elseif(in_array($type, ['headerRichA', 'image']) && isset($block->image->url)) {
+ $list[] = ['url' => $block->image->url, 'title' => $block->image->title ?? null];
+ } elseif($type == 'image' && isset($block->data->url)) {
+ $list[] = ['url' => '/' . ltrim($block->data->url, '/'), 'title' => $block->data->title ?? null];
+ }
+ }
+ $list = array_values(array_filter($list, function($image) { return $image['url']; }));
+ foreach($list as &$image) {
+ if(!isset($image['own'])) {
+ $image['own'] = false;
+ if(!preg_match('#^(https?:)?//#i', $image['url'])) {
+ $image['url'] = url($image['url']);
+ }
+ }
+ }
+ return $list;
+ }
+
+ private static function normalizeText($html) {
+ $html = preg_replace('# |
|#i', ' ', $html);
+ $text = html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, 'UTF-8');
+ return trim(preg_replace('/\s+/u', ' ', $text));
}
public function excerpt() {
diff --git a/resources/views/layouts/_head.blade.php b/resources/views/layouts/_head.blade.php
index 8da9d4c0..9bee3512 100644
--- a/resources/views/layouts/_head.blade.php
+++ b/resources/views/layouts/_head.blade.php
@@ -1,12 +1,14 @@
@include('layouts._analytics')
- @yield('title') | GOOI&meer
+ {!! trim($__env->yieldContent('title')) !!} | GOOI&meer
-
-
+
+ {{-- Allow large image previews (Google Discover, Top Stories) --}}
+
+
@@ -35,6 +37,8 @@
->append('title', ' | GOOI&meer')
->metaTags();
} ?>
+
+ @stack('head')
@stack('styles')
diff --git a/resources/views/layouts/empty.blade.php b/resources/views/layouts/empty.blade.php
index 9f72e81c..823508cd 100644
--- a/resources/views/layouts/empty.blade.php
+++ b/resources/views/layouts/empty.blade.php
@@ -1,5 +1,5 @@
-
+
@include('layouts._head')
@yield('content')
diff --git a/resources/views/layouts/master.blade.php b/resources/views/layouts/master.blade.php
index 40d84a47..9abb87ea 100644
--- a/resources/views/layouts/master.blade.php
+++ b/resources/views/layouts/master.blade.php
@@ -2,7 +2,7 @@
@yield('content')
@endsection
-
+
@include('layouts._head')
diff --git a/resources/views/layouts/sidebar.blade.php b/resources/views/layouts/sidebar.blade.php
index 389e0c98..9ff0278c 100644
--- a/resources/views/layouts/sidebar.blade.php
+++ b/resources/views/layouts/sidebar.blade.php
@@ -11,12 +11,13 @@
@yield('breadcrumb_top')
-
+ {{-- Pages can make this an
with @section('content_tag', 'article') --}}
+ <@yield('content_tag', 'div') class="full-width md_margin_top @yield('content_class')">
@yield('breadcrumb')
@yield('tags')
@yield('title')
@yield('content')
-
+ @yield('content_tag', 'div')>
diff --git a/resources/views/podcastitem.blade.php b/resources/views/podcastitem.blade.php
index e4299615..0878e846 100644
--- a/resources/views/podcastitem.blade.php
+++ b/resources/views/podcastitem.blade.php
@@ -155,7 +155,7 @@
-
@endif
@endsection
diff --git a/resources/views/podcastlist.blade.php b/resources/views/podcastlist.blade.php
index 45a22b04..af3779a3 100644
--- a/resources/views/podcastlist.blade.php
+++ b/resources/views/podcastlist.blade.php
@@ -91,7 +91,7 @@
@endif
-
diff --git a/resources/views/podcastseries.blade.php b/resources/views/podcastseries.blade.php
index f3cb6241..f37dd3c8 100644
--- a/resources/views/podcastseries.blade.php
+++ b/resources/views/podcastseries.blade.php
@@ -53,7 +53,7 @@
@endif
-
diff --git a/resources/views/programlist.blade.php b/resources/views/programlist.blade.php
index e96afe30..2b4f03c3 100644
--- a/resources/views/programlist.blade.php
+++ b/resources/views/programlist.blade.php
@@ -90,7 +90,7 @@
@endif
-