summaryrefslogtreecommitdiff
path: root/layouts/shortcodes/image.html
blob: 4217f391b5721735810685ce3935d71fb5a31f2b (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
66
67
<!-- Read page and image params -->
{{- $max_width := .Page.Params.image_max_width }}

{{- $caption := .Inner }}
{{- $file := .Params.file }}
{{- $class := .Params.class }}
{{- $source := .Params.source }}
{{- $alt := .Params.alt }}
{{- $dir := .Page.Params.image_dir }}

<!-- Get the the image -->
{{- $path := printf "%s/%s" $dir $file}}
{{- $img := resources.Get $path }}

<!-- Decide on the desired 1x and 2x width -->
{{- $raw_width := $img.Width}}
{{- $final1x_width := (cond (gt $max_width $raw_width) $raw_width $max_width) }}

<!-- forced width -->
{{ if .Params.forced_width }}
  {{ $final1x_width = cast.ToInt  .Params.forced_width }}
{{ end  }}

{{- $final2x_width := math.Mul $final1x_width 2}}
{{ $ext := "jpg" }}
{{ if eq .Params.transparency "yes" }}
{{ $ext = "png" }}
{{ end }}

<!-- Generate 1x and 2x images -->
{{- $img1xproc := printf "%dx %s q90" $final1x_width $ext }}
{{- $img2xproc := printf "%dx %s q90" $final2x_width $ext }}
{{- $img1x := $img.Resize $img1xproc }}
{{- $img2x := $img.Resize $img2xproc }}

<!-- Generate raw, optimized img -->
{{- $imgproc := printf "x%d jpg q90" $raw_width }}
{{- $img_raw := $img.Resize $imgproc }}

<!-- Resulting HTML -->
<figure
  class="{{ $class }}"
>
  <a href="{{ $img_raw.Permalink }}">
    <img 
      alt="{{ $alt }}" 
      src="{{ $img1x.Permalink }}" 
      srcset="
              {{ $img1x.Permalink }} 1x,
              {{ $img2x.Permalink }} 2x
              "
      width="{{ $img1x.Width }}" 
      height="{{ $img1x.Height }}"
      loading="lazy"
      >
  </a>
  {{ if not (strings.Contains $caption "noop") }}
  <figcaption>
    {{ $caption }}

    {{- if $source }}
    <a href="{{ $source }}" target="_blank" title="source">[source]</a>
    {{ end }}
  </figcaption>
      {{ end }}
</figure>