Minified the shortcodes and added handling for SVGs.

This commit is contained in:
wermos 2024-06-03 22:34:24 +05:30
parent de1cdd431c
commit bb435aded7
No known key found for this signature in database
GPG key ID: 8CBC132DB3E00784
2 changed files with 62 additions and 62 deletions

View file

@ -1,29 +1,29 @@
{{ $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }} {{- $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }}
{{ $url := urls.Parse .Destination }} {{- $url := urls.Parse .Destination }}
{{ $altText := .Text }} {{- $altText := .Text }}
{{ $caption := .Title }} {{- $caption := .Title }}
{{ if findRE "^https?" $url.Scheme }} {{- if findRE "^https?" $url.Scheme }}
<figure> <figure>
<img class="my-0 rounded-md" loading="lazy" src="{{ $url.String }}" alt="{{ $altText }}" /> <img class="my-0 rounded-md" loading="lazy" src="{{ $url.String }}" alt="{{ $altText }}" />
{{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }} {{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
</figure> </figure>
{{ else }} {{- else }}
{{ $resource := "" }} {{- $resource := "" }}
{{ if $.Page.Resources.GetMatch ($url.String) }} {{- if $.Page.Resources.GetMatch ($url.String) }}
{{ $resource = $.Page.Resources.GetMatch ($url.String) }} {{- $resource = $.Page.Resources.GetMatch ($url.String) }}
{{ else if resources.GetMatch ($url.String) }} {{- else if resources.GetMatch ($url.String) }}
{{ $resource = resources.Get ($url.String) }} {{- $resource = resources.Get ($url.String) }}
{{ end }} {{- end }}
{{ with $resource }} {{- with $resource }}
<figure> <figure>
{{ if or $disableImageOptimization (eq .MediaType.SubType "svg") }} {{- if or $disableImageOptimization (eq .MediaType.SubType "svg")}}
<img <img
class="my-0 rounded-md" class="my-0 rounded-md"
loading="lazy" loading="lazy"
src="{{ .RelPermalink }}" src="{{ .RelPermalink }}"
alt="{{ $altText }}" alt="{{ $altText }}"
/> />
{{ else }} {{- else }}
<img <img
class="my-0 rounded-md" class="my-0 rounded-md"
loading="lazy" loading="lazy"
@ -35,13 +35,13 @@
src="{{ (.Resize "660x").RelPermalink }}" src="{{ (.Resize "660x").RelPermalink }}"
alt="{{ $altText }}" alt="{{ $altText }}"
/> />
{{ end }} {{- end }}
{{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }} {{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
</figure> </figure>
{{ else }} {{- else }}
<figure> <figure>
<img class="my-0 rounded-md" loading="lazy" src="{{ $url.String }}" alt="{{ $altText }}" /> <img class="my-0 rounded-md" loading="lazy" src="{{ $url.String }}" alt="{{ $altText }}" />
{{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }} {{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
</figure> </figure>
{{ end }} {{- end }}
{{ end }} {{- end }}

View file

@ -2,49 +2,49 @@
{{ if .Get "default" }} {{ if .Get "default" }}
{{ template "_internal/shortcodes/figure.html" . }} {{ template "_internal/shortcodes/figure.html" . }}
{{ else }} {{ else }}
{{ $url := urls.Parse (.Get "src") }} {{- $url := urls.Parse (.Get "src") }}
{{ $altText := .Get "alt" }} {{- $altText := .Get "alt" }}
{{ $caption := .Get "caption" }} {{- $caption := .Get "caption" }}
{{ $href := .Get "href" }} {{- $href := .Get "href" }}
{{ $class := .Get "class" }} {{- $class := .Get "class" }}
{{ $target := .Get "target" | default "_blank" }} {{- $target := .Get "target" | default "_blank" }}
{{ $nozoom := .Get "nozoom" | default false }} {{- $nozoom := .Get "nozoom" | default false -}}
<figure> <figure>
{{ with $href }}<a href="{{ . }}" {{ with $target }}target="{{ . }}"{{ end }}>{{ end }} {{- with $href }}<a href="{{ . }}" {{ with $target }}target="{{ . }}"{{ end }}>{{ end -}}
{{ if findRE "^https?" $url.Scheme }} {{- if findRE "^https?" $url.Scheme }}
<img class="my-0 rounded-md {{ with $nozoom }} nozoom {{ end }}{{ with $class }} {{ . }} {{ end }}" src="{{ $url.String }}" alt="{{ $altText }}" /> <img class="my-0 rounded-md{{ with $nozoom }} nozoom{{ end }}{{ with $class }} {{ . }}{{ end }}" src="{{ $url.String }}" alt="{{ $altText }}" />
{{ else }} {{- else }}
{{ $resource := "" }} {{- $resource := "" }}
{{ if $.Page.Resources.GetMatch ($url.String) }} {{- if $.Page.Resources.GetMatch ($url.String) }}
{{ $resource = $.Page.Resources.GetMatch ($url.String) }} {{- $resource = $.Page.Resources.GetMatch ($url.String) }}
{{ else if resources.GetMatch ($url.String) }} {{- else if resources.GetMatch ($url.String) }}
{{ $resource = resources.Get ($url.String) }} {{- $resource = resources.Get ($url.String) }}
{{ end }} {{- end }}
{{ with $resource }} {{- with $resource }}
{{ if $disableImageOptimization }} {{- if or $disableImageOptimization (eq .MediaType.SubType "svg")}}
<img <img
class="my-0 rounded-md {{ with $nozoom }} nozoom {{ end }}{{ with $class }} {{ . }} {{ end }}" class="my-0 rounded-md{{ with $nozoom }} nozoom{{ end }}{{ with $class }} {{ . }}{{ end }}"
src="{{ .RelPermalink }}" src="{{ .RelPermalink }}"
alt="{{ $altText }}" alt="{{ $altText }}"
/> />
{{ else }} {{- else }}
<img <img
class="my-0 rounded-md {{ with $nozoom }} nozoom {{ end }}{{ with $class }} {{ . }} {{ end }}" class="my-0 rounded-md{{ with $nozoom }} nozoom{{ end }}{{ with $class }} {{ . }}{{ end }}"
srcset=" srcset="
{{ (.Resize "330x").RelPermalink }} 330w, {{ (.Resize "330x").RelPermalink }} 330w,
{{ (.Resize "660x").RelPermalink }} 660w, {{ (.Resize "660x").RelPermalink }} 660w,
{{ (.Resize "1024x").RelPermalink }} 1024w, {{ (.Resize "1024x").RelPermalink }} 1024w,
{{ (.Resize "1320x").RelPermalink }} 2x" {{ (.Resize "1320x").RelPermalink }} 2x"
src="{{ (.Resize "660x").RelPermalink }}" src="{{ (.Resize "660x").RelPermalink }}"
alt="{{ $altText }}" alt="{{ $altText }}"
/> />
{{ end }} {{- end }}
{{ else }} {{- else }}
<img class="my-0 rounded-md {{ with $nozoom }} nozoom {{ end }}{{ with $class }} {{ . }} {{ end }}" src="{{ $url.String }}" alt="{{ $altText }}" /> <img class="my-0 rounded-md{{ with $nozoom }} nozoom{{ end }}{{ with $class }} {{ . }}{{ end }}" src="{{ $url.String }}" alt="{{ $altText }}" />
{{ end }} {{- end }}
{{ end }} {{- end }}
{{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }} {{- with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
{{ if $href }}</a>{{ end }} {{- if $href }}</a>{{ end }}
</figure> </figure>
{{ end }} {{- end -}}