@@ -167,6 +167,7 @@ def relative_url(input)
167167end
168168
169169Jekyll ::Hooks . register :site , :post_read do |site |
170+ cache = Jekyll ::Cache . new ( "KramdownEnhancer" )
170171 KramdownEnhancer . baseurl = site . config [ "baseurl" ]
171172 webp_list = [ ]
172173 site . each_site_file do |file |
@@ -178,9 +179,17 @@ def relative_url(input)
178179 if File . exist? ( source )
179180 KramdownEnhancer . webp [ file . url ] = url
180181 elsif %w[ .png .jpg .jpeg .tif .tiff ] . include? ( file . extname . downcase )
182+ source_base64 = Base64 . encode64 ( File . read ( file . path , mode : "rb" ) )
183+ hash = Digest ::SHA256 . hexdigest ( source_base64 )
184+ if cache . key? ( "webp_#{ hash } " )
185+ Jekyll . logger . info "Kramdown Enhancer:" , "[webp] Hit Cache #{ url } "
186+ else
187+ destination_base64 = Script . call ( "webp" , source : source_base64 )
188+ cache [ "webp_#{ hash } " ] = Base64 . decode64 ( destination_base64 )
189+ Jekyll . logger . info "Kramdown Enhancer:" , "[webp] Generated #{ url } "
190+ end
181191 FileUtils . mkdir_p ( File . dirname ( destination ) )
182- result = Node . call ( "webp" , source : file . path , destination : destination )
183- Jekyll . logger . info "Kramdown Enhancer:" , "[webp] Generated #{ result } "
192+ File . write ( destination , cache [ "webp_#{ hash } " ] , mode : "wb" )
184193 webp_list . push ( KramdownEnhancer ::WebpFile . new ( site , site . dest , File . dirname ( url ) , File . basename ( url ) ) )
185194 KramdownEnhancer . webp [ file . url ] = url
186195 end
0 commit comments