按修改日期而不是发布日期对Jekyll帖子进行排序? [英] Sorting Jekyll posts by modification date instead of posted date?

查看:212
本文介绍了按修改日期而不是发布日期对Jekyll帖子进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于经常更新帖子的人,有必要根据上次修改日期将帖子从新到旧进行排序,而不是Jekyll的默认发布日期排序.

似乎没有简单的方法可以做到这一点.我已经阅读并测试了几乎所有的方法.

以下是有效的方法(部分符合预期):

使用了该宝石 https://github.com/gjtorikian/jekyll-last-修改为,但现在我必须在每个帖子的前面手动输入last_modified_at,以使Jekyll使用此循环对帖子进行排序:

{% assign sorted_posts = site.posts | sort: "last_modified_at" | reverse %}
{% for post in sorted_posts %}
<!-- CODE HERE -->
{% endfor %}

问题在于每个帖子中都有last_modified_at,因为每次我按下CTRL + S来保存帖子时,插件都会阻止该插件自动设置该值.

有什么办法可以使它自动化?

解决方案

它仅在开发中有效,不确定为什么在生产中也不能正常使用.每当我部署网站时,它都会更新具有相同日期的所有帖子.

我非常感谢钩子和这篇文章: https://stackoverflow.com/a/36769049

步骤:

  • 在名为hook-add-last-modified-date.rb
  • _plugins文件夹中创建了新文件
  • 将此代码粘贴到其中并保存:

    Jekyll::Hooks.register :posts, :pre_render do |post|
    
    # get the current post last modified time
    modification_time = File.mtime( post.path )
    
    # inject modification_time in post's datas.
    post.data['last-modified-date'] = modification_time
    
    end
    

  • 当我现在在帖子上按CTRL + S时,会发生两件事:a)更新我有{{ post.last-modified-date | date_to_xmlschema }}的位置的上次修改日期,并b)将其颠簸到顶部我在索引页面上的帖子,因为它是按该变量排序的.

爱你们!

For someone that updates posts very often it's necessary to have posts sorted from new to old based on last modification date instead of Jekyll's default sort by posted date.

There seems to be no easy way of accomplishing this. I've read and tested pretty much all methods out there.

Here's what worked (partially as expected):

Used this gem https://github.com/gjtorikian/jekyll-last-modified-at but now I have to manually enter last_modified_at in each post's front-matter in order for Jekyll to sort the posts using this loop:

{% assign sorted_posts = site.posts | sort: "last_modified_at" | reverse %}
{% for post in sorted_posts %}
<!-- CODE HERE -->
{% endfor %}

The issue is having the last_modified_at inside each post as it stops the plugin from auto-setting that value every time I hit CTRL+S to save a post.

Is there any way I can automate this?

解决方案

EDIT: It only works in DEVELOPMENT, not sure why it doesn't work in production as well. It updates ALL posts with the same date whenever I deploy the website.

I'm very thankful for hooks and this post ofc: https://stackoverflow.com/a/36769049

Steps:

  • created new file in _plugins folder named hook-add-last-modified-date.rb
  • paste this code inside it and save:

    Jekyll::Hooks.register :posts, :pre_render do |post|
    
    # get the current post last modified time
    modification_time = File.mtime( post.path )
    
    # inject modification_time in post's datas.
    post.data['last-modified-date'] = modification_time
    
    end
    

  • when I hit CTRL+S on a post now 2 things happen: a) updates the last modified date wherever I have {{ post.last-modified-date | date_to_xmlschema }} and b) it bumps it to the top of my posts on the index page because it is sorting by that variable.

Love you guys!

这篇关于按修改日期而不是发布日期对Jekyll帖子进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆