From afb6fb637cd6ad7ffe99a523b8b2a47cfacd990e Mon Sep 17 00:00:00 2001 From: jesselau76 Date: Tue, 27 Nov 2018 13:21:27 +1300 Subject: [PATCH] lazy load --- {static => assets}/placeholder.svg | 0 layouts/post/single.html | 15 ++++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) rename {static => assets}/placeholder.svg (100%) diff --git a/static/placeholder.svg b/assets/placeholder.svg similarity index 100% rename from static/placeholder.svg rename to assets/placeholder.svg diff --git a/layouts/post/single.html b/layouts/post/single.html index d59445b..4e01443 100644 --- a/layouts/post/single.html +++ b/layouts/post/single.html @@ -47,13 +47,18 @@

{{ .Title }}

{{ if .Site.Params.lazyload }} - {{ $img := findRE "src=\"(.+?png|jpg|jpeg|gif|bmp)\"" .Content }} - {{ if gt (len $img) 1 }} - - {{ (.Content | replaceRE "src=\"(.+?png|jpg|jpeg|gif|bmp)\"" "src=\"/placeholder.svg\" data-src=\"$1\"") | safeHTML }} + + + {{ $regex := "src=\"(.+?)\"" }} + {{ $img := findRE $regex .Content }} + + {{ if gt (len $img) 0 }} + {{ $placeholder := resources.Get "placeholder.svg" }} + {{ $replacement := print "src=\"" $placeholder.RelPermalink "\" data-src=\"$1\"" }} + {{ .Content | replaceRE $regex $replacement | safeHTML }} {{ else }} {{ .Content }} - {{ end }} + {{ end }} {{ else }}