{{ $permalink := .Permalink }}
{{- with $.Params.images -}}
    <meta name="twitter:card" content="summary_large_image"/>
    <!-- As per https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/summary-card-with-large-image,
         the required card property is "twitter:image", not "twitter:image:src". -->
    {{- $image := (index . 0) -}}
    {{- $image_link_absolute := (findRE "^/" $image) -}}
    {{- if $image_link_absolute -}}
        <meta name="twitter:image" content="{{- $image | absURL -}}"/>
    {{- else -}}
        <meta name="twitter:image" content="{{- (printf "%s%s" $permalink $image) -}}"/>
    {{- end -}}
{{ else -}}
    {{- $images := $.Resources.ByType "image" -}}
    {{- $featured := $images.GetMatch "*feature*" -}}
    {{- $featured := cond (ne $featured nil) $featured ($images.GetMatch "{*cover*,*thumbnail*}") -}}
    {{- with $featured -}}
        <meta name="twitter:card" content="summary_large_image"/>
        <meta name="twitter:image" content="{{ $featured.Permalink }}"/>
    {{- else -}}
        <meta name="twitter:card" content="summary"/>
        {{- with site.Params.images -}}
            {{- $image := (index . 0) -}}
            {{- $image_link_absolute := (findRE "^/" $image) -}}
            {{- if $image_link_absolute -}}
                <meta name="twitter:image" content="{{- $image | absURL -}}"/>
            {{- else -}}
                <meta name="twitter:image" content="{{- (printf "%s%s" $permalink $image) -}}"/>
            {{- end -}}
        {{- end -}}
    {{- end -}}
{{- end }}
<meta name="twitter:title" content="{{ .Title }}"/>
<meta name="twitter:description" content="{{ with .Description }}{{ . | plainify }}{{ else }}{{ if .IsPage }}{{ partial "summary_minus_toc.html" . | plainify }}{{ else }}{{ with site.Params.description }}{{ . | plainify }}{{ end }}{{ end }}{{ end -}}"/>
{{ with site.Social.twitter -}}
    <meta name="twitter:site" content="@{{ . }}"/>
    <meta name="twitter:creator" content="@{{ . }}"/>
{{ end -}}
