Skip to content

Commit 74e03cc

Browse files
authored
添加自定义过滤器以按版本号排序字符串 (#388)
1 parent 7b2b3ff commit 74e03cc

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

_layouts/changelog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{{ content }}
66

77
{% assign channel = page.channel | default: 'stable' %}
8-
{% assign changelogs = site.changelogs | where: "channel", channel | reverse %}
8+
{% assign changelogs = site.changelogs | where: "channel", channel | version_sort: "slug" | reverse %}
99
{% for item in changelogs %}
1010
{% assign version = item.slug %}
1111
<h1 id="{% if forloop.index == 1 %}nowchange{% else %}HMCL-{{ version }}{% endif %}" data-version="{{ version }}">HMCL {{ version }}</h1>

_plugins/filter-version-sort.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module VersionSortFilter
2+
def version_sort(input, property)
3+
raise ArgumentError, "Cannot sort a null object." if input.nil?
4+
raise ArgumentError, "Cannot sort an object with a null property." if property.nil?
5+
raise ArgumentError, "Property #{property} is not an array of strings." unless valid_string_array?(input, property)
6+
7+
input.sort_by { |version| version_to_numbers(version[property]) }
8+
end
9+
10+
private
11+
12+
def valid_string_array?(array, property)
13+
array.is_a?(Array) && array.all? { |element| element[property].is_a?(String) }
14+
end
15+
16+
def version_to_numbers(version_string)
17+
version_string.split('.').map { |n| n.to_i }
18+
end
19+
end
20+
21+
Liquid::Template.register_filter(VersionSortFilter)

0 commit comments

Comments
 (0)