Skip to content

Instantly share code, notes, and snippets.

@robszumski
Created August 21, 2015 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robszumski/e915b024ba6504c3f8ce to your computer and use it in GitHub Desktop.
Save robszumski/e915b024ba6504c3f8ce to your computer and use it in GitHub Desktop.
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)
@robszumski
Copy link
Author

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:

{{site.pages | semver:'version'}}

and a subset of a pages have front matter that looks like:


---
layout: docs
version: 1.0.0

---

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment