Bläddra i källkod

shortcodes: Update figure and video to support files within page dir

To reference file in page folder, simply use
`{{< figure src="a.png" >}}`

To reference file from `static/img/` media library, set `library=1`
in shortcode. E.g. `{{< figure src="a.png" library=1 >}}`

See #708
George Cushen 6 år sedan
förälder
incheckning
9bcecc7b25

+ 1 - 1
layouts/partials/widgets/projects.html

@@ -77,7 +77,7 @@
       {{   $.Scratch.Set "project_url" $project.Params.external_link }}
       {{   $.Scratch.Set "target" "target=\"_blank\"" }}
       {{ end }}
-      <div class="col-12 col-md-6 col-lg-4 col-xl-6 project-item isotope-item {{ delimit (apply .Params.tags "urlize" ".") " " }}">
+      <div class="col-12 col-md-6 col-lg-4 project-item isotope-item {{ delimit (apply .Params.tags "urlize" ".") " " }}">
         <div class="card">
           {{ with $project.Params.image_preview }}
           <a href="{{ $.Scratch.Get "project_url" }}" title="" class="card-image hover-overlay"

+ 8 - 3
layouts/shortcodes/figure.html

@@ -1,11 +1,16 @@
+{{/* Enable image to be loaded from local page dir or media library at `static/img/`. */}}
+{{ $image_src := .Get "src" }}
+{{ if .Get "library" }}
+  {{ $image_src = printf "img/%s" $image_src | relURL }}
+{{ end }}
 <figure{{ with .Get "class" }} class="{{.}}"{{ end }}>
 {{ if .Get "link"}}<a href="{{ .Get "link" }}"{{ with .Get "target" }} target="{{ . }}"{{ end }}{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>{{ end }}
-<img src="{{ .Get "src" | relURL }}" {{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt"}}{{.}}{{else}}{{ .Get "caption" }}{{ end }}" {{ end }}{{ with .Get "width" }}width="{{.}}" {{ end }}{{ with .Get "height" }}height="{{.}}" {{ end }}/>
+<img src="{{ $image_src }}" {{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt"}}{{.}}{{else}}{{ .Get "caption" }}{{ end }}" {{ end }}{{ with .Get "width" }}width="{{.}}" {{ end }}{{ with .Get "height" }}height="{{.}}" {{ end }}/>
 {{ if .Get "link"}}</a>{{ end }}
 {{ if or (or (.Get "title") (.Get "caption")) (.Get "attr")}}
 {{ $figure := split (i18n "figure" | default "Figure %d:") "%d" }}
-<figcaption data-pre="{{ index $figure 0 }}" data-post="{{ index $figure 1 }}" {{ if eq (.Get "numbered") "true" }}class="numbered"{{ end }}>{{ if isset .Params "title" }}
-  <h4>{{ .Get "title" | markdownify | emojify }}</h4>{{ end }}
+<figcaption data-pre="{{ index $figure 0 }}" data-post="{{ index $figure 1 }}" {{ if eq (.Get "numbered") "true" }}class="numbered"{{ end }}>
+  {{ if isset .Params "title" }}<h4>{{ .Get "title" | markdownify | emojify }}</h4>{{ end }}
   {{ if or (.Get "caption") (.Get "attr")}}<p>
     {{ .Get "caption" | markdownify | emojify }}
     {{ with .Get "attrlink"}}<a href="{{.}}"> {{ end }}

+ 23 - 8
layouts/shortcodes/video.html

@@ -1,12 +1,27 @@
-{{ $directory := "img/" }}
-{{ $video := printf "%s%s" $directory (.Get "src") }}
+{{/* Enable video to be loaded from local page dir or media library at `static/img/`. */}}
+{{ $path := "" }}
+{{ $root_dir := "" }}
+{{ if .Get "library" }}
+  {{ $path = "img/" }}
+  {{ $root_dir = "static/" }}
+{{ end }}
+
+{{ $video := printf "%s%s" $path (.Get "src") }}
 {{ $video_type := substr (.Get "src") -1 3 }}
-{{ $poster := printf "%s%s%s" $directory (substr (.Get "src") 0 -4) ".jpg" }}
-{{ if fileExists (printf "%s%s" "static/" $poster) }}
-  {{ .Scratch.Set "poster" ($poster | relURL) }}
+{{ if .Get "library" }}
+  {{ $video = $video | relURL }}
+{{ end }}
+
+{{ $poster := printf "%s%s%s" $path (substr (.Get "src") 0 -4) ".jpg" }}
+{{ $has_poster := fileExists (printf "%s%s" $root_dir $poster) }}
+{{ if and $has_poster (.Get "library") }}
+  {{ $poster = $poster | relURL }}
+{{ else if $has_poster }}
+  {{ $poster = $poster }}
 {{ else }}
-  {{ .Scratch.Set "poster" "" }}
+  {{ $poster = "" }}
 {{ end }}
-<video {{if (.Get "controls")}}controls{{else}}autoplay loop{{end}} {{with .Scratch.Get "poster"}}poster="{{.}}"{{end}}>
-  <source src="{{ $video | relURL }}" type="video/{{$video_type}}">
+
+<video {{if (.Get "controls")}}controls{{else}}autoplay loop{{end}} {{with $poster}}poster="{{.}}"{{end}}>
+  <source src="{{ $video }}" type="video/{{$video_type}}">
 </video>