summaryrefslogtreecommitdiffstats
path: root/tpl/tplimpl/embedded/templates/_shortcodes/youtube.html
blob: 18b0869445f877d512c263369fc60a003daa327f (plain) (blame)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{{- /*
Renders an embedded YouTube video.

@param {bool} [allowFullScreen=true] Whether the iframe element can activate full screen mode.
@param {bool} [autoplay=false] Whether to automatically play the video. Forces mute to be true.
@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.
@param {bool} [controls=true] Whether to display the video controls.
@param {int} [end] The time, measured in seconds from the start of the video, when the player should stop playing the video.
@param {string} [id] The video id. Optional if the id is the first and only positional argument.
@param {string} [loading=eager] The loading attribute of the iframe element.
@param {bool} [loop=false] Whether to indefinitely repeat the video. Ignores the start and end arguments after the first play.
@param {bool} [mute=false] Whether to mute the video. Always true when autoplay is true.
@param {int} [start] The time, measured in seconds from the start of the video, when the player should start playing the video.
@param {string} [title] The title attribute of the iframe element. Defaults to "YouTube video".

@returns {template.HTML}

@reference https://developers.google.com/youtube/player_parameters

@example {{< youtube 0RKpf3rK57I >}}
@example {{< youtube id=0RKpf3rK57I loading=lazy start=30 >}}
*/}}

{{- $pc := .Page.Site.Config.Privacy.YouTube }}
{{- $remoteErrID := "err-youtube-remote" }}
{{- if not $pc.Disable }}
  {{- with $id := or (.Get "id") (.Get 0) }}

    {{- /* Set defaults. */}}
    {{- $allowFullScreen := true }}
    {{- $autoplay := 0 }}
    {{- $class := "" }}
    {{- $controls := 1 }}
    {{- $end := 0 }}
    {{- $loading := "eager" }}
    {{- $loop := 0 }}
    {{- $mute := 0 }}
    {{- $start := 0 }}
    {{- $title := "YouTube video" }}
    {{- $iframeAllowList := "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" }}

    {{- /* Get arguments. */}}
    {{- if in (slice "true" true 1) ($.Get "allowFullScreen") }}
      {{- $allowFullScreen = true }}
    {{- else if in (slice "false" false 0) ($.Get "allowFullScreen") }}
      {{- $allowFullScreen = false }}
    {{- end }}
    {{- if in (slice "true" true 1) ($.Get "autoplay") }}
      {{- $autoplay = 1 }}
    {{- else if in (slice "false" false 0) ($.Get "autoplay") }}
      {{- $autoplay = 0 }}
    {{- end }}
    {{- if in (slice "true" true 1) ($.Get "controls") }}
      {{- $controls = 1 }}
    {{- else if in (slice "false" false 0) ($.Get "controls") }}
      {{- $controls = 0 }}
    {{- end }}
    {{- if in (slice "true" true 1) ($.Get "loop") }}
      {{- $loop = 1 }}
    {{- else if in (slice "false" false 0) ($.Get "loop") }}
      {{- $loop = 0 }}
    {{- end }}
    {{- if or (in (slice "true" true 1) ($.Get "mute")) $autoplay }}
      {{- $mute = 1 }}
    {{- else if in (slice "false" false 0) ($.Get "mute") }}
      {{- $mute = 0 }}
    {{- end }}
    {{- $class := or ($.Get "class") $class }}
    {{- $end := or ($.Get "end") $end }}
    {{- $loading := or ($.Get "loading") $loading }}
    {{- $start := or ($.Get "start") $start }}
    {{- $title := or ($.Get "title") $title }}

    {{- /* Adjust iframeAllowList. */}}
    {{- if $allowFullScreen }}
      {{- $iframeAllowList = printf "%s; fullscreen" $iframeAllowList }}
    {{- end }}

    {{- /* Define src attribute. */}}
    {{- $host := cond $pc.PrivacyEnhanced "www.youtube-nocookie.com" "www.youtube.com" }}
    {{- $src := printf "https://%s/embed/%s" $host $id }}
    {{- $params := dict
      "autoplay" $autoplay
      "controls" $controls
      "end" $end
      "mute" $mute
      "start" $start
      "loop" $loop
    }}
    {{- if $loop }}
      {{- $params = merge $params (dict "playlist" $id) }}
    {{- end }}
    {{- with querify $params }}
      {{- $src = printf "%s?%s" $src . }}
    {{- end }}

    {{- /* Set div attributes. */}}
    {{- $divStyle := "position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;" }}
    {{- if $class }}
      {{- $divStyle = "" }}
    {{- end }}

    {{- /* Set iframe attributes. */}}
    {{- $iframeStyle := "position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" }}
    {{- if $class }}
      {{- $iframeStyle = "" }}
    {{- end }}
    {{- $referrerpolicy := "strict-origin-when-cross-origin" }}

    {{- /* Render. */ -}}
    <div
      {{- with $class }} class="{{ . }}" {{- end }}
      {{- with $divStyle }} style="{{ . | safeCSS }}" {{- end -}}
    >
      <iframe
        {{- with $iframeAllowList }} allow="{{ . }}" {{- end }}
        {{- with $loading }} loading="{{ . }}" {{- end }}
        {{- with $referrerpolicy }} referrerpolicy="{{ . }}" {{- end }}
        {{- with $src }} src="{{ . }}" {{- end }}
        {{- with $iframeStyle}} style="{{ . | safeCSS }}" {{- end }}
        {{- with $title }} title="{{ . }}" {{- end -}}
      ></iframe>
    </div>
  {{- else }}
    {{- errorf "The %q shortcode requires an id argument. See %s" .Name .Position }}
  {{- end }}
{{- end }}