Image Not Copied by Jekyll-Assets

↘锁芯ラ 提交于 2019-12-11 06:23:48

问题


I am using Jekyll-Assets to manage the asset pipeline for a jekyll project. I have references to a few different images on the index.html file:

<img src="{% asset_path slides/slide-1.jpg %}"

The source images are located in _assets/images/slides. My _config.yml file has this configured for sources:

sources:
- _assets/images

One of the five images in the _assets/slides directory is not getting copied to the destination _site/assets/slides. The other four are copied correctly. I have verified that the same img tag and liquid tag are being used for all five images (see above).

However, I have also tried:

sources:
- _assets/images
- _assets/images/slides

I have tried renaming the image as well as removing the subdirectory slides with no luck. I did save the image (using Photoshop) as a completely new file and it does get copied. This feels like some attribute of the file itself is causing this to be omitted.

Under what circumstances will an asset not get copied?


回答1:


I ran into this too. The problem is that jekyll-assets removes duplicate asset files based on (the MD5 hash of) the contents of those files. When you have duplicate images, only one will be copied to the destination folder. Saving the file again from Photoshop changed its contents (possibly just a timestamp).

Here's the fix I'm using, which seems to work well:

require "jekyll-assets"

# Monkey-patch jekyll-assets so it doesn't drop duplicate files.
module FixSitePatch
  def self.included(base)
    base.class_eval do
      alias_method :write, :__my_write
    end
  end
  def __my_write
    static_files.push(*asset_files)
    __orig_write
  end
end
Jekyll::Site.send :include, FixSitePatch

This replaces the monkey-patch that jekyll-assets made with a different monkey-patch that doesn't remove duplicates. Put this in _plugins/ext.rb or wherever you currently include jekyll-assets.

A possibly better fix would be for jekyll-assets to rewrite all the target asset paths to point to the one unique copy of the file, although that could break external links or cause problems with search engines that index content partially based on path components.

Alternatively, you could make some (different) edits to all duplicate files to change their MD5 hashes, e.g., by changing a pixel. (This doesn't work well in my situation, where I have additional scripts generating content such as thumbnails.)




回答2:


Make sure the following config information is in your _config.yml:

assets:
    sources:
        - _assets/images          
        - _assets/stylesheets          
        - _assets/images/slides



回答3:


What worked for me was advice as from https://github.com/jekyll/jekyll-assets/issues/261#issuecomment-221068294 by arron-jeffery, i.e. to rm -rf .asset-cache. Cleaning _site, and setting

   `assets:
       cache: false` 

might help as well.

Moreover, if you use jekyll-assets, you can use its {% img %} tag - there are some formatting options to it, too, and it seems a bit more reliable to me.



来源:https://stackoverflow.com/questions/22185926/image-not-copied-by-jekyll-assets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!