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 -}}