Skip to content

Commit

Permalink
fix: add media querys to markdown images
Browse files Browse the repository at this point in the history
Signed-off-by: lennartrommeiss <lennart.rommeiss@deutschebahn.com>
  • Loading branch information
lenderom committed Feb 3, 2025
1 parent a97d1d6 commit 2554c20
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions themes/fiptheme/layouts/_default/_markup/render-image.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{{ $myTitle := .Title | default (i18n "image not found") }}

{{/* Prüfen, ob .Destination (also die Bildquelle) mit http:// oder https:// beginnt */}}
{{ if and .Destination (or (strings.HasPrefix .Destination "http://") (strings.HasPrefix .Destination "https://")) }}
<!-- Remote Bild: Direkt ausgeben ohne Bildverarbeitung -->
{{ $remoteImage := resources.GetRemote .Destination }}
<p class="md-image">
<img src="{{ $remoteImage.Permalink }}" alt="{{ .Title }}" title="{{ .Title }}" />
</p>
{{ else }}
{{/* Lokales Bild: Mit Bildverarbeitung */}}
{{ $originalImage := resources.Get "images/glider.png" }}
{{ with .Destination }}
{{ $myTitle = $.Title }}
<!-- Zunächst im Seitenkontext nach Ressourcen suchen -->
{{ with $.Page.Resources.Get . }}
{{ $originalImage = . }}
{{ else }}
<!-- Dann in den statischen Ressourcen suchen -->
{{ with resources.Get . }}
{{ $originalImage = . }}
{{ end }}
{{ end }}
{{ end }}

<p class="md-image">
<picture>
{{ $width := $originalImage.Width }}

{{ if gt $width 2200 }}
{{ with $originalImage.Resize "2200x" }}
<source media="(min-width:2200px)" srcset="{{ .RelPermalink | safeURL }}" />
{{ end }}
{{ end }}

{{ if gt $width 1500 }}
{{ with $originalImage.Resize "1500x" }}
<source media="(min-width:1500px)" srcset="{{ .RelPermalink | safeURL }}" />
{{ end }}
{{ end }}

{{ if gt $width 1200 }}
{{ with $originalImage.Resize "1200x" }}
<source media="(min-width:1200px)" srcset="{{ .RelPermalink | safeURL }}" />
{{ end }}
{{ end }}

{{ if gt $width 800 }}
{{ with $originalImage.Resize "800x" }}
<source media="(min-width:800px)" srcset="{{ .RelPermalink | safeURL }}" />
{{ end }}
{{ end }}

{{ $tinyImage := $originalImage }}
{{ if gt $width 500 }}
{{ $tinyImage = $originalImage.Resize "500x" }}
{{ end }}
<img src="{{ $tinyImage.RelPermalink | safeURL }}" alt="{{ $myTitle }}" title="{{ $myTitle }}" />
</picture>
</p>
{{ end }}

0 comments on commit 2554c20

Please sign in to comment.