From 64869086015a832b9f47245cca53fe9a8aca6a62 Mon Sep 17 00:00:00 2001
From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com>
Date: Sun, 4 May 2025 00:10:21 +0800
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20gallery=20shortcode=20suppo?=
=?UTF-8?q?rts=20all=20kinds=20of=20input?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Also trim white spaces
---
layouts/shortcodes/gallery.html | 67 +++++++++++++++++++--------------
1 file changed, 38 insertions(+), 29 deletions(-)
diff --git a/layouts/shortcodes/gallery.html b/layouts/shortcodes/gallery.html
index 31d9bc9b..68a65488 100644
--- a/layouts/shortcodes/gallery.html
+++ b/layouts/shortcodes/gallery.html
@@ -1,31 +1,40 @@
-{{ $id := delimit (slice "gallery" (partial "functions/uid.html" .)) "-" }}
+{{ $id := delimit (slice "gallery" (partial "functions/uid.html" .)) "-" -}}
-
- {{ $page := .Page }}
-
- {{/* find all img tags */}}
- {{ $imgTagRegex := `
![]()
]*>` }}
- {{ $imgTags := findRE $imgTagRegex .Inner }}
- {{ $newContent := .Inner }}
-
- {{ range $imgTags }}
- {{ $imgTag := . }}
- {{/* extract src attribute */}}
- {{ $srcRegex := `src=['"]([^'"]+)['"]` }}
- {{ $srcMatches := findRESubmatch $srcRegex $imgTag }}
-
- {{ if $srcMatches }}
- {{ $srcFull := index (index $srcMatches 0) 0 }}
- {{ $src := index (index $srcMatches 0) 1 }}
-
- {{ $resource := $page.Resources.GetMatch $src }}
- {{ if $resource }}
- {{ $newSrc := printf `src="%s"` $resource.RelPermalink }}
- {{ $newImg := replace $imgTag $srcFull $newSrc }}
- {{ $newContent = replace $newContent $imgTag $newImg }}
- {{ end }}
- {{ end }}
- {{ end }}
-
- {{ $newContent | safeHTML }}
+
+ {{ $page := .Page -}}
+
+
+ {{ $imgTagRegex := `
![]()
]*>` -}}
+ {{ $imgTags := findRE $imgTagRegex .Inner -}}
+ {{ $newContent := .Inner -}}
+
+ {{ range $imgTags -}}
+ {{ $imgTag := . -}}
+
+ {{ $srcRegex := `src=['"]([^'"]+)['"]` -}}
+ {{ $srcMatches := findRESubmatch $srcRegex $imgTag -}}
+
+ {{ if $srcMatches -}}
+ {{ $srcFull := index (index $srcMatches 0) 0 -}}
+ {{ $src := index (index $srcMatches 0) 1 -}}
+
+ {{ $finalSrc := $src -}}
+ {{ $isExternalURL := or (hasPrefix $src "http://") (hasPrefix $src "https://") -}}
+
+ {{ if $isExternalURL -}}
+ {{ with resources.GetRemote $src -}}{{ $finalSrc = .RelPermalink -}}{{ end -}}
+ {{ else -}}
+ {{ with $page.Resources.GetMatch $src -}}{{ $finalSrc = .RelPermalink -}}
+ {{ else -}}
+ {{ with resources.GetMatch $src -}}{{ $finalSrc = .RelPermalink }}{{ end -}}
+ {{ end -}}
+ {{ end -}}
+
+ {{ $newSrc := printf `src="%s"` $finalSrc -}}
+ {{ $newImg := replace $imgTag $srcFull $newSrc -}}
+ {{ $newContent = replace $newContent $imgTag $newImg -}}
+ {{ end -}}
+ {{ end -}}
+
+ {{ $newContent | safeHTML -}}