如何遍历 liquid/jekyll 中过去两周的前 3 篇帖子?

How to iterate through the top 3 posts from the last two weeks in liquid/jekyll?

让我补充一些背景信息:我正在尝试使用 jekyll 将我无聊的报告自动化到管理层!

我想为我所做的每一个 activity 写一个 post,每周,从本周和上周开始,将编译报告与 "top 3 highlights" 一起发送。

作为一个 jekyll 新手,我的尝试是添加发布周,如果 post 是 yaml 前言中的亮点:

---
layout: event
title:  "Gave a Jelyll Talk!"
date:   2015-04-23 16:05:04
highlight: week
week: 17
---

并获取模板中的最后两周,如下所示:

---
layout: email
---

Here are my activities from the last two weeks:

{% assign hls = (site.posts | where: "highlight" , "week") %}
{% assign weeks = (hls | group_by: "week") %}
{% assign thisw = weeks[0] %}
{% assign lastw = weeks[1] %}

<h1>Week #{{thisw.name}}</h1>
<ul>
    {% for post in thisw.items %}
    <li>{{post.title}}</li>
    {% endfor %} 
</ul>

<h1>Week #{{lastw.name}}</h1>
<ul>
    {% for post in lastw.items %}
    <li>{{post.title}}</li>
    {% endfor %} 
</ul>

有点效果,但是,我想问一下:

是否可以自动计算当前周,仅获取过去两周(不是未来或之前)的 posts,每周仅获取最新的 3 个亮点并避免重复 html模板?

是的...应该可以。
至少你可以使用 {{ site.time }} 获取当前(即构建)时间并使用 liquid filter 格式化它以获得周数,如下所示:

{% assign current_week = site.time | date: "%w" | plus: 0 %}

(您还需要应用 plus: 0 过滤器,以获取数字而不是分配的字符串。)现在事情变得很糟糕,因为您可能想适当地考虑年轮...但是它应该是可行的。

我可以考虑获取过去三周的 YEAR-WEEKNUMBER 之类的字符串,将具有相同日期字符串的所有帖子分配给一个新变量,比如 posts_lastweek 等等。然后,遍历帖子,比较并限制 for 循环 ({% for post in posts_lastweek | limit: 3 %}) 或使用计数器来限制输出。

让我知道这是否适合你...

我会考虑为 Jekyll 写一个插件,像这样:

WeeklyHighlights.rb

module Jekyll
    class WeeklyHighlights < Generator

        safe true
        priority :high

        def week_id(time)
            # to handle turn of year properly
            return time.strftime('%Y-%W')
        end

        def generate(site)
            # hash (dict) to store highlights grouped by week number
            highlights_by_week = {}

            today = Time.now
            # initialize recent weeks with empty highlights
            (0 .. 4).each do |i|
                w = week_id(today - i)
                highlights_by_week[w] = []
            end

            # group highlights according to week number
            site.posts.each do |post|
                if post['highlight'] != 'week'
                    next
                end
                week = week_id(post.date)
                highlights_by_week[week] ||= []
                highlights_by_week[week] << post
            end

            # make array of arrays of highlights, in the required order
            weekly_highlights = []
            highlights_by_week.keys.sort.reverse.each do |w|
                highlights_by_week[w].sort! {|a,b| b.date <=> a.date} # = hl.sort{|a,b| b.date <=> a.date }
                weekly_highlights << {'id' => w, 'hls' => highlights_by_week[w]}
            end

            # store prepared highlights
            site.config['weekly_highlights'] = weekly_highlights
        end
    end
end

将文件放在plugins目录下(一般在Jekyll站点的根目录下plugins_plugins),然后在模板中:

{% for week in site.weekly_highlights limit:2 %}
<h2>week {{week.id}}</h2>
<ol>
{% for p in week.hls limit:3 %}
<li>{{p.title}}</li>
{% endfor %}
</ol>
{% endfor %}

关于 Jekyll 插件的更多信息:official documentation