Using liquid to sort posts alphabetically

后端 未结 7 1273
借酒劲吻你
借酒劲吻你 2021-02-01 19:45

Is there a way to sort a number of posts alphabetically, using Jekyll?

I have something like this now:

{% for post in site.categories.threat %}


        
7条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 20:00

    I adapted a Jekyll plugin from https://gist.github.com/3812259 to accomplish this. I couldn't use the plugin as-is, because it failed in the presence of null values. I'm a beginning ruby programmer, and coded the null handling with help from https://stackoverflow.com/a/808721/1135052

    sort_for example reversing the sort and performing case-sensitive string comparisons (ignored if the sorted property is not a string):

    {% sorted_for node in site.pages reversed sort_by:title case_sensitive:true %}
      {{ node.title }}
    {% endsorted_for %}
    

    sorted_keys_for example:

    {% sorted_keys_for tag in site.tags %}
      {{ tag }}
    Num posts: {{ site.tags[tag].size }} {% endsorted_keys_for %}

    For use in Jekyll, put this code in _plugins/sort_for.rb

    module Jekyll
      module SortedForImpl
        def render(context)
          sorted_collection = collection_to_sort context
          return if sorted_collection.empty?
          sort_attr = @attributes['sort_by']
          case_sensitive = @attributes['case_sensitive'] == 'true'
          i = sorted_collection.first
    
          if sort_attr != nil
            if i.to_liquid[sort_attr].instance_of? String and not case_sensitive
              sorted_collection.sort_by! { |i|
                k = i.to_liquid[sort_attr]
                k ? k.downcase : ''
              }
            else
              sorted_collection.sort_by! { |i|
                k = i.to_liquid[sort_attr]
                [k ? 1 : 0,k || 1]
              }
            end
          else
            if i.instance_of? String and not case_sensitive
              sorted_collection.sort_by! { |i| i.downcase }
            else
              sorted_collection.sort!
            end
          end
    
          original_name = @collection_name
          result = nil
          context.stack do
            sorted_collection_name = "#{@collection_name}_sorted".sub('.', '_')
            context[sorted_collection_name] = sorted_collection
            @collection_name = sorted_collection_name
            result = super
            @collection_name = original_name
          end
          result
        end
      end
    
      class SortedForTag < Liquid::For
        include SortedForImpl
    
        def collection_to_sort(context)
          return context[@collection_name].dup
        end
    
        def end_tag
          'endsorted_for'
        end
      end
    
      class SortedKeysForTag < Liquid::For
        include SortedForImpl
    
        def collection_to_sort(context)
          return context[@collection_name].keys
        end
    
        def end_tag
          'endsorted_keys_for'
        end
      end
    end
    
    Liquid::Template.register_tag('sorted_for', Jekyll::SortedForTag)
    Liquid::Template.register_tag('sorted_keys_for', Jekyll::SortedKeysForTag)
    

提交回复
热议问题