From 472176a88c5568c81403419a48355a768af02fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Sun, 19 Feb 2023 22:00:09 +0100 Subject: [PATCH] attachments: don't break build if directory wasn't found #480 --- layouts/partials/shortcodes/attachments.html | 34 +++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/layouts/partials/shortcodes/attachments.html b/layouts/partials/shortcodes/attachments.html index 9b7398984c..5ef67be687 100644 --- a/layouts/partials/shortcodes/attachments.html +++ b/layouts/partials/shortcodes/attachments.html @@ -39,23 +39,25 @@ {{- $filesName = "files" }} {{- end }} {{- $dir := printf "%s%s" $fileDir $filesName }} - {{- range sort (readDir $dir) "Name" $sort }} - {{- if findRE $pattern .Name}} - {{- $size := .Size }} - {{- $unit := "Byte-symbol" }} - {{- if ge $size 1024 }} - {{- $size = div $size 1024 }} - {{- $unit = "Kilobyte-symbol" }} + {{- if fileExists $dir }} + {{- range sort (readDir $dir) "Name" $sort }} + {{- if findRE $pattern .Name}} + {{- $size := .Size }} + {{- $unit := "Byte-symbol" }} + {{- if ge $size 1024 }} + {{- $size = div $size 1024 }} + {{- $unit = "Kilobyte-symbol" }} + {{- end }} + {{- if ge $size 1024 }} + {{- $size = div $size 1024 }} + {{- $unit = "Megabyte-symbol" }} + {{- end }} + {{- $unitsymbol := $unit | T }} + {{- $link := (printf "%s%s/%s" $fileLink $filesName .Name) | relLangURL }} + {{- $c := "" }}{{/* Hugo stores the attachments always in the default language subdirectory */}} + {{- $link = replace $link $urlPrefix $defaultUrlPrefix 1 }} +
  • {{.Name}} ({{$size}} {{$unitsymbol}})
  • {{- end }} - {{- if ge $size 1024 }} - {{- $size = div $size 1024 }} - {{- $unit = "Megabyte-symbol" }} - {{- end }} - {{- $unitsymbol := $unit | T }} - {{- $link := (printf "%s%s/%s" $fileLink $filesName .Name) | relLangURL }} - {{- $c := "" }}{{/* Hugo stores the attachments always in the default language subdirectory */}} - {{- $link = replace $link $urlPrefix $defaultUrlPrefix 1 }} -
  • {{.Name}} ({{$size}} {{$unitsymbol}})
  • {{- end }} {{- end }}