Skip to content

Commit 8b67740

Browse files
authored
fix: add code comments
Add comments to the code, and take url parsing from adityatelange#1651. Hope @valnaumov will forgive me.
1 parent f992f1b commit 8b67740

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

layouts/shortcodes/figure.html

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
{{/*
2+
Usage:
3+
{{< figure
4+
class="specificclass"
5+
align="center" // this adds "center" to the class of the image
6+
src="sourceImage" // Image location. May be in the page bundle, in the site resources, or be external to the site (eg: https://other.site/image.png)
7+
link="targetOfTheImageLink" // Makes the image into a link
8+
linkFullImage="false // If link is NOT provided, AND this parameter is "true",
9+
// then makes the image into a link to the full size image
10+
responsive="false" // If the image is found in the page bundle or in the site resources
11+
// AND if the environment is "production"
12+
// AND if the image is in a processable format
13+
// AND if this parameter is "true", then smaller images are created,
14+
// responsiveness is provided by way of srcset, the browser chooses the image size
15+
title="Title of the image"
16+
alt="Alternative text"
17+
caption="Text to appear below the image"
18+
attr="End text of the caption"
19+
attrlink="targetURLForAttr" // If provided, turns the end text of the caption into a link
20+
>}}
21+
*/}}
122
<figure{{ if or (.Get "class") (eq (.Get "align") "center") }} class="
223
{{- if eq (.Get "align") "center" }}align-center {{ end }}
324
{{- with .Get "class" }}{{ . }}{{- end }}"
@@ -9,31 +30,31 @@
930
{{- $processableFormats = $processableFormats | append "webp" -}}
1031
{{- end -}}
1132

33+
{{- /* Find the image in the page bundle or in the site resources */ -}}
1234
{{- $image := "" -}}
13-
{{- if $.Page.Resources -}}
14-
{{- $image = $.Page.Resources.Get (.Get "src") -}}
35+
{{- $imageURL := urls.Parse (.Get "src") -}}
36+
{{- $imageSrc := $imageURL.String -}}
37+
{{- if not $imageURL.IsAbs -}}
38+
{{- $image = or (.Page.Resources.Get $imageURL.Path) (resources.Get $imageURL.Path) -}}
39+
{{- $imageSrc = $image.RelPermalink -}}
1540
{{- end -}}
16-
{{- if not $image -}}
17-
{{- $image = resources.Get (.Get "src") -}}
18-
{{- end -}}
19-
{{- $imageSrc := $image.RelPermalink | default ( .Get "src" ) -}}
2041

42+
{{/* Make the image into a link, provided by the developer, or a link into itself in its original size */}}
2143
{{- $linkURL := .Get "link" -}}
2244
{{- $linkFullImage := .Get "linkFullImage" | default "false" | eq "true" -}}
2345
{{- if and $linkFullImage (not $linkURL) $image -}}
2446
{{- $linkURL = $image.RelPermalink -}}
2547
{{- end -}}
2648

27-
{{- $responsive := .Get "responsive" | default "false" | eq "true" -}}
28-
29-
{{- $responsive = and $prod $image $responsive ( in $processableFormats $image.MediaType.SubType ) -}}
30-
3149
{{- if $linkURL -}}
3250
<a href="{{ $linkURL }}"{{ with .Get "target" }} target="{{ . }}"{{ end }}{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>
3351
{{- end }}
3452

53+
{{- $responsive := .Get "responsive" | default "false" | eq "true" -}}
54+
{{- $responsive = and $prod $image $responsive ( in $processableFormats $image.MediaType.SubType ) -}}
55+
3556
{{- if $responsive }}
36-
{{- $sizes := (slice "360" "480" "720" "1080" "1500") }}
57+
{{- $sizes := (slice "360" "480" "720" "1080" "1500") }}
3758
<img loading="lazy"
3859
srcset='{{- range $size := $sizes -}}
3960
{{- if (ge $image.Width (int $size)) }}

0 commit comments

Comments
 (0)