1
Files
2025/themes/2026/layouts/_partials/asides/complementary-images.html

116 lines
4.3 KiB
HTML

{{- $page := . -}}
{{- $bundleImages := slice -}}
{{- range $page.Resources.ByType "image" -}}
{{- if strings.HasPrefix .Name "images/" -}}
{{- $bundleImages = $bundleImages | append . -}}
{{- end -}}
{{- end -}}
{{- if gt (len $bundleImages) 0 -}}
{{- $coverPath := "" -}}
{{- with $page.Params.cover -}}
{{- $coverPath = strings.TrimSpace (printf "%v" .) -}}
{{- end -}}
{{- $coverImagePath := "" -}}
{{- if ne $coverPath "" -}}
{{- $coverPathLower := lower $coverPath -}}
{{- if or (strings.HasSuffix $coverPathLower ".yaml") (strings.HasSuffix $coverPathLower ".yml") -}}
{{- $coverDataFile := $page.Resources.GetMatch $coverPath -}}
{{- if $coverDataFile -}}
{{- $coverData := $coverDataFile.Content | transform.Unmarshal -}}
{{- with index $coverData "file" -}}
{{- $coverImagePath = strings.TrimSpace (printf "%v" .) -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{- $coverImagePath = $coverPath -}}
{{- end -}}
{{- end -}}
{{- $coverImageName := "" -}}
{{- if ne $coverImagePath "" -}}
{{- $coverImageName = lower (path.Base $coverImagePath) -}}
{{- end -}}
{{- $contentImageNames := slice -}}
{{- $contentImageStems := slice -}}
{{- $imageTags := findRE `(?is)<img[^>]+src=["'][^"']+["'][^>]*>` .Content -}}
{{- range $imageTags -}}
{{- $srcMatch := findRESubmatch `(?is)src=["']([^"']+)["']` . -}}
{{- if gt (len $srcMatch) 0 -}}
{{- $src := lower (index (index $srcMatch 0) 1) -}}
{{- if or (in $src "/images/") (strings.HasPrefix $src "images/") (strings.HasPrefix $src "./images/") -}}
{{- $srcName := path.Base $src -}}
{{- $srcName = replaceRE "\\?.*$" "" $srcName -}}
{{- $srcName = replaceRE "#.*$" "" $srcName -}}
{{- $srcName = replaceRE "_hu_[^.]*" "" $srcName -}}
{{- $srcStem := $srcName | replaceRE "\\.[^.]+$" "" -}}
{{- $contentImageNames = $contentImageNames | append $srcName -}}
{{- $contentImageStems = $contentImageStems | append $srcStem -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $unusedImages := slice -}}
{{- range $bundleImages -}}
{{- $imageName := path.Base .Name -}}
{{- $imageNameLower := lower $imageName -}}
{{- $isCoverImage := false -}}
{{- if and (ne $coverImageName "") (eq $imageNameLower $coverImageName) -}}
{{- $isCoverImage = true -}}
{{- end -}}
{{- if not $isCoverImage -}}
{{- $imageStem := $imageName | replaceRE "\\.[^.]+$" "" -}}
{{- $imageStemLower := lower $imageStem -}}
{{- $isUsedInContent := false -}}
{{- range $contentImageNames -}}
{{- if eq . $imageNameLower -}}
{{- $isUsedInContent = true -}}
{{- end -}}
{{- end -}}
{{- if not $isUsedInContent -}}
{{- range $contentImageStems -}}
{{- if eq . $imageStemLower -}}
{{- $isUsedInContent = true -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if not $isUsedInContent -}}
{{- $unusedImages = $unusedImages | append . -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if gt (len $unusedImages) 0 -}}
<aside class="article-complementary-images">
<section>
<h2>Images complémentaires</h2>
<ul>
{{- range $unusedImages -}}
{{- $imageName := path.Base .Name -}}
{{- $imageStem := $imageName | replaceRE "\\.[^.]+$" "" -}}
{{- $data := dict -}}
{{- $dataFile := $page.Resources.Get (printf "data/images/%s.yaml" $imageStem) -}}
{{- if not $dataFile -}}
{{- $dataFile = $page.Resources.Get (printf "data/%s.yaml" $imageStem) -}}
{{- end -}}
{{- if $dataFile -}}
{{- $data = $dataFile.Content | transform.Unmarshal -}}
{{- end -}}
{{- $title := default (printf "Image complémentaire : %s" $imageName) (index $data "title") -}}
{{- $thumbnail := . -}}
{{- if and (ne .MediaType.SubType "svg") (gt .Width 360) -}}
{{- $thumbnail = .Resize "360x" -}}
{{- end -}}
<li>
<a href="{{ .RelPermalink }}" title="{{ $title }}">
<img src="{{ $thumbnail.RelPermalink }}" alt="{{ $title }}" loading="lazy" decoding="async">
</a>
</li>
{{- end -}}
</ul>
</section>
</aside>
{{- end -}}
{{- end -}}