Skip to content

Commit 28d1a76

Browse files
committed
add width and height handlers to figure shortcode
1 parent 3845a41 commit 28d1a76

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

layouts/shortcodes/tmfigure.html

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{{/* layouts/shortcodes/tmfigure.html */}}
2-
<figure{{ with .Get "class" }} class="{{ . }}"{{ end }}>
2+
{{- $figureStyleParts := slice "padding-top: 0.5em;" -}}
3+
{{- with .Get "style" -}}
4+
{{- $figureStyleParts = $figureStyleParts | append . -}}
5+
{{- end -}}
6+
{{- $figureStyleAttr := printf ` style="%s"` (delimit $figureStyleParts " ") -}}
7+
<figure{{ with .Get "class" }} class="{{ . }}"{{ end }}{{ $figureStyleAttr | safeHTMLAttr }}>
38
{{- if .Get "link" -}}
49
<a href="{{ .Get "link" }}"{{ with .Get "target" }} target="{{ . }}"{{ end }}{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>
510
{{- end }}
@@ -10,12 +15,32 @@
1015
{{- else }}
1116
{{/* Fallback for external or static images */}}
1217
{{- end }}
18+
{{- $width := $.Get "width" -}}
19+
{{- $height := $.Get "height" -}}
20+
{{- $widthIsNumber := and $width (findRE "^[0-9]+$" $width) -}}
21+
{{- $heightIsNumber := and $height (findRE "^[0-9]+$" $height) -}}
22+
{{- $styleParts := slice -}}
23+
{{- if and $width (not $widthIsNumber) -}}
24+
{{- $styleParts = $styleParts | append (printf "width: %s;" $width) -}}
25+
{{- end -}}
26+
{{- if and $height (not $heightIsNumber) -}}
27+
{{- $styleParts = $styleParts | append (printf "height: %s;" $height) -}}
28+
{{- end -}}
29+
{{- if and $width (not $height) -}}
30+
{{- $styleParts = $styleParts | append "height: auto;" -}}
31+
{{- end -}}
32+
{{- $styleParts = $styleParts | append "display: block;" "margin-left: auto;" "margin-right: auto;" -}}
33+
{{- $styleAttr := "" -}}
34+
{{- if gt (len $styleParts) 0 -}}
35+
{{- $styleAttr = printf ` style="%s"` (delimit $styleParts " ") -}}
36+
{{- end -}}
1337
<img src="{{$src | safeURL}}"
1438
{{- if or ($.Get "alt") ($.Get "caption") }}
1539
alt="{{ with $.Get "alt" }}{{ . }}{{ else }}{{ $.Get "caption" | markdownify | plainify }}{{ end }}"
1640
{{- end -}}
17-
{{- with $.Get "width" }} width="{{ . }}"{{ end -}}
18-
{{- with $.Get "height" }} height="{{ . }}"{{ end -}}
41+
{{- if $widthIsNumber }} width="{{ $width }}"{{ end -}}
42+
{{- if $heightIsNumber }} height="{{ $height }}"{{ end -}}
43+
{{- $styleAttr | safeHTMLAttr -}}
1944
/>
2045
{{- end }}
2146

@@ -34,4 +59,4 @@ <h4 style="margin-bottom: 0.5em;">{{ . }}</h4>
3459
{{ end }}
3560
</figcaption>
3661
{{- end }}
37-
</figure>
62+
</figure>

0 commit comments

Comments
 (0)