updated判断

在hexo目录下创建脚本文件scripts/custom-index-generator.js
使用就是在文章头部加个updated字段就行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// scripts/custom-index-generator.js  
const pagination = require('hexo-pagination');

hexo.extend.generator.register('index', function(locals) {
const config = this.config;
const perPage = config.per_page || 10;
const paginationDir = config.pagination_dir || 'page';

// Deep copy posts to avoid modifying original data
const posts = locals.posts.slice();

// Debug raw posts data
console.log('Generator Raw Posts:', posts.data.map(post => ({
title: post.title || 'No Title',
date: post.date.format('YYYY-MM-DD'),
updated: post.updated ? post.updated.format('YYYY-MM-DD') : 'N/A',
use_updated: post.use_updated,
sort_time: (post.updated ? post.updated.format('YYYY-MM-DD') : post.date.format('YYYY-MM-DD'))
})));

// Sort posts with error handling
posts.data.sort((a, b) => {
// 自动检测:如果有 updated 字段且有效,就使用 updated 时间;否则使用 date 时间
const timeA = a.updated && a.updated.isValid()
? a.updated.unix()
: a.date.unix();
const timeB = b.updated && b.updated.isValid()
? b.updated.unix()
: b.date.unix();

console.log(`Generator Comparing sort_time: ${a.title || 'No Title'} (${timeA} = ${a.updated && a.updated.isValid() ? a.updated.format('YYYY-MM-DD') : a.date.format('YYYY-MM-DD')}) vs ${b.title || 'No Title'} (${timeB} = ${b.updated && b.updated.isValid() ? b.updated.format('YYYY-MM-DD') : b.date.format('YYYY-MM-DD')})`);

if (timeB !== timeA) {
return timeB - timeA;
}

const dateA = a.date.unix();
const dateB = b.date.unix();
console.log(`Generator Comparing date (sort_time equal): ${a.title || 'No Title'} (${dateA} = ${a.date.format('YYYY-MM-DD')}) vs ${b.title || 'No Title'} (${dateB} = ${b.date.format('YYYY-MM-DD')})`);
return dateB - dateA;
});

// Debug sorted order
console.log('Generator Sorted order:', posts.data.map(post => `${post.title || 'No Title'} (${(post.updated && post.updated.isValid() ? post.updated.format('YYYY-MM-DD') : post.date.format('YYYY-MM-DD'))})`).join(' -> '));

// Generate pages with pagination
return pagination('', posts, {
perPage: perPage,
layout: ['index', 'archive'],
format: paginationDir + '/%d/',
data: {
__index: true,
index_img: config.theme_config ? config.theme_config.index_img : '/img/2.webp' // Fallback to ensure index_img
}
});
});

修改主题渲染模板,themes/butterfly/layout/includes/mixins/indexPostUI.pug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//- themes/butterfly/layout/includes/mixins/indexPostUI.pug

//- Mixin for rendering the index post UI
mixin indexPostUI()
- const indexLayout = theme.index_layout
- const masonryLayoutClass = (indexLayout === 6 || indexLayout === 7) ? 'masonry' : ''
#recent-posts.recent-posts.nc(class=masonryLayoutClass)
.recent-post-items
// 调试代码:输出当前页面文章数据
- console.log('Page posts count:', page.posts.data.length)
- page.posts.data.forEach((article, index) => {
- console.log(`Page [${index}] Title: ${article.title}, use_updated: ${article.use_updated}, updated: ${article.updated ? article.updated.format('YYYY-MM-DD') : 'N/A'}, date: ${article.date.format('YYYY-MM-DD')}, sort_time: ${(article.use_updated === true || article.use_updated === 'true') && article.updated ? article.updated.format('YYYY-MM-DD') : article.date.format('YYYY-MM-DD')}`)
- })

// 调试当前页面顺序
- console.log('Page sorted order:', page.posts.data.map(article => `${article.title} (${((article.use_updated === true || article.use_updated === 'true') && article.updated ? article.updated.format('YYYY-MM-DD') : article.date.format('YYYY-MM-DD'))})`).join(' -> '))

// 遍历文章
each article, index in page.posts.data
.recent-post-item
- const link = article.link || article.path
- const title = article.title || _p('no_title')
- const leftOrRight = indexLayout === 3 ? (index % 2 === 0 ? 'left' : 'right') : (indexLayout === 2 ? 'right' : '')
- const post_cover = article.cover
- const no_cover = article.cover === false || !theme.cover.index_enable ? 'no-cover' : ''

// 文章封面
if post_cover && theme.cover.index_enable
.post_cover(class=leftOrRight)
a(href=url_for(link) title=title)
if article.cover_type === 'img'
img.post-bg(src=url_for(post_cover) onerror=`this.onerror=null;this.src='${url_for(theme.error_img.post_page)}'` alt=title)
else
div.post-bg(style=`background: ${post_cover}`)

// 文章信息
.recent-post-info(class=no_cover)
a.article-title(href=url_for(link) title=title)
if is_home() && (article.top || article.sticky > 0)
i.fas.fa-thumbtack.sticky
= title

// 文章元信息(日期、分类等)
.article-meta-wrap
if theme.post_meta.page.date_type
span.post-meta-date
if theme.post_meta.page.date_type === 'both'
i.far.fa-calendar-alt
span.article-meta-label=_p('post.created')
time.post-meta-date-created(datetime=date_xml(article.date) title=_p('post.created') + ' ' + full_date(article.date))= date(article.date, config.date_format)
span.article-meta-separator |
i.fas.fa-history
span.article-meta-label=_p('post.updated')
time.post-meta-date-updated(datetime=date_xml(article.updated) title=_p('post.updated') + ' ' + full_date(article.updated))= date(article.updated, config.date_format)
else
- const data_type_updated = theme.post_meta.page.date_type === 'updated'
- const date_type = data_type_updated ? 'updated' : 'date'
- const date_icon = data_type_updated ? 'fas fa-history' : 'far fa-calendar-alt'
- const date_title = data_type_updated ? _p('post.updated') : _p('post.created')
i(class=date_icon)
span.article-meta-label= date_title
time(datetime=date_xml(article[date_type]) title=date_title + ' ' + full_date(article[date_type]))= date(article[date_type], config.date_format)

// 分类信息
if theme.post_meta.page.categories && article.categories.data.length > 0
span.article-meta
span.article-meta-separator |
each item, index in article.categories.data
i.fas.fa-inbox
a(href=url_for(item.path)).article-meta__categories #[=item.name]
if index < article.categories.data.length - 1
i.fas.fa-angle-right.article-meta-link

// 标签信息
if theme.post_meta.page.tags && article.tags.length > 0
span.article-meta.tags
span.article-meta-separator |
each item, index in article.tags.data
i.fas.fa-tag
a(href=url_for(item.path)).article-meta__tags #[=item.name]
if index < article.tags.data.length - 1
span.article-meta-link #[='•']

// 评论数统计
mixin countBlockInIndex
- needLoadCountJs = true
span.article-meta
span.article-meta-separator |
i.fas.fa-comments
if block
block
span.article-meta-label= ' ' + _p('card_post_count')

if theme.comments.card_post_count && theme.comments.use
case theme.comments.use[0]
when 'Disqus'
when 'Disqusjs'
+countBlockInIndex
a.disqus-count(href=full_url_for(link) + '#post-comment')
i.fa-solid.fa-spinner.fa-spin
when 'Valine'
+countBlockInIndex
a(href=url_for(link) + '#post-comment')
span.valine-comment-count(data-xid=url_for(link))
i.fa-solid.fa-spinner.fa-spin
when 'Waline'
+countBlockInIndex
a(href=url_for(link) + '#post-comment')
span.waline-comment-count(data-path=url_for(link))
i.fa-solid.fa-spinner.fa-spin
when 'Twikoo'
+countBlockInIndex
a.twikoo-count(href=url_for(link) + '#post-comment')
i.fa-solid.fa-spinner.fa-spin
when 'Facebook Comments'
+countBlockInIndex
a(href=url_for(link) + '#post-comment')
span.fb-comments-count(data-href=urlNoIndex(article.permalink))
when 'Remark42'
+countBlockInIndex
a(href=url_for(link) + '#post-comment')
span.remark42__counter(data-url=urlNoIndex(article.permalink))
i.fa-solid.fa-spinner.fa-spin
when 'Artalk'
+countBlockInIndex
a(href=url_for(link) + '#post-comment')
span.artalk-count(data-page-key=url_for(link))
i.fa-solid.fa-spinner.fa-spin

// 文章摘要
- const content = postDesc(article)
if content
.content!=content

// 广告插入
if theme.ad && theme.ad.index
if (index + 1) % 3 === 0
.recent-post-item.ads-wrap!= theme.ad.index

// 分页组件
include ../pagination.pug

显示效果