Jekyll LF/CRLF issue with git

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 05:01:52

问题


I have a Jekyll folder in which only the production part (_site) is tracked with git. When I run the command to serve the local site with jekyll serve -w, the files will be changed to LF or CRLF depending on the machine I'm working on: CRLF for Windows, LF for Mac. This is really annoying because all my files inside _site will be commited everytime I switch my OS.

I've tried to fix this in the git config file with autocrlf = false, but since the files are generated at a higher level by Jekyll it seems to have no impact at all.

Is there a way to tell Jekyll to generate all the files in a specific format, either LF or CRLF ?


回答1:


Three obvious solutions :

First solution : Jekyll plugin

To globally replace CR or CRLF by LF, the easiest way is to do it when files are written to destination.

This plugin overload the Jekyll:Convertible.write method :

module Jekyll
  module Convertible
    def write(dest)
      ### begin overloading
      # Replaces CR and CRLF by LF
      self.output = self.output.gsub(/\r\n?/, "\n")
      ### end overloading

      path = destination(dest)
      FileUtils.mkdir_p(File.dirname(path))
      File.open(path, 'wb') do |f|
        f.write(output)
      end
    end
  end
end

Save this in _plugins/crlf.rb and it will automatically run at jekyll build time.

Second solution : Configure your code editor

Configure your code editor to use LF. If you can't, change editor.



来源:https://stackoverflow.com/questions/25681960/jekyll-lf-crlf-issue-with-git

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