├── .eslintignore ├── .eslintrc ├── .gitignore ├── LICENSE ├── README.md ├── README_zh.md ├── index.js ├── lib └── generator.js └── package.json /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "hexo", 3 | "root": true 4 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | tmp/ 4 | *.log 5 | .idea/ 6 | coverage/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Tommy Chen 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hexo-generator-index-custom 2 | 3 | ![npm](https://img.shields.io/npm/v/hexo-generator-index-custom) 4 | 5 | [中文文档](./README_zh.md) 6 | 7 | Custom Index generator for [Hexo]. 8 | 9 | It generates an archive of posts on your homepage, according to the `index` or `archive` layout of your theme. 10 | 11 | ## Difference 12 | 13 | - pin top 14 | 15 | both the `sticky` and `top` parameters in the post Front-matter can be used to pin the post to the top of the index page. 16 | 17 | > because of in the old version, hexo-generator-index cannot pin the post to the top of the index page 18 | > 19 | > many people use [hexo-generator-index-pin-top](https://github.com/Qyh-Q/hexo-generator-index-pin-top) to do this 20 | 21 | - hide posts 22 | 23 | use `hide: true` in in the post Front-matter to hide the post from the index page 24 | 25 | 26 | ## Installation 27 | 28 | ``` bash 29 | $ npm uninstall hexo-generator-index 30 | 31 | $ npm install hexo-generator-index-custom --save 32 | ``` 33 | 34 | ## Options 35 | 36 | Add or modify the following section to your root _config.yml file 37 | 38 | ``` yaml 39 | index_generator: 40 | path: '' 41 | per_page: 10 42 | order_by: -date 43 | pagination_dir: page 44 | ``` 45 | 46 | - **path**: Root path for your blog's index page. 47 | - default: "" 48 | - **per_page**: Posts displayed per page. 49 | - default: [`config.per_page`](https://hexo.io/docs/configuration.html#Pagination) as specified in the official Hexo docs (if present), otherwise `10` 50 | - `0` disables pagination 51 | - **order_by**: Posts order. 52 | - default: date descending 53 | - **pagination_dir**: URL format. 54 | - default: 'page' 55 | - `awesome-page` makes the URL ends with 'awesome-page/' for second page and beyond. 56 | 57 | ## Usage 58 | 59 | The `sticky` or `top` parameter in the post [Front-matter](https://hexo.io/docs/front-matter) will be used to pin the post to the top of the index page. Higher `sticky` (or `top`) means that it will be ranked first. 60 | 61 | You can also use `sticky: true` or `top: true` to pin the post to the top of the index page. 62 | 63 | ```yml 64 | --- 65 | title: Hello World 66 | date: 2013/7/13 20:46:25 67 | sticky: 100 68 | --- 69 | ``` 70 | 71 | ```yml 72 | --- 73 | title: Hello World 74 | date: 2013/7/13 20:46:25 75 | top: 100 76 | --- 77 | ``` 78 | 79 | The `hide` parameter in the post Front-matter will be used to hide the post from the index page. 80 | 81 | ```yml 82 | --- 83 | title: Hello World 84 | date: 2013/7/13 20:46:25 85 | hide: true 86 | --- 87 | ``` 88 | 89 | ## Note 90 | 91 | If your theme define a non-archive `index` layout (e.g. About Me page), this plugin would follow that layout instead and not generate an archive. In that case, use [hexo-generator-archive](https://github.com/hexojs/hexo-generator-archive) to generate an archive according to the `archive` layout. 92 | 93 | ## License 94 | 95 | MIT 96 | 97 | [hexojs/Hexo](https://github.com/hexojs/hexo) 98 | 99 | [hexojs/hexo-generator-index](https://github.com/hexojs/hexo-generator-index) 100 | 101 | [Hexo]: http://hexo.io/ 102 | -------------------------------------------------------------------------------- /README_zh.md: -------------------------------------------------------------------------------- 1 | # hexo-generator-index-custom 2 | 3 | ![npm](https://img.shields.io/npm/v/hexo-generator-index-custom) 4 | 5 | [EN](./README.md) 6 | 7 | 支持置顶与隐藏文章的 [Hexo] 首页生成器插件。在官方的首页生成器的基础上添加了使用 `top` 置顶文章和 `hide` 隐藏文章的功能。 8 | 9 | 它会根据您的主题的 `index` 或 `archive` 格式生成首页。 10 | 11 | ## 不同之处 12 | 13 | - 置顶 14 | 15 | `sticky` 与 `top` 参数在文章的 [Front-matter](https://hexo.io/docs/front-matter) 中可以使用来置顶文章。高的 `sticky` (或 `top`) 值意味着它会被排到最前面。 16 | 17 | > 因为在旧版本中,hexo-generator-index 不能置顶文章到首页。 18 | > 19 | > 大多数人使用 [hexo-generator-index-pin-top](https://github.com/Qyh-Q/hexo-generator-index-pin-top) 来实现置顶功能 20 | 21 | 22 | - 隐藏文章 23 | 24 | 在文章的 Front-matter 中使用 `hide: true` 可以隐藏文章(不在首页显示)。 25 | 26 | > [hexo-generator-index-pin-top](https://github.com/Qyh-Q/hexo-generator-index-pin-top) 没有隐藏文章的功能 27 | 28 | 29 | ## 安装 30 | 31 | [hexo-generator-index-custom](https://github.com/im0o/hexo-generator-index-custom)可以完全替代官方的 [hexojs/hexo-generator-index](https://github.com/hexojs/hexo-generator-index),所以安装之后,先卸载官方的插件,不然会引起一些冲突。 32 | 33 | ``` bash 34 | $ npm uninstall hexo-generator-index 35 | 36 | $ npm install hexo-generator-index-custom --save 37 | ``` 38 | 39 | ## 配置 40 | 41 | 在 _config.yml 中新增或修改如下配置: 42 | 43 | ``` yaml 44 | index_generator: 45 | path: '' 46 | per_page: 10 47 | order_by: -date 48 | pagination_dir: page 49 | ``` 50 | 51 | - **path**: Root path for your blog's index page. 52 | - default: "" 53 | - **per_page**: Posts displayed per page. 54 | - default: [`config.per_page`](https://hexo.io/docs/configuration.html#Pagination) as specified in the official Hexo docs (if present), otherwise `10` 55 | - `0` disables pagination 56 | - **order_by**: Posts order. 57 | - default: date descending 58 | - **pagination_dir**: URL format. 59 | - default: 'page' 60 | - `awesome-page` makes the URL ends with 'awesome-page/' for second page and beyond. 61 | 62 | 所有的配置均与官方首页生成器一样,不必修改。 63 | 64 | ## 使用方法 65 | 66 | 在文章开头的配置中添加 `sticky` 或 `top` 参数,可以置顶文章。 67 | 68 | 其值可以是 `true` 或数字,数字越大,文章排在越前面。 69 | 70 | ```yml 71 | --- 72 | title: Hello World 73 | date: 2013/7/13 20:46:25 74 | sticky: 100 75 | --- 76 | ``` 77 | 78 | ```yml 79 | --- 80 | title: Hello World 81 | date: 2013/7/13 20:46:25 82 | top: 100 83 | --- 84 | ``` 85 | 86 | 在文章开头的配置中添加 `hide` 参数,可以隐藏文章。 87 | 88 | ```yml 89 | --- 90 | title: Hello World 91 | date: 2013/7/13 20:46:25 92 | hide: true 93 | --- 94 | ``` 95 | 96 | ## Note 97 | 98 | If your theme define a non-archive `index` layout (e.g. About Me page), this plugin would follow that layout instead and not generate an archive. In that case, use [hexo-generator-archive](https://github.com/hexojs/hexo-generator-archive) to generate an archive according to the `archive` layout. 99 | 100 | ## License 101 | 102 | MIT 103 | 104 | [hexojs/Hexo](https://github.com/hexojs/hexo) 105 | 106 | [hexojs/hexo-generator-index](https://github.com/hexojs/hexo-generator-index) 107 | 108 | [Hexo]: http://hexo.io/ 109 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* global hexo */ 2 | 3 | 'use strict'; 4 | 5 | hexo.config.index_generator = Object.assign({ 6 | per_page: typeof hexo.config.per_page === 'undefined' ? 10 : hexo.config.per_page, 7 | order_by: '-date' 8 | }, hexo.config.index_generator); 9 | 10 | hexo.extend.generator.register('index', require('./lib/generator')); 11 | -------------------------------------------------------------------------------- /lib/generator.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const pagination = require('hexo-pagination'); 4 | 5 | module.exports = function(locals) { 6 | const config = this.config; 7 | const posts = locals.posts 8 | .sort(config.index_generator.order_by) 9 | .filter(post => post.hide !== true); 10 | 11 | posts.data = posts.data.sort((a, b) => ((b.sticky || 0) + (b.top || 0)) - ((a.sticky || 0) + (a.top || 0))); 12 | 13 | const paginationDir = config.index_generator.pagination_dir || config.pagination_dir || 'page'; 14 | const path = config.index_generator.path || ''; 15 | 16 | return pagination(path, posts, { 17 | perPage: config.index_generator.per_page, 18 | layout: config.index_generator.layout || ['index', 'archive'], 19 | format: paginationDir + '/%d/', 20 | data: { 21 | __index: true 22 | } 23 | }); 24 | }; 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hexo-generator-index-custom", 3 | "version": "1.1.1", 4 | "description": " Custom Index generator for Hexo.", 5 | "main": "index", 6 | "scripts": { 7 | "eslint": "eslint .", 8 | "test": "mocha test/index.js", 9 | "test-cov": "c8 --reporter=lcovonly npm run test" 10 | }, 11 | "directories": { 12 | "lib": "./lib" 13 | }, 14 | "files": [ 15 | "index.js", 16 | "lib/" 17 | ], 18 | "engines": { 19 | "node": ">=14" 20 | }, 21 | "repository": "im0o/hexo-generator-index-custom", 22 | "homepage": "http://hexo.io/", 23 | "keywords": [ 24 | "hexo", 25 | "generator", 26 | "index", 27 | "home" 28 | ], 29 | "author": "Jz0ojiang (https://im0o.top)", 30 | "license": "MIT", 31 | "devDependencies": { 32 | "c8": "^7.12.0", 33 | "chai": "^4.3.6", 34 | "eslint": "^8.25.0", 35 | "eslint-config-hexo": "^5.0.0", 36 | "hexo": "^6.3.0", 37 | "mocha": "^10.0.0" 38 | }, 39 | "dependencies": { 40 | "hexo-pagination": "3.0.0" 41 | } 42 | } 43 | --------------------------------------------------------------------------------