mirror of https://github.com/theNewDynamic/gohugo-theme-ananke.git

Patrick Kollitsch
10 hours ago c70b22c0c65d21fc25eda46a8182eb042da11da0
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
{{/* Return a background style shared by home, list, and individual-page headers.
     Static URLs and unsupported image formats retain their original URL. */}}
{{ $url := partials.Include "func/GetFeaturedImage.html" . }}
{{ $page := . }}
{{ $stylesheet := "" }}
{{ $style := printf "background-image: url('%s');" $url }}
{{ with partials.Include "func/GetFeaturedImageResource.html" . }}
  {{ if in (slice "jpeg" "png" "webp" "tiff" "bmp" "gif") .MediaType.SubType }}
    {{ $image := . }}
    {{ $widths := ($page.Param "ananke.responsive_image_widths") | default (slice 480 960 1440 1920) }}
    {{ $candidates := slice }}
    {{ range sort $widths }}
      {{ if lt (int .) $image.Width }}
        {{ $resized := $image.Resize (printf "%dx" (int .)) }}
        {{ $candidates = $candidates | append (dict "width" (int .) "url" $resized.RelPermalink) }}
      {{ end }}
    {{ end }}
    {{ $candidates = $candidates | append (dict "width" $image.Width "url" $image.RelPermalink) }}
    {{/* Select the next sufficient width without upscaling the source. */}}
    {{ $id := printf "ananke-header-%s" (crypto.MD5 $image.RelPermalink) }}
    {{ $first := index $candidates 0 }}
    {{ $style = printf "background-image: var(--%s, url('%s'));" $id $first.url }}
    {{ $css := printf ":root { --%s: url('%s'); }" $id $first.url }}
    {{ range $index, $candidate := $candidates }}
      {{ if gt $index 0 }}
        {{ $previous := index $candidates (sub $index 1) }}
        {{ $css = printf "%s @media (min-width: %dpx) { :root { --%s: url('%s'); } }" $css (add $previous.width 1) $id $candidate.url }}
      {{ end }}
    {{ end }}
    {{/* Publish a shared stylesheet for each image and width configuration. */}}
    {{ $cssResource := resources.FromString (printf "ananke/header-%s.css" (crypto.MD5 $css)) $css }}
    {{ $stylesheet = $cssResource.RelPermalink }}
  {{ end }}
{{ end }}
{{ return (dict "style" $style "stylesheet" $stylesheet) }}