在没有插件的情况下,如何在Jekyll中按月份和年份表创建帖子?
我想创建一个表,在一个Jekyll博客中按月份和年份统计帖子(并链接到每个组中的第一个),而不使用任何插件(因为GitHub不会运行它们)。是我想要复制的东西;我非常感谢您的指点。这是在没有插件的情况下,如何在Jekyll中按月份和年份表创建帖子?,jekyll,Jekyll,我想创建一个表,在一个Jekyll博客中按月份和年份统计帖子(并链接到每个组中的第一个),而不使用任何插件(因为GitHub不会运行它们)。是我想要复制的东西;我非常感谢您的指点。这是archive.html页面的代码。你可以看到 测试了400篇文章,在构建/服务方面没有性能问题 --- layout: page title: archive --- {%comment%} ++++++++++ We first find start and end years ++++++++++ {%en
archive.html
页面的代码。你可以看到
测试了400篇文章,在构建/服务方面没有性能问题
---
layout: page
title: archive
---
{%comment%} ++++++++++ We first find start and end years ++++++++++ {%endcomment%}
{% assign startYear = 2222 %}
{% assign endYear = 1 %}
{% for post in site.posts %}
{%comment%} +++++
"| plus: 0" casts postYear to fixnum, because "post.date | date: "%Y"" is a string
and comparing "2013" with 2012 (string / number) throws an error
+++++ {%endcomment%}
{% assign postYear = post.date | date: "%Y" | plus: 0 %}
{% if postYear > endYear %}{% assign endYear = postYear %}{% endif %}
{% if postYear < startYear %}{% assign startYear = postYear %}{% endif %}
{% endfor %}
{%comment%} +++++++++++++++ build the table +++++++++++++++ {%endcomment%}
{% assign tableContent = "<tr><th></th><th>Jan</th><th>Feb</th><th>Mar</th><th>Apr</th><th>May</th><th>Jun</th><th>Jul</th><th>Aug</th><th>Sep</th><th>Oct</th><th>Nov</th><th>Dec</th></tr>" %}
{%comment%} +++++
currentPostIndex is used to loop over post in an efficient way
Knowing that posts a sorted by date, we don't need to loop over
all posts each time we want to inspect them.
Instead we only loop through posts we don't already inspect.
+++++ {%endcomment%}
{% assign currentPostIndex = 0 %}
{%comment%} +++++ site.posts array is zero numbered, so last index = size-1 +++++ {%endcomment%}
{% assign lastPostIndex = site.posts.size | minus: 1 %}
{%comment%} +++++ Looping trough years in REVERSE order +++++ {%endcomment%}
{% for year in (startYear...endYear) reversed %}
{% assign yearRow = "<tr><th>" | append: year | append: "</th>" %}
{%comment%} +++++ Trick to create an empty array +++++ {%endcomment%}
{% assign yearCellsArray = "" | split: "/" %}
{%comment%} +++++ Looping over month reversed +++++ {%endcomment%}
{% for month in (1...12) reversed %}
{% assign postsThisYearMonth = 0 %}
{% assign monthCell = "<td>" %}
{% for postIndex in (currentPostIndex...lastPostIndex) %}
{% assign p = site.posts[postIndex] %}
{% assign pYear = p.date | date: "%Y" | plus: 0 %}
{% assign pMonth = p.date | date: "%m" | plus: 0 %}
{% if pYear == year and pMonth == month %}
{% assign postsThisYearMonth = postsThisYearMonth | plus: 1 %}
{% else %}
{%comment%} +++++ Here we stop the loop +++++ {%endcomment%}
{% assign currentPostIndex = postIndex %}
{% break %}
{% endif %}
{% endfor %}
{% if postsThisYearMonth > 0 %}
{% assign linkTargetId = "#" | append: year | append: "-" | append: month %}
{% assign linkStart = "<a href='" | append: linkTargetId | append: "'>" %}
{% assign linkEnd = "</a>" %}
{% assign cellContent = linkStart | append: postsThisYearMonth | append: linkEnd %}
{% else %}
{% assign cellContent = " " %}
{% endif %}
{% assign monthCell = monthCell | append: cellContent | append: "</td>" %}
{% assign yearCellsArray = yearCellsArray | unshift: monthCell %}
{% endfor %}
{% assign yearCells = yearCellsArray | join: "" %}
{% assign yearRow = yearRow | append: yearCells | append: "</tr>" %}
{% assign tableContent = tableContent | append: yearRow %}
{% endfor %}
<h2>All Posts By Date</h2>
<table class="table table-striped blogcalendar">
<tbody>
{{ tableContent }}
</tbody>
</table>
{%comment%} +++++ Printing posts by Year then month +++++ {%endcomment%}
{% assign currentPostIndex = 0 %}
{% assign lastPostIndex = site.posts.size | minus: 1 %}
{% for year in (startYear...endYear) reversed %}
<h3>{{year}}</h3>
{% assign currentYear = year %}
{% for month in (1...12) reversed %}
{% assign postsArray = "" | split: "/" %}
{%comment%} +++++ Find post for this year / month +++++ {%endcomment%}
{% for postIndex in (currentPostIndex...lastPostIndex) %}
{% assign p = site.posts[postIndex] %}
{% assign pYear = p.date | date: "%Y" | plus: 0 %}
{% assign pMonth = p.date | date: "%m" | plus: 0 %}
{% if pYear == year and pMonth == month %}
{% assign postsArray = postsArray | push: p %}
{% else %}
{%comment%} +++++ Here we stop the loop +++++ {%endcomment%}
{% assign currentPostIndex = postIndex %}
{% break %}
{% endif %}
{% endfor %}
{% assign postArraySize = postsArray | size %}
{%comment%} +++++ Printing posts if we have some for this year month +++++ {%endcomment%}
{% if postArraySize and postArraySize > 0 %}
{%comment%} +++++ get month name from a post.date +++++ {%endcomment%}
{% assign post = postsArray | first %}
{% assign monthName = post.date | date: "%B" %}
{% assign monthId = year | append: "-" | append: month %}
<h4 id="{{ monthId }}">{{ monthName }} {{ year }}</h4>
<table class="table table-striped">
{% for p in postsArray %}
<tr>
<td>{{ p.date | date: "%Y-%m-%d" }}</td>
<td>
<a href="{{ site.baseurl }}{{ p.url }}">
{{ p.title }}
</a>
</td>
</tr>
{% endfor %}
</table>
{% endif %}
{% endfor %}
{% endfor %}
---
版面:第页
标题:档案
---
{%comment%}++++++我们首先找到开始年和结束年+++++{%endcomment%}
{%assign startYear=2222%}
{%assign endYear=1%}
{site.posts%中的post为%s}
{%comment%}+++++
“| plus:0”将postYear强制转换为fixnum,因为“post.date | date:“%Y”是一个字符串
将“2013”与2012(字符串/数字)进行比较会抛出一个错误
+++++{%endcomment%}
{%assign postYear=post.date | date:“%Y”|加上:0%}
{%if postYear>endYear%}{%assign endYear=postYear%}{%endif%}
{%if postYear0%}
{%assign linkTargetId=“#”|追加:年|追加:“-”|追加:月%)
{%assign linkStart=”“%}
{%assign cellContent=linkStart | append:poststhisearmonth | append:linkEnd%}
{%else%}
{%assign cellContent=“%nbsp”}
{%endif%}
{%assign monthCell=monthCell | append:cellContent | append:%}
{%assign yearCellsArray=yearCellsArray | unshift:monthCell%}
{%endfor%}
{%assign yearCells=yearCellsArray |连接:%}
{%assign yearRow=yearRow | append:yearCells | append:%}
{%assign tableContent=tableContent | append:yearRow%}
{%endfor%}
按日期分列的所有员额
{{tableContent}}
{%comment%}+++按年份然后按月份打印帖子++++{%endcomment%}
{%assign currentPostIndex=0%}
{%assign lastPostIndex=site.posts.size |减:1%}
{(开始日期…结束日期)中年份的%}
{{year}
{%assign currentYear=year%}
{(1…12)中月份的%}
{%assign postsArray=”“|拆分:“/”%}
{%comment%}+++Find post for this year/month++++++++++{%endcomment%}
{(currentPostIndex…lastPostIndex)%%中的postIndex为%
{%assign p=site.posts[postIndex]}
{%assign pYear=p.date | date:“%Y”|加上:0%}
{%assign pMonth=p.date | date:“%m”|加上:0%}
{%如果pYear==年,pMonth==月%}
{%assign postsArray=postsArray | push:p%}
{%else%}
{%comment%}+++++这里我们停止循环+++++{%endcomment%}
{%assign currentPostIndex=postIndex%}
{%break%}
{%endif%}
{%endfor%}
{%assign postArraySize=postsArray | size%}
{%comment%}+++Printing posts,如果我们今年月份有一些的话(+++++{%endcomment%}
{%如果后处理和后处理>0%}
{%comment%}++++从post.date获取月名++++{%endcomment%}
{%assign post=postsArray | first%}
{%assign monthName=post.date |日期:“%B”%}
{%assign monthId=year | append:“-”| append:month%}
{{monthName}{{year}
{postsArray%中p的%s}
{p.date | date:%Y-%m-%d}
{%endfor%}
{%endif%}
{%endfor%}
{%endfor%}
这篇文章在Meta上进行了讨论,所以任何不规则的投票模式都很可能是因为:那些反对票是什么?此答案已批准并合并到海报项目中。对这段代码的否决票需要参数。@DavidJacquel可能是因为人们认为你不应该回复。谢谢你@Keith。我可以告诉你,我为这个答案感到骄傲。我用它找到了一份工作!