{{- $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)
]+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 -}}
{{- end -}}
{{- end -}}