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
Sort collection items by an attribute. #5904
Conversation
sort collection items by specifying a "sort_by" metadata key in the _config.yml and assigning the key a value that corresponds to a key set in the document's Front Matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests will be added later.
@ashmaroli thanks for the quick turnaround, but this still requires people to modify multiple files when they want to reorder a couple of files or insert a new one. For example, if the collection includes |
@gvwilson True. Hence the reason I said that this implementation might not qualify for the bounty. |
@pathawks I added a basic test step. Will add more if you think this implementation could be beneficial addition to the Core.. |
I don't think that sorting by an I'm not sure what problem this is solving. |
@pathawks I've added an additional implementation to have the documents array modified by an
will add more tests, if required, after further feedback. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation and tests look good to me, but handling of "extra" files in collection doesn't appear specified or tested (but my Ruby knowledge is weak, so I may be missing something).
features/collections.feature
Outdated
tutorials: | ||
output: true | ||
order: | ||
- getting-started.md |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These appear to be in the same order as defined by the internal header's lesson
value, so there's no way to tell whether the sorting is due to the config information or the internal data. Can this test be modified to put the files in a different order so that we can be sure they're being sorted by config spec?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the collection would be sorted by lesson
key only if sort_by
key is set to "lesson" here. I'll nevertheless edit the order in the test to bring in clarity.
lib/jekyll/collection.rb
Outdated
end | ||
end | ||
docs.sort_by! { |d| d.data["sort_index"] } | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- What happens to files that are in the collection directory but not specified in the explicit order list in config?
- What happens to files that are in the collection directory but do not specify the key being used for sorting (in this case, don't have a
lesson
key)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens to files that are in the collection directory.. ..don't have a
lesson
key?
The build process aborts with an Error
.
I'll push a couple of patches to better handle such situations.
Failing if there are files in the collection that are not expected
(i.e., not listed in the ordering in _config.yml) is a useful behavior
in some cases, but in others, people may want to append them to the
collection in natural (alphabetic) order - the analogy is with "extra"
arguments to a function, which can either be an error or can be really
useful. Is it worth allowing another parameter in the config for the
collection to specify which behavior is desired? That will make it easy
to expand behaviors in future.
|
This seems like it makes things complicated.
This seems like it makes things complicated. |
How about "files not listed in the config are ignored" (and
equivalently, "files lacking the specified sorting key are ignored")?
|
If the user leaves out a certain document from the `order` array, its `sort_index` value will equal the total no. of documents in the collection. The value will be same for all documents left out and such documents will be sorted by the default algorithm.
This is how things will roll now:
|
e053ea7
to
d70d798
Compare
lib/jekyll/collection.rb
Outdated
end.map!(&:last) | ||
end | ||
|
||
def sort_mapped_items(apples, oranges) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this helper method doesn't make sense because the input and output is both created and destroyed inside another method. It make more sense for all use of this helper array to be in the sort_docs_by_key!
method. I'd like to see it all in one place to prevent confusing indirection.
0962995
to
4d03f68
Compare
Ruby 2.1.10 consistently fails here :( https://travis-ci.org/jekyll/jekyll/jobs/271100330 |
... and also on AppVeyor @ Ruby2.4.x ... |
Sorry, @ashmaroli 😦 I don't know why it's failing! Maybe something with the |
Can someone please take another look at this - it would greatly simplify our lives to have it merged. cc @parkr @ashmaroli @pathawks @DirtyF |
2c3cdf6
to
7dfa8df
Compare
7dfa8df
to
6b4116f
Compare
@gvwilson Over the course of time, the branch changed ideas and is now different from your original feature-request.. Even with this PR (as it stands currently) being merged in you'll not be able to rearrange collection documents manually by listing documents in the config file. |
That's really unfortunate: the bounty I paid was precisely so that collection documents' order could be specified in the configuration file. |
@gvwilson The Core Team is not willing to incorporate the proposed implementation into Jekyll Core. Besides, you need not wire your site's source to GitHub Pages to get it built on the fly, there are other alternatives like Netlify.com that allow you to use custom plugins and gems in the build sessions. |
sort collection items by specifying a "sort_by" metadata key in the
_config.yml
and assigning thekey
avalue
that corresponds to a key set in the document'sFront Matter
.Notes:
key
defined.sort!
if none of the items have the key defined.Example
Tests will be added later.
/cc @jekyll/ecosystem
@gvwilson, this implementation may not qualify for your bounty, but in theory should sort the documents and assign the
prev / next
pagers accordingly.Fixes #5754