Created
August 21, 2015 14:53
-
-
Save robszumski/e915b024ba6504c3f8ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Jekyll | |
module SortSemVer | |
def semver(input, property) | |
# check inputs | |
if input.nil? | |
raise ArgumentError.new("Cannot sort a null object.") | |
end | |
if property.nil? | |
raise ArgumentError.new("You must define the property that contains your semantic version number!") | |
else | |
# remove pages without the property | |
input.delete_if {|x| item_property(x, property) == nil } | |
# handle invalid semvers | |
input.delete_if {|x| !item_property(x, property).to_s.include? "."} | |
#sort remaining elements | |
input.sort { |apple, orange| | |
apple_version = item_property(apple, property).to_s.split(".") | |
orange_version = item_property(orange, property).to_s.split(".") | |
if apple_version[0] == orange_version[0] | |
if apple_version[1] == orange_version[1] | |
if apple_version[2] == orange_version[2] | |
1 | |
else | |
apple_version[2] <=> orange_version[2] | |
end | |
else | |
apple_version[1] <=> orange_version[1] | |
end | |
else | |
apple_version[0] <=> orange_version[0] | |
end | |
} | |
end | |
end | |
end | |
end | |
Liquid::Template.register_filter(Jekyll::SortSemVer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Attempting to copy https://github.com/jekyll/jekyll/blob/581dee7ba975e05622548fdfc785631dcff824f3/lib/jekyll/filters.rb#L217 and modify it to sort by semantic version number (
2.0.0
). I'm testing it within my site like:and a subset of a pages have front matter that looks like: