mirror of
https://gitee.com/huanghongxun/HMCL-docs.git
synced 2026-01-13 07:32:40 +08:00
添加自定义过滤器以按版本号排序字符串 (#388)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
7b2b3ff08b
commit
74e03cc184
@ -5,7 +5,7 @@ layout: document
|
|||||||
{{ content }}
|
{{ content }}
|
||||||
|
|
||||||
{% assign channel = page.channel | default: 'stable' %}
|
{% 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 %}
|
{% for item in changelogs %}
|
||||||
{% assign version = item.slug %}
|
{% assign version = item.slug %}
|
||||||
<h1 id="{% if forloop.index == 1 %}nowchange{% else %}HMCL-{{ version }}{% endif %}" data-version="{{ version }}">HMCL {{ version }}</h1>
|
<h1 id="{% if forloop.index == 1 %}nowchange{% else %}HMCL-{{ version }}{% endif %}" data-version="{{ version }}">HMCL {{ version }}</h1>
|
||||||
|
|||||||
21
_plugins/filter-version-sort.rb
Normal file
21
_plugins/filter-version-sort.rb
Normal file
@ -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)
|
||||||
Loading…
Reference in New Issue
Block a user