Skip to content

Commit

Permalink
local video shortcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
XanGrab committed Aug 7, 2024
1 parent ba57a18 commit ba59b26
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions layouts/shortcodes/video.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!--
Embed videos from Hugo Page Resources.
Setting src="my-video" with the Page Bundle containing...
- my-video.png
- my-video.mp4
- my-video.webm
...will render the following:
<video poster="my-video.png">
<source src="my-video.mp4" type="video/mp4" />
<source src="my-video.webm" type="video/webm" />
</video>
-->

{{ $src := .Get "src" }}

{{ if strings.HasPrefix $src "./" }}
<!-- Strip "./" prefix from relative path -->
{{ $src = substr $src 2 }}
{{ end }}

{{ $poster := ((.Page.Resources.ByType "image").GetMatch (printf "%s*" $src)) }}
{{ $videos := (.Page.Resources.ByType "video").Match (printf "%s*" $src) }}

<!--prettier-ignore-->
{{ if .Get "caption" }}
<figure>
{{ end }}

{{ if $videos }}
<video
{{ if eq (.Get "autoplay") "true" }}autoplay{{ end }}
{{ if eq (.Get "controls" | default "true") "true" }}controls{{ end }}
{{ with .Get "height" }}height="{{ . }}"{{ end }}
{{ if eq (.Get "loop") "true" }}loop{{ end }}
{{ if eq (.Get "muted") "true" }}muted{{ end }}
{{ with $poster }}poster="{{ .RelPermalink }}"{{ end }}
{{ with .Get "preload" }}preload="{{ . }}"{{ end }}
{{ with .Get "width" }}width="{{ . }}"{{ end }}
{{ if eq (.Get "playsinline" | default "true") "true" }}playsinline{{ end }}
>
{{- range $videos -}}
<source src="{{ .RelPermalink }}" type="{{ .MediaType }}" />
{{- end -}}
</video>
{{ end }}


<!--prettier-ignore-->
{{ if .Get "caption" }}
<figcaption>{{ .Page.RenderString (.Get "caption") }}</figcaption>
</figure>
{{ end }}

0 comments on commit ba59b26

Please sign in to comment.