add sizes to SVG

pull/666/head
stereobooster 2023-10-04 02:50:08 +02:00
parent 9b64aa8c30
commit 35e36b7a89
1 changed files with 22 additions and 2 deletions

View File

@ -40,13 +40,33 @@
type="image/webp"
/>
{{ end }}
{{ $width := ""}}
{{ $height := ""}}
{{ if eq .MediaType.SubType "svg" }}
{{ $svgContent := $resource.Content }}
{{ range (findRESubmatch `width=["']?(\d*)` $svgContent 1) }}
{{ $width = index . 1 }}
{{ end }}
{{ range (findRESubmatch `height=["']?(\d*)` $svgContent 1) }}
{{ $height = index . 1 }}
{{ end }}
{{ if (eq "" $width $height) }}
{{ range (findRESubmatch `viewBox=["']?(\d*) (\d*) (\d*) (\d*)` $svgContent 1) }}
{{ $width = index . 3 }}
{{ $height = index . 4 }}
{{ end }}
{{ end }}
{{ else }}
{{ $width = .Width }}
{{ $height = .Height }}
{{ end}}
<img
class="{{ $class }}"
width="{{ $width }}"
height="{{ $height }}"
{{ if eq .MediaType.SubType "svg" }}
src="{{ .RelPermalink }}"
{{ else }}
width="{{ .Width }}"
height="{{ .Height }}"
decoding="async"
{{ if lt .Width 660 }}
src="{{ .RelPermalink }}"