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 @@
- {% if site.data.updates %} - - {% include update-list.html %} - - {% if update_list.size > 0 %} - + {% include update-list.html %} + + {% if update_list.size > 0 %} +
{{- site.data.label.panel.lastmod -}} -
- - {% endif %} - - {% endif %} +
+ + {% endif %} {% include trending-tags.html %} diff --git a/_includes/update-list.html b/_includes/update-list.html index 231d9a0..b0eb52a 100644 --- a/_includes/update-list.html +++ b/_includes/update-list.html @@ -10,11 +10,13 @@ {% assign all_list = "" | split: "" %} -{% for entry in site.data.updates %} - {% capture elem %} - {{- entry.lastmod -}}::{{- entry.filename -}} - {% endcapture %} - {% assign all_list = all_list | push: elem %} +{% for post in site.posts %} + {% if post.lastmod %} + {% capture elem %} + {{- 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 %} diff --git a/_layouts/post.html b/_layouts/post.html index 94ea43a..cee27cc 100644 --- a/_layouts/post.html +++ b/_layouts/post.html @@ -31,22 +31,10 @@ layout: default - {%- 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 %}
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 +