Created
March 17, 2016 20:39
-
-
Save iloveitaly/901f4aa64bdf4780b992 to your computer and use it in GitHub Desktop.
GitHub HTML pipeline for adding asset fingerprints to image links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module HTML | |
class Pipeline | |
class ImageAssetPathFilter < HTML::Pipeline::Filter | |
def call | |
doc.search("img").each do |img| | |
next if img['src'].nil? | |
src = img['src'].strip | |
if src.start_with?('/') | |
# stripping `/assets/` from the URL enables asset fingerprinting in production | |
img["src"] = ActionController::Base.helpers.asset_path(src.gsub(/^\/assets\//, '')) | |
end | |
end | |
doc | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment