Hexo NexT 主题配置

本文记录如何使用Hexo中的NexT主题来配置和美化自己的博客页面。


好用插件

评论系统

giscus

安装hexo-next-giscus

配置参考hexo-next & giscus 评论测试Hexo 评论系统(Giscus)

文章加密

安装hexo-blog-encrypt

配置参考NEXT文章加密 ,为hexo文章加密

书单页、影单页

安装hexo-douban,嵌入豆瓣个人主页。

注意:在hexo-theme-next主题中,需要将_config.next.yml中的pjax设置为false,否则书单页面无法正常渲染。

文章隐藏

hexo-hide-posts

我的 _config.yml 配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
hide_posts:
# 是否启用 hexo-hide-posts
enable: true
# 隐藏文章的 front-matter 标识,也可以改成其他你喜欢的名字
filter: hidden
# 为隐藏的文章添加 noindex meta 标签,阻止搜索引擎收录
noindex: false
# 设置白名单,白名单中的 generator 可以访问隐藏文章
# 常见的 generators 有:index, tag, category, archive, sitemap, feed, etc.
allowlist_generators: ['*']
# 设置黑名单,黑名单中的 generator 不可以访问隐藏文章
blocklist_generators: ['index', 'feed']

配置后首页sidebar日志数量显示的不包含隐藏的,archive页面日志数量和按年统计数量不匹配(都是不包含隐藏的)。

修改代码如下:

node_modules\hexo-theme-next\layout\_partials\sidebar\site-overview.njknode_modules\hexo-theme-next\layout\archive.njk中:

site.posts.length 修改为 (site.all_posts or site.posts).length

node_modules\hexo-theme-next\layout\_macro\post-collapse.njk中将 post_count(year) 函数替换为 all_posts_count_by_year(year),

并在在站点根目录新建文件:scripts/all-posts-helper.js,添加内容:

1
2
3
4
5
6
7
8
9
10
11
12
hexo.extend.helper.register('all_posts_count_by_year', function (year) {
const posts = this.site.all_posts || this.site.posts;
if (!posts) return 0;

// Hexo 的 posts 通常是 Warehouse Query,toArray() 更稳
const arr = typeof posts.toArray === 'function' ? posts.toArray() : posts;

return arr.filter(post => {
if (!post || !post.date) return false;
return post.date.format('YYYY') === String(year);
}).length;
});

后续更新NexT主题时可能会覆盖修改,需将其手动改回来。(目前没时间考虑 universality)