New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files inside a collection folders are put in _site/ root #6410

Open
hosnas opened this Issue Oct 3, 2017 · 9 comments

Comments

Projects
None yet
6 participants
@hosnas

hosnas commented Oct 3, 2017

  • I believe this to be a bug, not a question about using Jekyll.
  • I updated to the latest Jekyll (or) if on GitHub Pages to the latest github-pages
  • I ran jekyll doctor to check my configuration
  • I read the CONTRIBUTION file at https://jekyllrb.com/docs/contributing/
  • This is a feature request.

  • I am on (or have tested on) macOS 10+
  • I am on (or have tested on) Debian/Ubuntu GNU/Linux
  • I am on (or have tested on) Fedora GNU/Linux
  • I am on (or have tested on) Arch GNU/Linux
  • I am on (or have tested on) Other GNU/Linux
  • I am on (or have tested on) Windows 10+

  • I was trying to install.
  • There is a broken Plugin API.
  • I had an error on GitHub Pages, and I have reproduced it locally.
  • I had an error on GitHub Pages, and GitHub Support said it was a Jekyll Bug.
  • I had an error on GitHub Pages and I did not test it locally.
  • I was trying to build.
  • It was another bug.

My Reproduction Steps

I am normally putting my doc assets beside the doc itself and I used jekyll postfiles plugin to handle assets of my posts and I had no problem. Recently I started to use jekyll collections too and I wanted to do the same approach (putting assets beside collection docs). I tweaked that plugin a little bit and its working exactly as I want. However I have a problem. All of the assets inside collection directory also are copied in the _site/ root directory. This doesn't happen for the assets beside my posts. And I think it isn't the intended behavior. So I guess its a bug?

I would be very happy if its fixed.

The Output I Wanted

Preferred output is to not copy collection files to site root and let the plugin handle where they should be. This is the normal behaviour for files that are in _post directory as far as I know. And _post is some kind of collection.

@DirtyF

This comment has been minimized.

Show comment
Hide comment
@DirtyF
Member

DirtyF commented Oct 3, 2017

@parkr

This comment has been minimized.

Show comment
Hide comment
@parkr

parkr Oct 3, 2017

Member

Anywhere Jekyll::StaticFile is used in that plugin, ensure the collection parameter is passed. It should be a Jekyll::Collection object (accessed through site.collections).

Member

parkr commented Oct 3, 2017

Anywhere Jekyll::StaticFile is used in that plugin, ensure the collection parameter is passed. It should be a Jekyll::Collection object (accessed through site.collections).

@hosnas

This comment has been minimized.

Show comment
Hide comment
@hosnas

hosnas Oct 3, 2017

@parkr I have already done that:

require "jekyll"

module Jekyll

  class PostFile < StaticFile

    # Initialize a new PostFile.
    #
    # site - The Site.
    # base - The String path to the <source>.
    # dir - The String path of the source directory of the file (rel <source>).
    # name - The String filename of the file.
    def initialize(site, base, dir, name, dest)
      super(site, base, dir, name)
      @name = name
      @dest = dest
    end

    # Obtain destination path.
    #
    # dest - The String path to the destination dir.
    #
    # Returns destination file path.
    def destination(dest)
      File.join(@dest, @name)
    end
  end

  class PostFileGenerator < Generator

    # Copy the files from post's folder.
    #
    # post - A Post which may have associated content.
    def copy_doc_files(post)

      postpath = post.path
      postdir = File.dirname(postpath)
      destdir = File.dirname(post.destination(""))
      site = post.site
      sitesrcdir = site.source
      contents = Dir.glob(File.join(postdir, '**', '*')) do |filepath|
        if filepath != postpath
          filedir, filename = File.split(filepath[sitesrcdir.length..-1])
          reldir, dolaki = File.split(filepath[postdir.length..-1])
          unaccepted_formats = [".md", ".meta", ".html", ".json", "Rakefile"]
          if reldir.include? "/_"
            puts " dir #{reldir} excluded"
          else
          if File.file?(filepath)
            if unaccepted_formats.include? File.extname(filename)
              puts " File #{filename} excluded"
            else
          site.static_files <<
            PostFile.new(site, sitesrcdir, filedir, filename, destdir + reldir)
          end
          end
        end
        end
      end
      #puts "========================================================"

    end

    # Generate content by copying files associated with each post.
    def generate(site)
      site.collections.each do |col|
        col[1].docs.each do |post|
          copy_doc_files(post)
        end
      end
    end
  end
end

The problem now is not the plugin, as it is working perfectly. The problem is that jekyll itself puts all assets inside collection folders in the root folder of the site. So it means all collection assets are in two place. 1- beside the rendered html (as intended by the plugin), and 2- in the site root.

hosnas commented Oct 3, 2017

@parkr I have already done that:

require "jekyll"

module Jekyll

  class PostFile < StaticFile

    # Initialize a new PostFile.
    #
    # site - The Site.
    # base - The String path to the <source>.
    # dir - The String path of the source directory of the file (rel <source>).
    # name - The String filename of the file.
    def initialize(site, base, dir, name, dest)
      super(site, base, dir, name)
      @name = name
      @dest = dest
    end

    # Obtain destination path.
    #
    # dest - The String path to the destination dir.
    #
    # Returns destination file path.
    def destination(dest)
      File.join(@dest, @name)
    end
  end

  class PostFileGenerator < Generator

    # Copy the files from post's folder.
    #
    # post - A Post which may have associated content.
    def copy_doc_files(post)

      postpath = post.path
      postdir = File.dirname(postpath)
      destdir = File.dirname(post.destination(""))
      site = post.site
      sitesrcdir = site.source
      contents = Dir.glob(File.join(postdir, '**', '*')) do |filepath|
        if filepath != postpath
          filedir, filename = File.split(filepath[sitesrcdir.length..-1])
          reldir, dolaki = File.split(filepath[postdir.length..-1])
          unaccepted_formats = [".md", ".meta", ".html", ".json", "Rakefile"]
          if reldir.include? "/_"
            puts " dir #{reldir} excluded"
          else
          if File.file?(filepath)
            if unaccepted_formats.include? File.extname(filename)
              puts " File #{filename} excluded"
            else
          site.static_files <<
            PostFile.new(site, sitesrcdir, filedir, filename, destdir + reldir)
          end
          end
        end
        end
      end
      #puts "========================================================"

    end

    # Generate content by copying files associated with each post.
    def generate(site)
      site.collections.each do |col|
        col[1].docs.each do |post|
          copy_doc_files(post)
        end
      end
    end
  end
end

The problem now is not the plugin, as it is working perfectly. The problem is that jekyll itself puts all assets inside collection folders in the root folder of the site. So it means all collection assets are in two place. 1- beside the rendered html (as intended by the plugin), and 2- in the site root.

@hosnas

This comment has been minimized.

Show comment
Hide comment
@hosnas

hosnas Oct 4, 2017

Ok so I found the snippet of code in jekyll which is responsible for handling files inside collection folders in static_file.rb

# Applies a similar URL-building technique as Jekyll::Document that takes
# the collection's URL template into account. The default URL template can
# be overriden in the collection's configuration in _config.yml.
def url
@url ||= if @collection.nil?
relative_path
else
::Jekyll::URL.new({
:template => @collection.url_template,
:placeholders => placeholders,
})
end.to_s.gsub(%r!/$!, "")
end

According to this, all files inside collection folder, no matter where exactly they are, are copied in a folder based collection template url.
so for example if template url is like /:name they are all put inside the root directory. and if the template is like /mycollection/:name they are all put in mycollection folder inside site root.
Now I don't know why it is designed to behave like this. But is there something to turn off this behavior? In other words, I want to handle the collection files myself with the plugin named above. Not jekyll.

hosnas commented Oct 4, 2017

Ok so I found the snippet of code in jekyll which is responsible for handling files inside collection folders in static_file.rb

# Applies a similar URL-building technique as Jekyll::Document that takes
# the collection's URL template into account. The default URL template can
# be overriden in the collection's configuration in _config.yml.
def url
@url ||= if @collection.nil?
relative_path
else
::Jekyll::URL.new({
:template => @collection.url_template,
:placeholders => placeholders,
})
end.to_s.gsub(%r!/$!, "")
end

According to this, all files inside collection folder, no matter where exactly they are, are copied in a folder based collection template url.
so for example if template url is like /:name they are all put inside the root directory. and if the template is like /mycollection/:name they are all put in mycollection folder inside site root.
Now I don't know why it is designed to behave like this. But is there something to turn off this behavior? In other words, I want to handle the collection files myself with the plugin named above. Not jekyll.

@jekyllbot

This comment has been minimized.

Show comment
Hide comment
@jekyllbot

jekyllbot Dec 4, 2017

Contributor

This issue has been automatically marked as stale because it has not been commented on for at least two months.

The resources of the Jekyll team are limited, and so we are asking for your help.

If this is a bug and you can still reproduce this error on the 3.3-stable or master branch, please reply with all of the information you have about it in order to keep the issue open.

If this is a feature request, please consider building it first as a plugin. Jekyll 3 introduced hooks which provide convenient access points throughout the Jekyll build pipeline whereby most needs can be fulfilled. If this is something that cannot be built as a plugin, then please provide more information about why in order to keep this issue open.

This issue will automatically be closed in two months if no further activity occurs. Thank you for all your contributions.

Contributor

jekyllbot commented Dec 4, 2017

This issue has been automatically marked as stale because it has not been commented on for at least two months.

The resources of the Jekyll team are limited, and so we are asking for your help.

If this is a bug and you can still reproduce this error on the 3.3-stable or master branch, please reply with all of the information you have about it in order to keep the issue open.

If this is a feature request, please consider building it first as a plugin. Jekyll 3 introduced hooks which provide convenient access points throughout the Jekyll build pipeline whereby most needs can be fulfilled. If this is something that cannot be built as a plugin, then please provide more information about why in order to keep this issue open.

This issue will automatically be closed in two months if no further activity occurs. Thank you for all your contributions.

@jekyllbot jekyllbot added the stale label Dec 4, 2017

@ashmaroli ashmaroli added pinned and removed stale labels Jan 28, 2018

@hosnas

This comment has been minimized.

Show comment
Hide comment
@hosnas

hosnas Feb 25, 2018

@ashmaroli Can you elaborate what is the status of this issue now? Is jekyll postfiles and collection files are trated the same way right now?

hosnas commented Feb 25, 2018

@ashmaroli Can you elaborate what is the status of this issue now? Is jekyll postfiles and collection files are trated the same way right now?

@ashmaroli

This comment has been minimized.

Show comment
Hide comment
@ashmaroli

ashmaroli Feb 25, 2018

Member

This is still in the unresolved category.
Posts and their associated assets behave differently in comparison to files and documents in other collections.. This cannot be normalized (have consistent behavior) without breaking a lot of existing sites.

Member

ashmaroli commented Feb 25, 2018

This is still in the unresolved category.
Posts and their associated assets behave differently in comparison to files and documents in other collections.. This cannot be normalized (have consistent behavior) without breaking a lot of existing sites.

@hosnas

This comment has been minimized.

Show comment
Hide comment
@hosnas

hosnas Feb 25, 2018

@ashmaroli can you give me explanation or point me to a documentation that explains how assets inside collection folders are treated?

_my_collection/
└── some_subdir
    └── some_doc.md
    └── some_pic.jpg

For example hove something like above is rendered now?

hosnas commented Feb 25, 2018

@ashmaroli can you give me explanation or point me to a documentation that explains how assets inside collection folders are treated?

_my_collection/
└── some_subdir
    └── some_doc.md
    └── some_pic.jpg

For example hove something like above is rendered now?

@ashmaroli

This comment has been minimized.

Show comment
Hide comment
@ashmaroli

ashmaroli Feb 25, 2018

Member

can you give me explanation or point me to a documentation

Please refrain from asking questions related to how to.. here.


_my_collection/
└── some_subdir
   └── some_doc.md
   └── some_pic.jpg

will render into:

  • _site/my_collection/some_subdir/some_doc.html
  • _site/my_collection/some_subdir/some_pic.jpg
Member

ashmaroli commented Feb 25, 2018

can you give me explanation or point me to a documentation

Please refrain from asking questions related to how to.. here.


_my_collection/
└── some_subdir
   └── some_doc.md
   └── some_pic.jpg

will render into:

  • _site/my_collection/some_subdir/some_doc.html
  • _site/my_collection/some_subdir/some_pic.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment