From 10bc44367a15dfa96fc34375643e7d6b33de4d2f Mon Sep 17 00:00:00 2001
From: Cotes Chung <11371340+cotes2020@users.noreply.github.com>
Date: Thu, 19 Nov 2020 01:58:35 +0800
Subject: [PATCH] Add hook to generate the lastmod of posts
---
_includes/panel.html | 30 +++++++++++-------------------
_includes/update-list.html | 12 +++++++-----
_layouts/post.html | 17 ++---------------
_plugins/posts-lastmod-hook.rb | 11 +++++++++++
feed.xml | 8 ++++----
sitemap.xml | 22 ++++++++--------------
6 files changed, 43 insertions(+), 57 deletions(-)
create mode 100644 _plugins/posts-lastmod-hook.rb
diff --git a/_includes/panel.html b/_includes/panel.html
index 35997c2..190c1f6 100644
--- a/_includes/panel.html
+++ b/_includes/panel.html
@@ -10,33 +10,25 @@
Updated
- {% include timeago.html date=lastmod class="lastmod" tooltip=true %}
+ {% include timeago.html date=page.lastmod class="lastmod" tooltip=true %}
{% endif %}
@@ -61,7 +49,6 @@ layout: default
-
{%- capture img_placehodler -%}
data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
{% endcapture%}
diff --git a/_plugins/posts-lastmod-hook.rb b/_plugins/posts-lastmod-hook.rb
new file mode 100644
index 0000000..5119b48
--- /dev/null
+++ b/_plugins/posts-lastmod-hook.rb
@@ -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
diff --git a/feed.xml b/feed.xml
index 30e01fc..0d88b56 100644
--- a/feed.xml
+++ b/feed.xml
@@ -30,10 +30,10 @@ layout: compress
{{ post.title }}
{{ post.date | date_to_xmlschema }}
- {% if post.seo.date_modified %}
-
{{ post.seo.date_modified | date_to_xmlschema }}
+ {% if post.lastmod %}
+
{{ post.lastmod | date_to_xmlschema }}
{% else %}
-
{{ site.time | date_to_xmlschema }}
+
{{ post.date | date_to_xmlschema }}
{% endif %}
{{ post_absolute_url }}
@@ -60,4 +60,4 @@ layout: compress
{% endfor %}
{% endcapture %}
-{{ source | replace: '&', '&' }}
\ No newline at end of file
+{{ source | replace: '&', '&' }}
diff --git a/sitemap.xml b/sitemap.xml
index 7b0bb70..1fb01fa 100644
--- a/sitemap.xml
+++ b/sitemap.xml
@@ -14,7 +14,11 @@ layout: compress
{% for post in site.posts %}
{{ site.url | append: site.baseurl | append: post.url }}
- {{ site.time | date_to_xmlschema }}
+ {% if post.lastmod %}
+ {{ post.lastmod | date_to_xmlschema }}
+ {% else %}
+ {{ post.date | date_to_xmlschema }}
+ {% endif %}
{% if post.sitemap.changefreq %}
{{ post.sitemap.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 %}
-
-
+
{{ site.url | append: site.baseurl | append: page.url | remove: "index.html" }}
- {{ lastmod | date_to_xmlschema }}
+ {{ site.time | date_to_xmlschema }}
{% if page.sitemap.changefreq %}
{{ page.sitemap.changefreq }}
@@ -88,4 +82,4 @@ layout: compress
{% endfor %}
-
\ No newline at end of file
+