gitnewlinejekylllf

Jekyll LF/CRLF issue with git


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 ?


Solution

  • 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.