diff --git a/modules/youtube/config/_default/params.toml b/modules/youtube/config/_default/params.toml index c039883c1..c69f26bb1 100755 --- a/modules/youtube/config/_default/params.toml +++ b/modules/youtube/config/_default/params.toml @@ -6,6 +6,12 @@ thumbnail_source = 1 # disabled = false # verbose = true +[dnb.youtube.apiparams] +# see https://developers.google.com/youtube/player_parameters#Parameters +iv_load_policy = 3 +controls = 0 +rel = 0 + [dnb.youtube.config.plugins.js] imports = ["import LiteYTEmbed from './lite-yt-embed';"] # es6, in flux calls = ["customElements.define('lite-youtube', LiteYTEmbed);"] diff --git a/modules/youtube/layouts/partials/func/getYTPreviewUrl.html b/modules/youtube/layouts/partials/func/getYTPreviewUrl.html new file mode 100644 index 000000000..5d2f603ce --- /dev/null +++ b/modules/youtube/layouts/partials/func/getYTPreviewUrl.html @@ -0,0 +1,14 @@ +{{- $config := site.Params.dnb.youtube -}} + +{{- $id := . -}} + +{{- $url := "https://i.ytimg.com/vi" -}} +{{- if compare.Eq (collections.Index $config "thumbnail_source" | compare.Default 1) 1 -}} + {{- $url = "https://ytimg.googleusercontent.com/vi" -}} +{{- end -}} + +{{- $output := dict + "maxres" (printf "%s/%s/maxresdefault.jpg" $url $id) + "lowres" (printf "%s/%s/sddefault.jpg" $url $id) -}} + +{{- return $output -}} diff --git a/modules/youtube/layouts/partials/func/prepareApiParams.html b/modules/youtube/layouts/partials/func/prepareApiParams.html new file mode 100644 index 000000000..a095a5e9d --- /dev/null +++ b/modules/youtube/layouts/partials/func/prepareApiParams.html @@ -0,0 +1,7 @@ +{{- $params := site.Params.dnb.youtube.apiparams -}} +{{- $params = collections.Merge (dict "hl" site.Language.Lang) $params -}} +{{- $paramsCollected := "" -}} +{{- range $key, $value := $params -}} + {{- $paramsCollected = fmt.Printf "%s&%s=%s" $paramsCollected $key $value -}} +{{- end -}} +{{- return $params -}} diff --git a/modules/youtube/layouts/partials/getYoutubePreviewUrl.html b/modules/youtube/layouts/partials/getYoutubePreviewUrl.html deleted file mode 100644 index 7ff9c2652..000000000 --- a/modules/youtube/layouts/partials/getYoutubePreviewUrl.html +++ /dev/null @@ -1,10 +0,0 @@ -{{- $config := site.Params.dnb.youtube -}} -{{- $url := "" -}} - -{{- if compare.Eq (collections.Index $config "thumbnail_source" | compare.Default 1) 1 -}} - {{- $url = "https://ytimg.googleusercontent.com/vi/%s/maxresdefault.jpg" -}} -{{- else -}} - {{- $url = "https://i.ytimg.com/vi/%s/maxresdefault.jpg" -}} -{{- end -}} - -{{- return $url -}} diff --git a/modules/youtube/layouts/partials/youtube.html b/modules/youtube/layouts/partials/youtube.html index 875252e80..16ff40df8 100644 --- a/modules/youtube/layouts/partials/youtube.html +++ b/modules/youtube/layouts/partials/youtube.html @@ -1,12 +1,5 @@ {{- /* - see - - https://web.dev/iframe-lazy-loading/ (not used collections.In this shortcode, just for - learning purposes) - - https://github.com/paulirish/lite-youtube-embed - - https://developers.google.com/youtube/player_parameters#Parameters (check for - parameters that can be used with the youtube player) - - @todo background image locally instead of URL - @todo config param for JS inline or via external file - @todo documentation about $pc.Disable @@ -15,36 +8,20 @@ */ -}} -{{- $params := dict - "modestbranding" "2" - "iv_load_policy" "3" - "controls" "0" - "rel" "0" - "hl" site.Language.Lang --}} -{{- $paramsCollected := "" -}} -{{- range $key, $value := $params -}} - {{- $paramsCollected = fmt.Printf "%s&%s=%s" $paramsCollected $key $value -}} -{{- end -}} + {{- $pc := site.Config.Privacy.YouTube -}} {{- if not $pc.Disable -}} {{- $id := .id -}} + {{- $jsapi := .jsapi | compare.Default false -}} {{- $class := .class | compare.Default "" -}} {{- $title := .title | compare.Default "" -}} - {{- $params := .params | compare.Default $paramsCollected -}} + {{- $playlabel := .playlabel | compare.Default "Play video" -}} + {{- $params := .params | compare.Default (partialCached "func/prepareApiParams.html" .) -}} {{- $config := site.Params.dnb.youtube -}} - {{- $thumbnailUrl := "https://i.ytimg.com/vi/%s/maxresdefault.jpg" -}} - {{- $thumbnailLowResUrl := "https://i.ytimg.com/vi/%s/sddefault.jpg" -}} - - {{- if compare.Eq (collections.Index $config "thumbnail_source" | compare.Default 1) 2 -}} - {{- $thumbnailUrl = "https://ytimg.googleusercontent.com/vi/%s/maxresdefault.jpg" -}} - {{- $thumbnailLowResUrl = "https://ytimg.googleusercontent.com/vi/%s/sddefault.jpg" -}} - {{- end -}} - {{- $remoteThumbnailUrl := fmt.Printf $thumbnailUrl $id -}} - {{- $remoteThumbnailLowresUrl := fmt.Printf $thumbnailLowResUrl $id -}} {{- $remoteThumbnail := "" -}} + {{- $thumbnailUrl := partials.IncludeCached "func/getYTPreviewUrl.html" $id $id -}} - {{ with resources.GetRemote $remoteThumbnailUrl }} + {{ with resources.GetRemote $thumbnailUrl.maxres }} {{ with .Err }} {{ warnf "%s" . }} {{ else }} @@ -54,7 +31,7 @@ {{ end -}} {{ end }} {{ else }} - {{ with resources.GetRemote $remoteThumbnailLowresUrl }} + {{ with resources.GetRemote $thumbnailUrl.lowres }} {{ with .Err }} {{ warnf "%s" . }} {{ else }} @@ -75,14 +52,15 @@
- diff --git a/modules/youtube/layouts/shortcodes/youtube.html b/modules/youtube/layouts/shortcodes/youtube.html index b3f246de1..249323f36 100755 --- a/modules/youtube/layouts/shortcodes/youtube.html +++ b/modules/youtube/layouts/shortcodes/youtube.html @@ -16,34 +16,23 @@ Maybe do the whole implementation via partials.Include that can be reused by the youtube partials.Include and shortcode */ -}} -{{- $paramScratch := newScratch -}} -{{- $paramScratch.SetInMap "params" "modestbranding" 2 -}} -{{- $paramScratch.SetInMap "params" "iv_load_policy" 3 -}} -{{- $paramScratch.SetInMap "params" "controls" 0 -}} -{{- $paramScratch.SetInMap "params" "rel" 0 -}} -{{- $paramScratch.SetInMap "params" "hl" .Site.Language.Lang -}} -{{- $params := $paramScratch.Get "params" -}} -{{- $paramsCollected := "" -}} -{{- range $key, $value := $params -}} - {{- $paramsCollected = fmt.Printf "%s&%s=%s" $paramsCollected $key $value -}} -{{- end -}} + {{- $pc := site.Config.Privacy.YouTube -}} {{- if not $pc.Disable -}} - {{- $id := .Get "id" | compare.Default (.Get 0) -}} - {{- $class := .Get "class" | compare.Default (.Get 1) -}} - {{- $title := .Get "title" | compare.Default (.Get 2) -}} - {{- $params := .Get "params" | compare.Default $paramsCollected -}} - {{- $thumbnailUrl := partials.IncludeCached "getYoutubePreviewUrl.html" . -}} - {{- $remoteThumbnailUrl := fmt.Printf $thumbnailUrl $id -}} -
- - - -
+ {{- $id := .Get "id" | compare.Default (.Get 0) -}} + {{- $class := .Get "class" | compare.Default (.Get 1) -}} + {{- $title := .Get "title" | compare.Default (.Get 2) -}} + {{- $params := .Get "params" | compare.Default (partialCached "func/prepareApiParams.html" .) -}} + {{- $thumbnailUrl := partials.IncludeCached "func/getYTPreviewUrl.html" $id $id -}} +
+ + + +
{{- end -}}