Skip to content

Commit 2ba0ee7

Browse files
authored
Merge pull request #1680 from future-architect/feature
数年以内に投稿実績がある著者にマークを付ける
2 parents b11bf46 + e8c650f commit 2ba0ee7

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

scripts/author_generator.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,61 @@ function author_to_url(author) {
5555
return ((this.config.author_generator || {}).url_map || {})[author] || author;
5656
}
5757

58-
hexo.extend.helper.register('list_authors', function(year='all') {
58+
hexo.extend.helper.register('list_authors', function(year = 'all') {
59+
// 年指定、または全期間での投稿数をカウントする関数を定義
5960
let count_posts = author => this.site.posts.filter(post => post.author === author).length;
6061
if (year != 'all') {
6162
count_posts = author => this.site.posts.filter(post => post.date.format("YYYY") === year && post.author === author).length;
6263
}
6364

65+
// 投稿数で著者をソート
6466
const compareFunc = (a, b) => count_posts(b) - count_posts(a);
65-
const postRankings = this.site.authors.filter(author => !Array.isArray(author)).sort(compareFunc)
66-
const authors = postRankings.filter(author => count_posts(author) > 0).map(author => `
67-
<li class="author-list-item">
68-
<a class="author-list-link" href="/authors/${author_to_url.call(this, author)}">${author}</a>
67+
const postRankings = this.site.authors.filter(author => !Array.isArray(author)).sort(compareFunc);
68+
69+
// authorMapperを定義。yearの値によって処理を分岐する
70+
let authorMapper;
71+
72+
if (year === 'all') {
73+
// 全期間表示の場合のロジック
74+
const currentYear = new Date().getFullYear(); // 今年 (2025)
75+
const lastYear = currentYear - 1; // 昨年 (2024)
76+
const twoYearsAgo = currentYear - 2; // 2年前 (2023)
77+
78+
// 特定の年に著者の投稿があるかチェックするヘルパー
79+
const hasPostsInYear = (author, checkYear) =>
80+
this.site.posts.some(post => post.date.year() === checkYear && post.author === author);
81+
82+
authorMapper = author => {
83+
let suffix = '';
84+
const hasNoPostsThisYear = !hasPostsInYear(author, currentYear);
85+
86+
// 今年の投稿実績がない著者のみを対象にサフィックスを判定
87+
if (hasNoPostsThisYear) {
88+
// 条件(拡張): 昨年実績があるか -> '**'を付与
89+
if (hasPostsInYear(author, lastYear)) {
90+
suffix = '**';
91+
// 条件: 2年前に実績があるか -> '*'を付与
92+
} else if (hasPostsInYear(author, twoYearsAgo)) {
93+
suffix = '*';
94+
}
95+
}
96+
97+
return `
98+
<li class="author-list-item">
99+
<a class="author-list-link" href="/authors/${author_to_url.call(this, author)}">${author}${suffix}</a>
69100
<span class="author-list-count">${count_posts(author)} 件</span>
70-
</li>`).join('');
101+
</li>`;
102+
};
103+
} else {
104+
// 年指定の場合のロジック (従来通り)
105+
authorMapper = author => `
106+
<li class="author-list-item">
107+
<a class="author-list-link" href="/authors/${author_to_url.call(this, author)}">${author}</a>
108+
<span class="author-list-count">${count_posts(author)} 件</span>
109+
</li>`;
110+
}
111+
112+
const authors = postRankings.filter(author => count_posts(author) > 0).map(authorMapper).join('');
71113

72114
return `<ul class="author-list">${authors}</ul>`;
73115
});

0 commit comments

Comments
 (0)