Skip to content

Commit b9add1c

Browse files
jmooringbep
authored andcommitted
tpl/tplimpl: Add loading attribute to Vimeo shortcode
Closes #13445
1 parent b068671 commit b9add1c

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

Diff for: tpl/tplimpl/embedded/templates/shortcodes/vimeo.html

+52-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
1-
{{- $pc := site.Config.Privacy.Vimeo -}}
2-
{{- if not $pc.Disable -}}
3-
{{- if $pc.Simple -}}
4-
{{ template "_internal/shortcodes/vimeo_simple.html" . }}
5-
{{- else -}}
6-
{{ if .IsNamedParams }}<div {{ if .Get "class" }}class="{{ .Get "class" }}"{{ else }}style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;"{{ end }}>
7-
<iframe src="https://player.vimeo.com/video/{{ .Get "id" }}{{- if $pc.EnableDNT -}}?dnt=1{{- end -}}" {{ if not (.Get "class") }}style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" {{ end }}{{ if .Get "title"}}title="{{ .Get "title" }}"{{ else }}title="vimeo video"{{ end }} webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
8-
</div>{{ else }}
9-
<div {{ if gt (len .Params) 1 }}class="{{ .Get 1 }}"{{ else }}style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;"{{ end }}>
10-
<iframe src="https://player.vimeo.com/video/{{ .Get 0 }}{{- if $pc.EnableDNT -}}?dnt=1{{- end -}}" {{ if len .Params | eq 1 }}style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" {{ end }}{{ if len .Params | eq 3 }}title="{{ .Get 2 }}"{{ else }}title="vimeo video"{{ end }} webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
11-
</div>
12-
{{ end }}
13-
{{- end -}}
14-
{{- end -}}
1+
{{- /*
2+
Renders an embedded Vimeo video.
3+
4+
Accepts named or positional arguments. If positional, order is id, class,
5+
title, then loading.
6+
7+
@param {string} [class] The class attribute of the wrapping div element. When specified, removes the style attributes from the iframe element and its wrapping div element.
8+
@param {string} [id] The video id. Optional if the id is provided as first positional argument.
9+
@param {string} [loading=eager] The loading attribute of the iframe element.
10+
@param {string} [title=Vimeo video] The title attribute of the iframe element.
11+
12+
@returns {template.HTML}
13+
14+
@example {{< vimeo 55073825 >}}
15+
@example {{< vimeo id=55073825 class="foo bar" loading=lazy title="My Video" >}}
16+
*/}}
17+
{{- $pc := site.Config.Privacy.Vimeo }}
18+
{{- if not $pc.Disable }}
19+
{{- if $pc.Simple }}
20+
{{- template "_internal/shortcodes/vimeo_simple.html" . }}
21+
{{- else }}
22+
{{- $id := or (.Get "id") (.Get 0) "" }}
23+
{{- $class := or (.Get "class") (.Get 1) "" }}
24+
{{- $title := or (.Get "title") (.Get 2) "Vimeo video" }}
25+
{{- $loading := or (.Get "loading") (.Get 3) "eager" }}
26+
{{- $dnt := cond $pc.EnableDNT 1 0 }}
27+
28+
{{- $divStyle := "position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;" }}
29+
{{- $iframeStyle := "position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" }}
30+
31+
{{- with $id }}
32+
{{- $src := printf "https://player.vimeo.com/video/%v?dnt=%v" . $dnt }}
33+
<div
34+
{{- with $class }}
35+
class="{{ . }}"
36+
{{- else }}
37+
style="{{ $divStyle | safeCSS }}"
38+
{{- end }}>
39+
<iframe webkitallowfullscreen mozallowfullscreen allowfullscreen
40+
src="{{- $src }}"
41+
{{- if not $class }}
42+
style="{{ $iframeStyle | safeCSS }}"
43+
{{- end }}
44+
{{- with $loading }} loading="{{ . }}" {{- end }}
45+
{{- with $title }} title="{{ . }}" {{- end }}>
46+
</iframe>
47+
</div>
48+
{{- else }}
49+
{{- errorf "The %q shortcode requires a video id, either as the first positional argument or an argument named id. See %s" .Name .Position }}
50+
{{- end }}
51+
{{- end }}
52+
{{- end }}

Diff for: tpl/tplimpl/shortcodes_integration_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,12 @@ Content: {{ .Content }}
478478

479479
// Regular mode
480480
b := hugolib.Test(t, files)
481-
b.AssertFileContent("public/index.html", "d5b2a079cc37d0ed")
481+
b.AssertFileContent("public/index.html", "d1f592d2256ac3ff")
482482

483483
// Simple mode
484484
files = strings.ReplaceAll(files, "privacy.vimeo.simple = false", "privacy.vimeo.simple = true")
485485
b = hugolib.Test(t, files)
486-
b.AssertFileContent("public/index.html", "73b8767ce8bdf694")
486+
b.AssertFileContent("public/index.html", "c5bf16d87e2a370b")
487487

488488
// Simple mode with non-existent id
489489
files = strings.ReplaceAll(files, "{{< vimeo 55073825 >}}", "{{< vimeo __id_does_not_exist__ >}}")

0 commit comments

Comments
 (0)