fix: handle photo array in post.njk JSON-LD

The photo property can be an array for multi-photo posts.
Also avoid calling startsWith on non-string values.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ricardo
2026-01-28 17:50:16 +01:00
parent 0627a663c2
commit 0a0718c6b2

View File

@@ -93,7 +93,17 @@ withBlogSidebar: true
</span>
{# JSON-LD Structured Data for SEO #}
{% set postImage = photo or image or (content | extractFirstImage) %}
{# Handle photo as potentially an array #}
{% set postImage = photo %}
{% if postImage %}
{# If photo is an array, use first element (check if first element looks like a URL) #}
{% if postImage[0] and (postImage[0] | length) > 10 %}
{% set postImage = postImage[0] %}
{% endif %}
{% endif %}
{% if not postImage or postImage == "" %}
{% set postImage = image or (content | extractFirstImage) %}
{% endif %}
{% set postDesc = description | default(content | ogDescription(160)) %}
<script type="application/ld+json">
{
@@ -121,8 +131,8 @@ withBlogSidebar: true
"url": "{{ site.url }}/images/og-default.png"
}
},
"description": {{ postDesc | dump | safe }}{% if postImage %},
"image": ["{{ site.url }}{% if postImage.startsWith('/') %}{{ postImage }}{% else %}/{{ postImage }}{% endif %}"]{% endif %}
"description": {{ postDesc | dump | safe }}{% if postImage and postImage != "" and (postImage | length) > 10 %},
"image": ["{{ site.url }}{% if '/' in postImage and postImage[0] == '/' %}{{ postImage }}{% else %}/{{ postImage }}{% endif %}"]{% endif %}
}
</script>
</article>