# hexo-generator-index-custom

在 Hexo 官方首页生成器上，补上我需要的置顶和隐藏规则

Source: https://works.im0o.top/projects/7a96720e/
Date: 2022-08-21
Status: archived
Tags: 开发工具, JavaScript, Hexo, npm

## Project links

- [源代码](https://github.com/im0o/hexo-generator-index-custom)
- [npm](https://www.npmjs.com/package/hexo-generator-index-custom)


改 Hexo 博客时，我既想把文章置顶，也想让某些文章不出现在首页。找过的插件各有一部分：有的支持置顶，有的按标签隐藏，直接在主题里隐藏又会留下分页数量的问题。最后我从官方首页生成器 fork 了一份，补上自己要用的规则，发布成 hexo-generator-index-custom。

## 首页文章的顺序

插件读取文章 frontmatter 里的 top、sticky 和 hide。先按配置的 order_by 排序、去掉 hide 为 true 的文章，再整理置顶顺序，最后交给 hexo-pagination 分页。

这一版把 top 与 sticky 相加作为置顶值，数值越大越靠前：

~~~js
posts.data = posts.data.sort(
  (a, b) =>
    ((b.sticky || 0) + (b.top || 0)) -
    ((a.sticky || 0) + (a.top || 0))
);
~~~

因此，原来用 top 的文章和用 sticky 的文章都能参与排序；只想隐藏一篇文章时，写 hide: true 即可。

~~~yaml
---
title: 一篇需要置顶的文章
top: 10
---
~~~

![top 与 sticky 相加，按合计值整理置顶顺序](https://image.im0o.top/2026/20261117104205620.webp "top 与 sticky 相加，按合计值整理置顶顺序")

## 隐藏发生在分页之前

在主题模板里判断是否显示文章，文章已经参与了分页。隐藏多篇后，首页实际能看到的条目就会减少。

我把过滤放到生成器中，分页接收到的就是过滤后的列表。我还在 v1.0.1 修过尾页数量的问题，在开发记录里补了句“光速修复”。

插件继续使用官方生成器的 path、per_page、order_by 和 pagination_dir 等配置，接入点也是 Hexo 的 index generator。安装时替换原来的首页生成器，避免两个插件同时生成首页。

[源代码](https://github.com/im0o/hexo-generator-index-custom) · [npm](https://www.npmjs.com/package/hexo-generator-index-custom) · [开发记录](https://blog.im0o.top/posts/3ff4be18.html)

![先过滤隐藏文章，再把剩下的文章交给分页](https://image.im0o.top/2026/20261117104213165.webp "先过滤隐藏文章，再把剩下的文章交给分页")

