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
- <html lang="nl">, title without leading whitespace, no keywords meta
- Article wrapped in <article>, dates in <time datetime>
- 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 <noreply@anthropic.com>
This commit is contained in:
eric.busandClaude Opus 5.5 committed 2026-11-23 14:00:58 +01:00
1 parent ab3499e365
commit dbbdca504e
9 files changed
+223 -23

No files matched your search

+2
View File
@@ -41,6 +41,8 @@ class NewsController extends Controller
} elseif (isset($apiResult->source->blocks)) { } elseif (isset($apiResult->source->blocks)) {
$newsItem->content = $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'])); return view('newsitem', array_merge($this->getSidebareData(), ['type' => $apiResult->type, 'news' => $newsItem, 'metadata' => $newsItem->metadata, 'searchURL' => 'nieuws/zoeken']));
} }
} }
+34 -6
View File
@@ -1,35 +1,63 @@
<?php <?php
namespace Model; namespace Model;
/**
* Open Graph / article meta tags for a page.
*
* Properties are stored without prefix and rendered as og:<prop>, 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 { class MetaData {
private $data = ['type' => 'article', 'locale' => 'nl-nl']; private $data = ['type' => 'article', 'locale' => 'nl_NL'];
public function set($prop, $val) { public function set($prop, $val) {
if($val) { if($val) {
$this->data[$prop] = $val; $this->data[$prop] = self::clean($val);
} }
return $this; return $this;
} }
public function get($prop, $default = null) {
return $this->data[$prop] ?? $default;
}
public function append($prop, $val) { 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; $this->data[$prop] .= $val;
} }
return $this; 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) { 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]; $this->data[$prop] = $val . $this->data[$prop];
} }
return $this; return $this;
} }
public function metaTags() { public function metaTags() {
foreach($this->data as $prop => $val) { foreach($this->data as $prop => $values) {
$name = strpos($prop, ':') !== false && strpos($prop, 'image:') !== 0 ? $prop : "og:$prop";
foreach((array)$values as $val) {
if($val) { if($val) {
echo "<meta property=\"og:$prop\" content=\"" . htmlentities($val) . "\" />\n"; echo "<meta property=\"$name\" content=\"" . htmlspecialchars($val, ENT_QUOTES, 'UTF-8') . "\" />\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;
}
}
+100 -5
View File
@@ -96,15 +96,110 @@ class NewsItem extends Model {
} }
$this->url = "/nieuws/{$this->id}/" . parent::url_slug($this->title); $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()) $this->metadata = (new MetaData())
->set('title', $this->title) ->set('title', $this->plainTitle())
->set('description', strip_tags($this->excerpt())) ->set('description', $this->summary(200))
->set('image', isset($this->images) && count($this->images) ? $this->images[0]->url : null ) ->set('image', $images ? $images[0]['url'] : null)
->set('image:alt', $images ? $images[0]['title'] : null)
->set('video', isset($this->video) ? $this->video : null) ->set('video', isset($this->video) ? $this->video : null)
->set('audio', isset($this->podcast) ? $this->podcast->url : null) ->set('audio', isset($this->podcast) ? $this->podcast->url : null)
->set('article:published_time', $this->published->format('r')) ->set('article:published_time', $this->published ? $this->published->format(DATE_ATOM) : null)
->set('article:modified_time', $this->edited ? $this->edited->format('r') : 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('#<br\s*/?>|</p>|</h[1-6]>#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() { public function excerpt() {
+7 -3
View File
@@ -1,12 +1,14 @@
<head> <head>
@include('layouts._analytics') @include('layouts._analytics')
<title>@yield('title') | GOOI&meer</title> <title>{!! trim($__env->yieldContent('title')) !!} | GOOI&meer</title>
<!--meta--> <!--meta-->
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.2" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.2" />
<meta name="format-detection" content="telephone=no" /> <meta name="format-detection" content="telephone=no" />
<meta name="keywords" content="GOOI&meer,Radio,TV,Nieuws,Lokale omroep,Huizen,Blaricum,Eemnes,Laren,Hilversum,BEL-gemeenten,Streekomroep" /> <meta name="description" content="{{ (isset($metadata) && $metadata->get('description')) ? $metadata->get('description') : 'GOOI&meer is de streekomroep voor Hilversum, Huizen, Blaricum, Eemnes en Laren.' }}" />
<meta name="description" content="GOOI&meer is de streekomroep voor Hilversum, Huizen, Blaricum, Eemnes en Laren." /> {{-- Allow large image previews (Google Discover, Top Stories) --}}
<meta name="robots" content="max-image-preview:large" />
<link rel="canonical" href="{{ url()->current() }}" />
<!--style--> <!--style-->
<link href='//fonts.googleapis.com/css?family=Roboto:300,400,700' rel='stylesheet' type='text/css'> <link href='//fonts.googleapis.com/css?family=Roboto:300,400,700' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Roboto+Condensed:300,400,700' rel='stylesheet' type='text/css'> <link href='//fonts.googleapis.com/css?family=Roboto+Condensed:300,400,700' rel='stylesheet' type='text/css'>
@@ -35,6 +37,8 @@
->append('title', ' | GOOI&meer') ->append('title', ' | GOOI&meer')
->metaTags(); ->metaTags();
} ?> } ?>
<meta name="twitter:card" content="{{ (isset($metadata) && $metadata->get('image')) ? 'summary_large_image' : 'summary' }}" />
@stack('head')
@stack('styles') @stack('styles')
<link rel="icon" href="/favicon.ico" sizes="48x48"> <link rel="icon" href="/favicon.ico" sizes="48x48">
<link rel="icon" href="/favicon.svg" type="image/svg+xml"> <link rel="icon" href="/favicon.svg" type="image/svg+xml">
+1 -1
View File
@@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="nl">
@include('layouts._head') @include('layouts._head')
<body> <body>
@yield('content') @yield('content')
+1 -1
View File
@@ -2,7 +2,7 @@
@yield('content') @yield('content')
@endsection @endsection
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="nl">
@include('layouts._head') @include('layouts._head')
<body> <body>
<!-- Google Tag Manager (noscript) --> <!-- Google Tag Manager (noscript) -->
+3 -2
View File
@@ -11,12 +11,13 @@
@yield('breadcrumb_top') @yield('breadcrumb_top')
</div> </div>
<div class="col-12 col-md content_container"> <div class="col-12 col-md content_container">
<div class="full-width md_margin_top @yield('content_class')"> {{-- Pages can make this an <article> with @section('content_tag', 'article') --}}
<@yield('content_tag', 'div') class="full-width md_margin_top @yield('content_class')">
@yield('breadcrumb') @yield('breadcrumb')
@yield('tags') @yield('tags')
<h1 class="page_title">@yield('title')</h1> <h1 class="page_title">@yield('title')</h1>
@yield('content') @yield('content')
</div> </@yield('content_tag', 'div')>
</div> </div>
<div class="col-12 col-md-auto sidebar"> <div class="col-12 col-md-auto sidebar">
+9 -5
View File
@@ -6,6 +6,13 @@
@section('page_class')news_post post_container @endsection @section('page_class')news_post post_container @endsection
{{-- The article itself is an <article> element (title, details and text) --}}
@section('content_tag', 'article')
@push('head')
@include('partial/news_jsonld', ['news' => $news])
@endpush
@section('breadcrumb') @section('breadcrumb')
<ul class="bread_crumb"> <ul class="bread_crumb">
<li><a title="Home" href="/">Home</a></li> <li><a title="Home" href="/">Home</a></li>
@@ -32,13 +39,10 @@
<ul class="post_details clearfix"> <ul class="post_details clearfix">
<li class="detail date"> <li class="detail date">
<i class="fa-regular fa-clock"></i> <i class="fa-regular fa-clock"></i>
{{Formatter::relativeDate($news->published)}} om {{$news->published->format('H:i')}} <time datetime="{{$news->published->format(DATE_ATOM)}}">{{Formatter::relativeDate($news->published)}} om {{$news->published->format('H:i')}}</time>
@if($news->edited && ($news->edited->format('d m H i') != $news->published->format('d m H i'))) @if($news->edited && ($news->edited->format('d m H i') != $news->published->format('d m H i')))
| bijgewerkt: | bijgewerkt:
@if($news->edited->format('d m') != $news->published->format('d m')) <time datetime="{{$news->edited->format(DATE_ATOM)}}">@if($news->edited->format('d m') != $news->published->format('d m')){{strtolower(Formatter::relativeDate($news->edited))}} @endif om {{$news->edited->format('H:i')}} uur</time>
{{strtolower(Formatter::relativeDate($news->edited))}}
@endif
om {{$news->edited->format('H:i')}} uur
@endif @endif
</li> </li>
@if($news->author) @if($news->author)
@@ -0,0 +1,66 @@
{{--
Structured data (schema.org JSON-LD) for a news article: NewsArticle with
GOOI&meer as publisher, and the breadcrumb. Used by search engines (Google
News / Top Stories) and AI assistants to understand the article.
--}}
@php
$canonical = url()->current();
$absolute = function ($image) use ($imgBase) {
return $image['own'] ? $imgBase . $image['url'] : $image['url'];
};
$images = array_map($absolute, $news->imageList());
$tags = $news->tagTitles();
// "Saskia Plokker, Bart Koomen" / "A en B" -> separate authors;
// an affiliation in brackets ("Elsa Poorthuis (Gooi & Meer)") is dropped
$authors = [];
$authorText = preg_replace('/\s*\([^)]*\)/u', '', trim(strip_tags((string) $news->author)));
foreach (preg_split('/\s*(?:,|\s&\s|\s+en\s+)\s*/u', $authorText) as $name) {
if ($name !== '') {
$authors[] = ['@type' => 'Person', 'name' => $name];
}
}
$publisher = [
'@type' => 'NewsMediaOrganization',
'@id' => rtrim(url('/'), '/') . '/#organization',
'name' => 'GOOI&meer',
'url' => url('/'),
'logo' => [
'@type' => 'ImageObject',
'url' => url('/android-chrome-512x512.png'),
'width' => 512,
'height' => 512,
],
];
$article = array_filter([
'@type' => 'NewsArticle',
'@id' => $canonical . '#article',
'mainEntityOfPage' => ['@type' => 'WebPage', '@id' => $canonical],
'url' => $canonical,
'headline' => mb_substr($news->plainTitle(), 0, 110),
'description' => $news->summary(200),
'image' => $images ?: null,
'datePublished' => $news->published ? $news->published->format(DATE_ATOM) : null,
'dateModified' => ($news->edited ?: $news->published) ? ($news->edited ?: $news->published)->format(DATE_ATOM) : null,
'author' => $authors ?: $publisher,
'publisher' => $publisher,
'articleSection' => $tags[0] ?? null,
'keywords' => $tags ? implode(', ', $tags) : null,
'inLanguage' => 'nl-NL',
'isAccessibleForFree' => true,
]);
$breadcrumb = [
'@type' => 'BreadcrumbList',
'itemListElement' => [
['@type' => 'ListItem', 'position' => 1, 'name' => 'Home', 'item' => url('/')],
['@type' => 'ListItem', 'position' => 2, 'name' => 'Nieuws', 'item' => route('nieuws')],
['@type' => 'ListItem', 'position' => 3, 'name' => $news->plainTitle(), 'item' => $canonical],
],
];
$jsonld = ['@context' => 'https://schema.org', '@graph' => [$article, $breadcrumb]];
@endphp
<script type="application/ld+json">{!! json_encode($jsonld, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_PRETTY_PRINT) !!}</script>