添加自定义过滤器以按版本号排序字符串 (#388)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
neveler 2026-01-01 16:36:47 +08:00 committed by GitHub
parent 7b2b3ff08b
commit 74e03cc184
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View File

@ -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>

View 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)