gjtorikian / jekyll-last-modified-at

A Jekyll plugin to show the last_modified_at time of a post.
MIT License
241 stars 38 forks source link

Sort collection by last_modified_at #70

Open lsarni opened 5 years ago

lsarni commented 5 years ago

I'm trying to use this value to sort a custom collection called docs.

{% assign sorted_docs = site.docs | sort: 'last_modified_at' | reverse %} This causes:

Liquid Exception: Liquid error (line 6): comparison of Array with Array failed in index.md Error: Liquid error (line 6): comparison of Array with Array failed Error: Run jekyll build --trace for more information.

The same happens if I remove the reverse. But it doesn't happen if I use other field to sort it (like title).

Since it isn't documented I'm not sure if this can be used for sorting, should it work?

xplosionmind commented 4 years ago

If it’s not a bug, it should definitely be a feature request! This happens to me too. It would be so useful to sort collections based on the last modification date and time.

This is the error message I get (my collection is called "Jar")

Liquid Warning: Liquid syntax error (line 87): Expected end_of_string but found pipe in "note in site.jar | sort: 'last_modified_at'" in home.html

Thanks a lot, this plugin is great!

henryiii commented 3 years ago

I've worked around this by adding a plugin that adds a string version, since that can be compared.

Real comparisons would be even better!

# frozen_string_literal: true

require 'fileutils'
require 'pathname'
require 'jekyll-last-modified-at'

module Recents
  # Generate change information for all markdown pages
  class Generator < Jekyll::Generator
    def generate(site)
      items = site.pages.select { |p| p.path.end_with? '.md' }
      items.each do |page|
        page.data['last_modified_at_str'] = Jekyll::LastModifiedAt::Determinator.new(site.source, page.path, '%FT%T%:z').to_s
      end
    end
  end
end
xplosionmind commented 2 years ago

Hi @henryiii! I am no Ruby developer, but I am wondering how I could include not only .md, but also .html files in your plugin.

henryiii commented 2 years ago

@xplosionmind,

String#end_with? takes multiple arguments. So just change this line:

items = site.pages.select { |p| p.path.end_with?('.md', '.html') }
xplosionmind commented 2 years ago

Thanks a lot!

T

heyteresagreen commented 1 year ago

Hi, I'm trying to display a list of notes ordered by most recently modified. I'm using this line: {% assign notes = site.notes | sort: 'last_modified_at' | reverse %}

It's not creating an error as in the above posts, and it does seem to be sorting the notes based on something, but it doesn't seem to be the last modified date. In my list it shows the note title and {{ note.last_modified_at }} which seems to be correct, but it isn't sorted in that order.

Is the issue my implementation, or just that the plugin isn't set up to do this?

Here's my full template if that helps:

{% assign notes = site.notes | sort: 'last_modified_at' | reverse %}

  {% for note in notes %}
  <p>
      <a href="{{ note.url }}" class="internal-link">{{ note.title }}</a> 
      <small>Updated {{ note.last_modified_at | date: "%B %-d, %Y" }}</small>
  </p>
  {% endfor %}