Add hook to generate the lastmod of posts

This commit is contained in:
Cotes Chung 2020-11-19 01:58:35 +08:00
parent 1889b6b4e0
commit 10bc44367a
6 changed files with 43 additions and 57 deletions

View file

@ -10,8 +10,6 @@
<div class="access">
{% if site.data.updates %}
{% include update-list.html %}
{% if update_list.size > 0 %}
@ -21,23 +19,17 @@
<ul class="post-content pl-0 pb-1 ml-1 mt-2">
{% for item in update_list %}
{% assign post_url = item | split: "::" | last | url_encode | prepend: "/posts/" | append: "/" %}
{% assign post = site.posts | where: "url", post_url | first %}
{% if post %}
{% assign index = item | split: "::" | last | plus: 0 %}
{% assign post = site.posts[index] %}
{% assign url = post.url | relative_url %}
<li><a href="{{ url }}">{{ post.title }}</a></li>
{% endif %}
{% endfor %}
</ul>
</div><!-- #access-lastmod -->
</div> <!-- #access-lastmod -->
{% endif %}
{% endif %} <!-- site.data.updates -->
{% include trending-tags.html %}
{% if trending_tags.size > 0 %}

View file

@ -10,11 +10,13 @@
{% assign all_list = "" | split: "" %}
{% for entry in site.data.updates %}
{% for post in site.posts %}
{% if post.lastmod %}
{% capture elem %}
{{- entry.lastmod -}}::{{- entry.filename -}}
{{- post.lastmod | date: "%Y%m%d%H%M%S" -}}::{{- forloop.index0 -}}
{% endcapture %}
{% assign all_list = all_list | push: elem %}
{% endif %}
{% endfor %}
{% assign all_list = all_list | sort | reverse %}

View file

@ -31,22 +31,10 @@ layout: default
</div>
<!-- lastmod -->
{%- capture filename -%}
{{ page.url | split: "/" | last }}
{%- endcapture -%}
{% for item in site.data.updates %}
{% assign encode_filename = item.filename | url_encode %}
{% if filename == encode_filename %}
{% assign lastmod = item.lastmod %}
{% break %}
{% endif %}
{% endfor %}
{% if lastmod %}
{% if page.lastmod %}
<div>
Updated
{% include timeago.html date=lastmod class="lastmod" tooltip=true %}
{% include timeago.html date=page.lastmod class="lastmod" tooltip=true %}
</div>
{% endif %}
@ -61,7 +49,6 @@ layout: default
</div> <!-- .post-meta -->
<div class="post-content">
{%- capture img_placehodler -%}
data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
{% endcapture%}

View file

@ -0,0 +1,11 @@
# Check for changed posts
Jekyll::Hooks.register :posts, :post_init do |post|
commit_num = `git rev-list --count HEAD "#{ post.path }"`
if commit_num.to_i > 1
lastmod_date = `git log -1 --pretty="%ad" --date=iso "#{ post.path }"`
post.data['lastmod'] = lastmod_date
end
end

View file

@ -30,10 +30,10 @@ layout: compress
<title>{{ post.title }}</title>
<link href="{{ post_absolute_url }}" rel="alternate" type="text/html" title="{{ post.title }}" />
<published>{{ post.date | date_to_xmlschema }}</published>
{% if post.seo.date_modified %}
<updated>{{ post.seo.date_modified | date_to_xmlschema }}</updated>
{% if post.lastmod %}
<updated>{{ post.lastmod | date_to_xmlschema }}</updated>
{% else %}
<updated>{{ site.time | date_to_xmlschema }}</updated>
<updated>{{ post.date | date_to_xmlschema }}</updated>
{% endif %}
<id>{{ post_absolute_url }}</id>
<content src="{{ post_absolute_url }}" />

View file

@ -14,7 +14,11 @@ layout: compress
{% for post in site.posts %}
<url>
<loc>{{ site.url | append: site.baseurl | append: post.url }}</loc>
<lastmod>{{ site.time | date_to_xmlschema }}</lastmod>
{% if post.lastmod %}
<lastmod>{{ post.lastmod | date_to_xmlschema }}</lastmod>
{% else %}
<lastmod>{{ post.date | date_to_xmlschema }}</lastmod>
{% endif %}
{% if post.sitemap.changefreq %}
<changefreq>{{ post.sitemap.changefreq }}</changefreq>
@ -59,19 +63,9 @@ layout: compress
{% continue %}
{% endif %}
{% capture lastmod %}
{% if page.lastmod %}
{{ page.lastmod }}
{% elsif page.date %}
{{ page.date }}
{% else %}
{{ site.time }}
{% endif %}
{% endcapture %}
<url>
<loc>{{ site.url | append: site.baseurl | append: page.url | remove: "index.html" }}</loc>
<lastmod>{{ lastmod | date_to_xmlschema }}</lastmod>
<lastmod>{{ site.time | date_to_xmlschema }}</lastmod>
{% if page.sitemap.changefreq %}
<changefreq>{{ page.sitemap.changefreq }}</changefreq>