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

View file

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

View file

@ -31,22 +31,10 @@ layout: default
</div> </div>
<!-- lastmod --> <!-- lastmod -->
{%- capture filename -%} {% if page.lastmod %}
{{ 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 %}
<div> <div>
Updated Updated
{% include timeago.html date=lastmod class="lastmod" tooltip=true %} {% include timeago.html date=page.lastmod class="lastmod" tooltip=true %}
</div> </div>
{% endif %} {% endif %}
@ -61,7 +49,6 @@ layout: default
</div> <!-- .post-meta --> </div> <!-- .post-meta -->
<div class="post-content"> <div class="post-content">
{%- capture img_placehodler -%} {%- capture img_placehodler -%}
data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
{% endcapture%} {% 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> <title>{{ post.title }}</title>
<link href="{{ post_absolute_url }}" rel="alternate" type="text/html" title="{{ post.title }}" /> <link href="{{ post_absolute_url }}" rel="alternate" type="text/html" title="{{ post.title }}" />
<published>{{ post.date | date_to_xmlschema }}</published> <published>{{ post.date | date_to_xmlschema }}</published>
{% if post.seo.date_modified %} {% if post.lastmod %}
<updated>{{ post.seo.date_modified | date_to_xmlschema }}</updated> <updated>{{ post.lastmod | date_to_xmlschema }}</updated>
{% else %} {% else %}
<updated>{{ site.time | date_to_xmlschema }}</updated> <updated>{{ post.date | date_to_xmlschema }}</updated>
{% endif %} {% endif %}
<id>{{ post_absolute_url }}</id> <id>{{ post_absolute_url }}</id>
<content src="{{ post_absolute_url }}" /> <content src="{{ post_absolute_url }}" />

View file

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