{% for meta in page.metadata %}
  <meta {% if meta.name %} name="{{ meta.name }}" {% endif %} {% if meta.http_equiv %} http-equiv="{{ meta.http_equiv }}" {% endif %} {% if meta.charset %} charset="{{ meta.charset }}" {% endif %} {% if meta.property %} property="{{ meta.property }}" {% endif %} {% if meta.content %} content="{{ meta.content }}" {% endif %}/>
{% endfor %}

{% set image = page.media[page.header.primaryImage] ?: page.media.all|filter((v, k) => k != page.header.avatarImage and (v.type == 'image' or v.type == 'vector'))|first %}

{# Open Graph #}
{# Partial code from https://github.com/paulcmal/grav-plugin-socialmeta #}

{% if not page.metadata["og:sitename"] %}
<meta name="og:site_name" property="og:site_name" content="{{ site.title|raw }}" />
{% endif %}
{% if not page.metadata["og:title"] %}
<meta name="og:title" property="og:title" content="{{ page.title|raw }}" />
{% endif %}
{% if not page.metadata["og:description"] %}
<meta name="og:description" property="og:description" content="{{ page.summary|striptags|truncate(150)|raw }}" />
{% endif %}
{% if not page.metadata["og:type"] %}
<meta name="og:type" property="og:type" content="article" />
{% endif %}
{% if not page.metadata["og:url"] %}
<meta name="og:url" property="og:url" content="{{ page.url(true)|e }}" />
{% endif %}
{% if not page.metadata["og:image"] and image %}
<meta name="og:image" property="og:image" content="{{ uri.base ~ image.url|raw }}" />
{% endif %}

{# Twitter Cards #}
{# Partial code from https://github.com/paulcmal/grav-plugin-socialmeta #}

{% if not page.metadata["twitter:card"] %}
<meta name="twitter:card" property="twitter:card" content="summary" />
{% endif %}
{% if not page.metadata["twitter:site"] %}
	{% for item in theme_config.social if item.name|lower == 'twitter' %}
    <meta name="twitter:site" property="twitter:site" content="{{ item.url }}" />
  {% endfor %}
{% endif %}
{% if not page.metadata["twitter:title"] %}
<meta name="twitter:title" property="twitter:title" content="{{ page.title|raw }}" />
{% endif %}
{% if not page.metadata["twitter:description"] %}
<meta name="twitter:description" property="twitter:description" content="{{ page.summary|striptags|truncate(150)|raw }}" />
{% endif %}
{% if not page.metadata["twitter:image"] and image %}
<meta name="twitter:image" property="twitter:image" content="{{ uri.base ~ image.url|raw }}" />
{% endif %}