diff --git a/_layouts/changelog.html b/_layouts/changelog.html index 9a72f040..40261deb 100644 --- a/_layouts/changelog.html +++ b/_layouts/changelog.html @@ -5,7 +5,7 @@ {{ content }} {% assign channel = page.channel | default: 'stable' %} -{% assign changelogs = site.changelogs | where: "channel", channel | reverse %} +{% assign changelogs = site.changelogs | where: "channel", channel | version_sort: "slug" | reverse %} {% for item in changelogs %} {% assign version = item.slug %}

HMCL {{ version }}

diff --git a/_plugins/filter-version-sort.rb b/_plugins/filter-version-sort.rb new file mode 100644 index 00000000..8b49d525 --- /dev/null +++ b/_plugins/filter-version-sort.rb @@ -0,0 +1,21 @@ +module VersionSortFilter + def version_sort(input, property) + raise ArgumentError, "Cannot sort a null object." if input.nil? + raise ArgumentError, "Cannot sort an object with a null property." if property.nil? + raise ArgumentError, "Property #{property} is not an array of strings." unless valid_string_array?(input, property) + + input.sort_by { |version| version_to_numbers(version[property]) } + end + + private + + def valid_string_array?(array, property) + array.is_a?(Array) && array.all? { |element| element[property].is_a?(String) } + end + + def version_to_numbers(version_string) + version_string.split('.').map { |n| n.to_i } + end +end + +Liquid::Template.register_filter(VersionSortFilter)