summaryrefslogtreecommitdiff
path: root/themes/PaperMod/layouts/partials/cover.html
blob: 9c1f9eaa0b1537b129e94387257ae1589765f602 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{{- with .cxt}} {{/* Apply proper context from dict */}}
{{- if (and .Params.cover.image (not $.isHidden)) }}
<figure class="entry-cover">
    {{- $loading := cond $.IsSingle "eager" "lazy" }}
    {{- $addLink := (and site.Params.cover.linkFullImages $.IsSingle) }}
    {{- $prod := (hugo.IsProduction | or (eq site.Params.env "production")) }}
    {{- $alt := (.Params.cover.alt | default .Params.cover.caption | plainify) }}
    {{- $responsiveImages := (.Params.cover.responsiveImages | default site.Params.cover.responsiveImages) | default true }}

    {{- $pageBundleCover := (.Resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
    {{- $globalResourcesCover := (resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
    {{- $cover := (or $pageBundleCover $globalResourcesCover)}}
    {{- /* We are not using the .Param.cover.relative to decide the location of image */}}
    {{- /* If we have the image in pageBundle or globalResources we can process the image */}}

    {{- $sizes := (slice "360" "480" "720" "1080" "1500") }}
    {{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "bmp" "gif") -}}
    {{- if hugo.IsExtended -}}
        {{- $processableFormats = $processableFormats | append "webp" -}}
    {{- end -}}

    {{- $imgdl := (.Params.cover.image) | absURL }}
    {{- if $cover -}}
        {{- $imgdl = $cover.Permalink }}
    {{- end -}}

    {{- if $addLink }}
        <a href="{{ $imgdl }}" target="_blank" rel="noopener noreferrer">
    {{- end }}

    {{- if $cover -}}
        {{/* i.e it is present in page bundle */}}
        {{- if (and (in $processableFormats $cover.MediaType.SubType) ($responsiveImages) (eq $prod true)) }}
            <img loading="{{$loading}}"
                srcset='{{- range $size := $sizes -}}
                            {{- if (ge $cover.Width $size) }}
                                {{- printf "%s %s" (($cover.Resize (printf "%sx" $size)).Permalink) (printf "%sw," $size) }}
                            {{- end }}
                        {{- end }}
                        {{- printf "%s %dw" ($cover.Permalink) ($cover.Width) }}'
                src="{{ $cover.Permalink }}"
                sizes="(min-width: 768px) 720px, 100vw"
                width="{{ $cover.Width }}" height="{{ $cover.Height }}"
                alt="{{ $alt }}">
        {{- else }}{{/* Unprocessable image or responsive images disabled */}}
            <img loading="{{ $loading }}" src="{{ $imgdl }}" alt="{{ $alt }}">
        {{- end }}
    {{- else }}
        {{- /* For absolute urls and external links, no img processing here */}}
        <img loading="{{ $loading }}" src="{{ $imgdl }}" alt="{{ $alt }}">
    {{- end }}

    {{- if $addLink }}
        </a>
    {{- end -}}

    {{- /*  Display Caption  */}}
    {{- if $.IsSingle }}
        {{ with .Params.cover.caption -}}
            <figcaption>{{ . | markdownify }}</figcaption>
        {{- end }}
    {{- end }}
</figure>
{{- end }}{{/* End image */}}
{{- end -}}{{/* End context */ -}}