pure主题增加文章更新时间

当给文章进行更新,却没有更新提示,这怎么能行呢,于是,这篇文章出现了。

浅浅声明一下

这篇文章,是借鉴了Hexo博客Hiker主题增加文章最后编辑时间并按照最后编辑时间排序。在此非常感谢Vientiane提供的详细而清晰的修改教程,因为每个人的需求不同,进行了相应的修改,如果想要观看原文,或者更多的配置,请移步上面的原文链接。

由于 Hiker 主题和 Pure 主题的 layout 结构是类似的,所以我们可以从 Vientiane 的修改方式中,发现适合我们 pure 主题配置的具体修改方式。

调整 Hexo 主配置文件

我们找到主配置文件 _config.yml(注意:不是主题的 _config.yml),修改 index_generator 的 order_by 为 -updated 就可以开启以更新时间来排序,就会将最后修改过的文章显示在最前面。

index_generator:
path: ''
per_page: 10
order_by: -updated # -date、-updated 以上传时间或更新时间排序

然后继续修改 date_format 部分,将 YYYY-MM-DD 改成如下的 YYYY-MM-DD HH:mm:ss格式,再重新生成一下网页就能看到具体的时间了。

date_format: YYYY-MM-DD HH:mm:ss
time_format: HH:mm:ss
## updated_option supports 'mtime', 'date', 'empty'
updated_option: 'mtime'

新建 updated.ejs 文件

themes\pure\layout\_partial\post 中的 date.ejs 这个文件中添加一些东西。

内容如下:

<!-- 上传时间 -->
<span class="article-date">
<i class="icon icon-calendar-check"></i>
<a href="<%- url_for(post.path) %>" class="<%= class_name %>">
<%= __('published') %>
<time datetime="<%= date_xml(post.date) %>" itemprop="datePublished">
<%= date(post.date, date_format) %>
</time>
</a>
</span>

<!-- 更新时间 -->
<span class="article-date">
<i class="icon icon-calendar-check"></i>
<a href="<%- url_for(post.path) %>" class="<%= class_name %>">
<%= __('updated') %>
<time datetime="<%= date_xml(post.updated) %>" itemprop="dateUpdated">
<%= date(post.updated, date_format) %>
</time>
</a>
</span>

大体上与 date.ejs 内容相仿,post.date 改成了 post.updated,itemprop=”datePublished” 改成了 itemprop="dateUpdated"。同时还多了一个 <%= __('updated') %>,因为在原来的格式下只会显示时间,而这两个时间没有任何的提示信息,就会不知道哪个是哪个,所以为了用户体验,我们需要加上一些明显的提示信息。

同时在 上传时间 中也多了个 <%= __('published') %>,这是为了配合 更新时间,而添加的提示性文本信息。

配置提示文本

由于 date.ejs 中分别引入了 published 和 updated 字段,因此我们需要去在语言文件中新增对应的字段,语言文件在 themes/pure/languages中,你博客设置成什么语言,就修改对应的语言文件,当然全部更改也是可以的,这样就方便后面的语言切换。

打开文件,在文件中增加两行:(我是添加到文章最后的)

copyright:
theme_by: Theme by
base_on: base on
powered_by: Powered by
# 这个是给时间前面设置个醒目文本
published: 首次
updated: 更新

保存退出即可。简单解释一下,格式为字段名:字段值,字段名不变,字段值可以随意更改。

修改 archive-post.ejs 文件

打开 themes\pure\layout\_partial\archive-post.ejs 文件,找到: <%- partial('post/date', {class_name: 'article-date', date_format: 'MMM D'}) %>,把其中的 date_format 改为 null,保存退出即可。这里是为了防止首页」和「文章页」时间格式不一致的问题。

<p class="article-meta">
<!-- 上传时间 -->
<%- partial('post/date', {class_name: 'article-date', date_format: null}) %>
<%- partial('post/category') %>
<%- partial('post/tag') %>
<span class="post-comment"><i class="icon icon-comment"></i> <a href="<%- url_for(post.path) %>#comments"
class="article-comment-link"><%= __('article.comments') %></a></span>
<%- partial('post/wordcount') %>
</p>

配置 .md 文件

date: {date} 
updated: {date}

结束$效果展示

然后你就可以:hexo cleanhexo ghexo s看看效果了。

首页样式

文章页样式