From 43857a5aa92f75e6906c23d72758e6e8085fe35a Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Tue, 24 Jun 2025 03:44:13 +0800 Subject: [PATCH] refactor(gallery): improve readibility - remove unnecessary intermediate variable - move the go process out of the div since they are not related - use expressive variable names - reorder declarations --- layouts/shortcodes/gallery.html | 64 ++++++++++++++------------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/layouts/shortcodes/gallery.html b/layouts/shortcodes/gallery.html index e2014dce..c1ebb264 100644 --- a/layouts/shortcodes/gallery.html +++ b/layouts/shortcodes/gallery.html @@ -1,43 +1,31 @@ {{ $id := delimit (slice "gallery" (partial "functions/uid.html" .)) "-" -}} +{{ $content := .Inner -}} + +{{/* find all img tags */}} +{{ range findRE `]*>` $content -}} + {{ $imgTag := . -}} + {{/* extract src attribute */}} + {{ with findRESubmatch `src=['"]([^'"]+)['"]` $imgTag -}} + {{ $srcAttr := index (index . 0) 0 -}} + {{ $srcValue := index (index . 0) 1 -}} + {{ $srcValueFinal := $srcValue -}} + + {{ if or (hasPrefix $srcValue "http://") (hasPrefix $srcValue "https://") -}} + {{ with resources.GetRemote $srcValue -}}{{ $srcValueFinal = .RelPermalink -}}{{ end -}} + {{ else -}} + {{ with $.Page.Resources.GetMatch $srcValue -}} + {{ $srcValueFinal = .RelPermalink -}} + {{ else -}} + {{ with resources.GetMatch $srcValue -}}{{ $srcValueFinal = .RelPermalink -}}{{ end -}} + {{ end -}} + {{ end -}} + + {{ $newTag := replace $imgTag $srcAttr (printf `src="%s"` $srcValueFinal) -}} + {{ $content = replace $content $imgTag $newTag -}} + {{ end -}} +{{ end -}}