我正在尝试延迟加载具有 {{ page.path }} 变量的 jekyll 图像。不幸的是,当我使用路径并使用硬链接图像时,可用的 jekyll 插件会失效。我在布局中使用变量来跨多个页面显示图像,所以我绝对不想开始使用硬编码的绝对路径。
插件可用于处理延迟加载类的添加、查找 w/h 并使用相关的类和尺寸呈现图像。下面是来自https://gist.github.com/ttscoff/9035690的图像标签插件 我需要修改什么以允许 jekyll 变量作为路径?或者有没有更好的方法来实现我没有想到的延迟加载?作为参考,我的 img 路径通常是:

图片标签插件
module Jekyll
class ImageTag < Liquid::Tag
@img = {}
def initialize(tag_name, markup, tokens)
# <img class="lazy" src="img/grey.gif" data-original="img/example.jpg" width="640" height="480">
if markup =~ /(?:(\S+) )?((?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(\d+))?(?:\s+(\d+))?\s+(.*)?/i
unless $2.nil?
imgclass = $1 || nil
image = $2
width = $3 || nil
height = $4 || nil
title = $5 || nil
@img = {}
@img["class"] = imgclass ? "lazy #{imgclass}" : "lazy"
begin
if image =~ /^(http:\/\/brettterpstra.com|\/)/
image.sub!(/^(http:\/\/brettterpstra.com)?/,"")
@img["data-original"] = image
filename = File.expand_path(File.join(%x{git rev-parse --show-toplevel}.strip,"source"+image))
@img["width"] = width || filename ? %x{sips -g pixelWidth "#{filename}"|awk '{print $2}'}.strip : nil
@img["height"] = height || filename ? %x{sips -g pixelHeight "#{filename}"|awk '{print $2}'}.strip : nil
else
@img["data-original"] = image
@img["width"] = width if width
@img["height"] = height if height
end
rescue
@img["data-original"] = image
@img["width"] = width if width
@img["height"] = height if height
end
@img["src"] = "/images/grey.gif"
if title && title !~ /^[\s"]*$/
if /(?:"|')(?<xtitle>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/ =~ title
@img['title'] = xtitle
@img['alt'] = alt
else
@img['alt'] = title.gsub(/(^["\s]*|["\s]*$)/, '')
end
end
end
end
super
end
def render(context)
unless @img.empty?
if context.registers[:site].config["production"]
@img["src"] = context.registers[:site].config["cdn_url"] + @img["src"]
if @img["data-original"] =~ /^(http:\/\/brettterpstra.com|\/)/
@img["data-original"] = context.registers[:site].config["cdn_url"] + @img["data-original"]
end
end
%Q{<img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>}
else
"Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \"title text\" [\"alt text\"]] %}"
end
end
end
end
Liquid::Template.register_tag('img', Jekyll::ImageTag)