Skip to content

Commit add6bf1

Browse files
committed
Add hook to generate the lastmod of posts
1 parent b3fc096 commit add6bf1

File tree

6 files changed

+43
-57
lines changed

6 files changed

+43
-57
lines changed

_includes/panel.html

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,25 @@
1010

1111
<div class="access">
1212

13-
{% if site.data.updates %}
14-
15-
{% include update-list.html %}
16-
17-
{% if update_list.size > 0 %}
18-
13+
{% include update-list.html %}
14+
15+
{% if update_list.size > 0 %}
16+
1917
<div id="access-lastmod" class="post">
2018
<span>{{- site.data.label.panel.lastmod -}}</span>
2119
<ul class="post-content pl-0 pb-1 ml-1 mt-2">
2220

2321
{% for item in update_list %}
24-
{% assign post_url = item | split: "::" | last | url_encode | prepend: "/posts/" | append: "/" %}
25-
{% assign post = site.posts | where: "url", post_url | first %}
26-
27-
{% if post %}
28-
{% assign url = post.url | relative_url %}
29-
<li><a href="{{ url }}">{{ post.title }}</a></li>
30-
{% endif %}
31-
22+
{% assign index = item | split: "::" | last | plus: 0 %}
23+
{% assign post = site.posts[index] %}
24+
{% assign url = post.url | relative_url %}
25+
<li><a href="{{ url }}">{{ post.title }}</a></li>
3226
{% endfor %}
3327

3428
</ul>
35-
</div><!-- #access-lastmod -->
36-
37-
{% endif %}
38-
39-
{% endif %} <!-- site.data.updates -->
29+
</div> <!-- #access-lastmod -->
30+
31+
{% endif %}
4032

4133
{% include trending-tags.html %}
4234

_includes/update-list.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
{% assign all_list = "" | split: "" %}
1212

13-
{% for entry in site.data.updates %}
14-
{% capture elem %}
15-
{{- entry.lastmod -}}::{{- entry.filename -}}
16-
{% endcapture %}
17-
{% assign all_list = all_list | push: elem %}
13+
{% for post in site.posts %}
14+
{% if post.lastmod %}
15+
{% capture elem %}
16+
{{- post.lastmod | date: "%Y%m%d%H%M%S" -}}::{{- forloop.index0 -}}
17+
{% endcapture %}
18+
{% assign all_list = all_list | push: elem %}
19+
{% endif %}
1820
{% endfor %}
1921

2022
{% assign all_list = all_list | sort | reverse %}

_layouts/post.html

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,10 @@ <h1 data-toc-skip>{{ page.title }}</h1>
3131
</div>
3232

3333
<!-- lastmod -->
34-
{%- capture filename -%}
35-
{{ page.url | split: "/" | last }}
36-
{%- endcapture -%}
37-
38-
{% for item in site.data.updates %}
39-
{% assign encode_filename = item.filename | url_encode %}
40-
{% if filename == encode_filename %}
41-
{% assign lastmod = item.lastmod %}
42-
{% break %}
43-
{% endif %}
44-
{% endfor %}
45-
46-
{% if lastmod %}
34+
{% if page.lastmod %}
4735
<div>
4836
Updated
49-
{% include timeago.html date=lastmod class="lastmod" tooltip=true %}
37+
{% include timeago.html date=page.lastmod class="lastmod" tooltip=true %}
5038
</div>
5139
{% endif %}
5240

@@ -61,7 +49,6 @@ <h1 data-toc-skip>{{ page.title }}</h1>
6149
</div> <!-- .post-meta -->
6250

6351
<div class="post-content">
64-
6552
{%- capture img_placehodler -%}
6653
data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
6754
{% endcapture%}

_plugins/posts-lastmod-hook.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Check for changed posts
2+
Jekyll::Hooks.register :posts, :post_init do |post|
3+
4+
commit_num = `git rev-list --count HEAD "#{ post.path }"`
5+
6+
if commit_num.to_i > 1
7+
lastmod_date = `git log -1 --pretty="%ad" --date=iso "#{ post.path }"`
8+
post.data['lastmod'] = lastmod_date
9+
end
10+
11+
end

feed.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ layout: compress
3030
<title>{{ post.title }}</title>
3131
<link href="{{ post_absolute_url }}" rel="alternate" type="text/html" title="{{ post.title }}" />
3232
<published>{{ post.date | date_to_xmlschema }}</published>
33-
{% if post.seo.date_modified %}
34-
<updated>{{ post.seo.date_modified | date_to_xmlschema }}</updated>
33+
{% if post.lastmod %}
34+
<updated>{{ post.lastmod | date_to_xmlschema }}</updated>
3535
{% else %}
36-
<updated>{{ site.time | date_to_xmlschema }}</updated>
36+
<updated>{{ post.date | date_to_xmlschema }}</updated>
3737
{% endif %}
3838
<id>{{ post_absolute_url }}</id>
3939
<content src="{{ post_absolute_url }}" />
@@ -60,4 +60,4 @@ layout: compress
6060
{% endfor %}
6161
</feed>
6262
{% endcapture %}
63-
{{ source | replace: '&', '&amp;' }}
63+
{{ source | replace: '&', '&amp;' }}

sitemap.xml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ layout: compress
1414
{% for post in site.posts %}
1515
<url>
1616
<loc>{{ site.url | append: site.baseurl | append: post.url }}</loc>
17-
<lastmod>{{ site.time | date_to_xmlschema }}</lastmod>
17+
{% if post.lastmod %}
18+
<lastmod>{{ post.lastmod | date_to_xmlschema }}</lastmod>
19+
{% else %}
20+
<lastmod>{{ post.date | date_to_xmlschema }}</lastmod>
21+
{% endif %}
1822

1923
{% if post.sitemap.changefreq %}
2024
<changefreq>{{ post.sitemap.changefreq }}</changefreq>
@@ -59,19 +63,9 @@ layout: compress
5963
{% continue %}
6064
{% endif %}
6165

62-
{% capture lastmod %}
63-
{% if page.lastmod %}
64-
{{ page.lastmod }}
65-
{% elsif page.date %}
66-
{{ page.date }}
67-
{% else %}
68-
{{ site.time }}
69-
{% endif %}
70-
{% endcapture %}
71-
72-
<url>
66+
<url>
7367
<loc>{{ site.url | append: site.baseurl | append: page.url | remove: "index.html" }}</loc>
74-
<lastmod>{{ lastmod | date_to_xmlschema }}</lastmod>
68+
<lastmod>{{ site.time | date_to_xmlschema }}</lastmod>
7569

7670
{% if page.sitemap.changefreq %}
7771
<changefreq>{{ page.sitemap.changefreq }}</changefreq>
@@ -88,4 +82,4 @@ layout: compress
8882

8983
{% endfor %}
9084

91-
</urlset>
85+
</urlset>

0 commit comments

Comments
 (0)