-
Notifications
You must be signed in to change notification settings - Fork 268
/
image-feature.html
78 lines (62 loc) · 2.65 KB
/
image-feature.html
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
68
69
70
71
72
73
74
75
76
77
78
{{- $p := .Params }}
{{- $t := .Title }}
{{ with $p.featureImage }}
{{- $alt := $p.featureImageAlt | default $t -}}{{/* alt text */}}
{{- $cap := $p.featureImageCap | safeHTML -}}{{/* caption */}}
{{- $file := . | safeURL }}{{/* filename of image */}}
{{/* Check if page bundles are set. */}}
{{- $bundle := $.Site.Params.usePageBundles }}
{{ if eq $.Params.usePageBundles false }}
{{- $bundle = false }}
{{ end }}
{{ if eq $.Params.usePageBundles true }}
{{- $bundle = true }}
{{ end }}
{{- $fileWeb := "" -}}{{/* full path to image, relative to the web root */}}
{{- $fileDisk := "" -}}{{/* full path to image, relative to the file root */}}
{{ if eq $bundle true }}
{{- $fileWeb = (add $.Page.RelPermalink $file) -}}
{{- $fileDisk = (add "/content/" (add $.Page.File.Dir $file)) }}
{{ else }}
{{- $fileWeb = absURL . -}}
{{- $fileDisk = (add "/static/" $file) }}
{{ end }}
{{ if fileExists $fileDisk }}
{{- $img := imageConfig $fileDisk -}}{{/* image element (passed through imageConfig) */}}
<div class="post_featured">
<figure>
<picture>
{{/* Image is external. */}}
{{- if strings.HasPrefix $file "http" -}}
<img class="image_featured image_external" src="{{ $file }}" alt="{{ $alt }}" />
{{/* Image is internal. */}}
{{- else -}}
{{ range $ext := (slice "avif" "webp" "jxl") }}
{{ $dotext := print "." $ext }}
{{ $diskPath := $fileDisk }}
{{ $webPath := $fileWeb }}
{{ $diskPath = replace $diskPath ".png" $dotext }}
{{ $diskPath = replace $diskPath ".jpg" $dotext }}
{{ $diskPath = replace $diskPath ".jpeg" $dotext }}
{{ $diskPath = replace $diskPath ".gif" $dotext }}
{{ $webPath = replace $webPath ".png" $dotext }}
{{ $webPath = replace $webPath ".jpg" $dotext }}
{{ $webPath = replace $webPath ".jpeg" $dotext }}
{{ $webPath = replace $webPath ".gif" $dotext }}
{{ if fileExists $diskPath }}
<source srcset="{{ $webPath | safeURL }}" type="image/{{ $ext }}">
{{ end }}
{{ end }}
<img class="image_featured image_internal" src="{{ $fileWeb }}" alt="{{ $alt }}" width="{{ $img.Width }}" height="{{ $img.Height }}" />
{{- end -}}
{{/* Provide caption, if it is set. */}}
{{ with $cap -}}
<figcaption>{{ $cap }}</figcaption>
{{- end }}
</picture>
</figure>
</div>
{{ else }}
{{ partial "image-error" (dict "fileWeb" $fileWeb "fileDisk" $fileDisk "bundle" $bundle) }}
{{ end }}
{{ end }}