From 6f33b9d5cfd4c285cc245baafc80b07ab2d3a005 Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Fri, 2 May 2025 17:43:56 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20incorrect=20path=20for=20?= =?UTF-8?q?gallery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix this issue with resource.RelPermalink --- layouts/shortcodes/gallery.html | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/layouts/shortcodes/gallery.html b/layouts/shortcodes/gallery.html index c01b48f8..31d9bc9b 100644 --- a/layouts/shortcodes/gallery.html +++ b/layouts/shortcodes/gallery.html @@ -1,5 +1,31 @@ {{ $id := delimit (slice "gallery" (partial "functions/uid.html" .)) "-" }} \ No newline at end of file + {{ $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 }} +