Skip to content

Commit f4cd50b

Browse files
committed
add local video shortcord
1 parent 7d4cbef commit f4cd50b

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

layouts/shortcodes/video.html

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{{- /* hugo-video shortcode
2+
/*
3+
/* This file is part of hugo-video shortcode.
4+
/* A Hugo component shortcode to embed videos using the HTML video element.
5+
/*
6+
/* @copyright @2019 onwards Nicolas Martignoni (nicolas@martignoni.net)
7+
/* @source https://github.com/martignoni/hugo-video
8+
/* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9+
/*
10+
*/ -}}
11+
12+
{{- $video_src := .Get "src" -}}
13+
{{- $video_mp4 := "" -}}
14+
{{- $video_webm := "" -}}
15+
{{- $video_ogg := "" -}}
16+
{{- $video_dl := "" -}}
17+
{{- $width := "100%" -}}
18+
{{- $filenotfound := true -}}
19+
{{- $unsupportedfile := true -}}
20+
21+
{{- /* Find all files with filename (without suffix) matching "src" parameter. */ -}}
22+
{{- $video_files := (.Page.Resources.Match (printf "%s*" $video_src)) -}}
23+
24+
{{- /* Find first image file with filename (without suffix) matching "src" parameter. */ -}}
25+
{{- $poster := ((.Page.Resources.ByType "image").GetMatch (printf "%s*" $video_src)) -}}
26+
27+
{{- /* Find in page bundle all valid video files with matching name. */ -}}
28+
{{- with $video_files -}}
29+
{{- $filenotfound = false -}}
30+
{{- range . -}}
31+
{{- if or (in .MediaType.Suffixes "mp4") (in .MediaType.Suffixes "m4v") -}}
32+
{{- $unsupportedfile = false -}}
33+
{{- $video_mp4 = . -}}
34+
{{- end -}}
35+
{{- if (in .MediaType.Suffixes "webm") -}}
36+
{{- $unsupportedfile = false -}}
37+
{{- $video_webm = . -}}
38+
{{- end -}}
39+
{{- if (in .MediaType.Suffixes "ogv") -}}
40+
{{- $unsupportedfile = false -}}
41+
{{- $video_ogg = . -}}
42+
{{- end -}}
43+
{{- end -}}
44+
{{- end -}}
45+
46+
{{- if $filenotfound -}}
47+
{{- /* No file of given name was found, we stop here. */ -}}
48+
{{- errorf "No file with filename %q found." $video_src -}}
49+
{{- else if $unsupportedfile -}}
50+
{{- errorf "No valid video file with filename %q found." $video_src -}}
51+
{{- else -}}
52+
<video {{ if ne (.Get "controls") "false" }}controls {{ end }}preload="auto" width="{{ or (.Get "width") $width }}" {{ with .Get "height" }}height="{{.}}"{{ end }} {{ if eq (.Get "autoplay") "true" }}autoplay {{ end }}{{ if eq (.Get "loop") "true" }}loop {{ end }}{{ if eq (.Get "muted") "true" }}muted {{ end }}{{ with $poster }}poster="{{ .RelPermalink }}" {{ end }}playsinline class="html-video">
53+
{{- with $video_webm }}
54+
<source src="{{ .RelPermalink }}" type="video/webm">
55+
{{- $video_dl = . -}}
56+
{{- end }}
57+
{{- with $video_ogg }}
58+
<source src="{{ .RelPermalink }}" type="video/ogg">
59+
{{- $video_dl = . -}}
60+
{{- end }}
61+
{{- with $video_mp4 }}
62+
<source src="{{ .RelPermalink }}" type="video/mp4">
63+
{{- $video_dl = . -}}
64+
{{- end }}
65+
<span>{{ i18n "videoUnsupported" $video_dl | safeHTML}}</span>
66+
</video>
67+
{{- end -}}

0 commit comments

Comments
 (0)