├── .eslintignore ├── .eslintrc ├── .github ├── dependabot.yml └── workflows │ └── tester.yml ├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── lib └── generator.js ├── package.json └── test ├── .eslintrc ├── .mocharc.yml └── index.js /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "hexo", 3 | "root": true 4 | } -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | - package-ecosystem: github-actions 8 | directory: "/" 9 | schedule: 10 | interval: daily -------------------------------------------------------------------------------- /.github/workflows/tester.yml: -------------------------------------------------------------------------------- 1 | name: Tester 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | tester: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | matrix: 10 | os: [ubuntu-latest, windows-latest, macos-latest] 11 | node-version: ['14.x', '16.x', '18.x'] 12 | fail-fast: false 13 | steps: 14 | - uses: actions/checkout@v4 15 | - name: Use Node.js ${{ matrix.node-version }} 16 | uses: actions/setup-node@v4 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - name: Install Dependencies 20 | run: npm install 21 | - name: Test 22 | run: npm test -- --no-parallel 23 | env: 24 | CI: true 25 | coverage: 26 | runs-on: ${{ matrix.os }} 27 | strategy: 28 | matrix: 29 | os: [ubuntu-latest] 30 | node-version: ['14.x'] 31 | steps: 32 | - uses: actions/checkout@v4 33 | - name: Use Node.js ${{ matrix.node-version }} 34 | uses: actions/setup-node@v4 35 | with: 36 | node-version: ${{ matrix.node-version }} 37 | - name: Install Dependencies 38 | run: npm install 39 | - name: Coverage 40 | run: npm run test-cov 41 | env: 42 | CI: true 43 | - name: Coveralls 44 | uses: coverallsapp/github-action@master 45 | with: 46 | github-token: ${{ secrets.github_token }} -------------------------------------------------------------------------------- /.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-tag 2 | 3 | [![Build Status](https://github.com/hexojs/hexo-generator-tag/workflows/Tester/badge.svg)](https://github.com/hexojs/hexo-generator-tag/actions?query=workflow%3ATester) 4 | [![NPM version](https://badge.fury.io/js/hexo-generator-tag.svg)](https://www.npmjs.com/package/hexo-generator-tag) 5 | [![Coverage Status](https://img.shields.io/coveralls/hexojs/hexo-generator-tag.svg)](https://coveralls.io/r/hexojs/hexo-generator-tag?branch=master) 6 | 7 | Tag generator for [Hexo]. 8 | 9 | ## Installation 10 | 11 | ``` bash 12 | $ npm install hexo-generator-tag --save 13 | ``` 14 | 15 | ## Options 16 | 17 | ``` yaml 18 | tag_generator: 19 | per_page: 10 20 | order_by: -date 21 | ``` 22 | 23 | - **per_page**: Posts displayed per page. (0 = disable pagination) 24 | - **order_by**: Posts order. (Order by date descending by default) 25 | - **enable_index_page**: Generate a `/[config.tag_dir]/index.html` page. 26 | - Support following template layout: `tag-index`, `tag`, `archive`, `index` 27 | 28 | ## License 29 | 30 | MIT 31 | 32 | [Hexo]: https://hexo.io/ 33 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* global hexo */ 2 | 'use strict'; 3 | 4 | hexo.config.tag_generator = Object.assign({ 5 | per_page: hexo.config.per_page == null ? 10 : hexo.config.per_page 6 | }, hexo.config.tag_generator); 7 | 8 | hexo.extend.generator.register('tag', require('./lib/generator')); 9 | -------------------------------------------------------------------------------- /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 perPage = config.tag_generator.per_page; 8 | const paginationDir = config.pagination_dir || 'page'; 9 | const orderBy = config.tag_generator.order_by || '-date'; 10 | const tags = locals.tags; 11 | let tagDir; 12 | 13 | const pages = tags.reduce((result, tag) => { 14 | if (!tag.length) return result; 15 | 16 | const posts = tag.posts.sort(orderBy); 17 | const data = pagination(tag.path, posts, { 18 | perPage: perPage, 19 | layout: ['tag', 'archive', 'index'], 20 | format: paginationDir + '/%d/', 21 | data: { 22 | tag: tag.name 23 | } 24 | }); 25 | 26 | return result.concat(data); 27 | }, []); 28 | 29 | // generate tag index page, usually /tags/index.html 30 | if (config.tag_generator.enable_index_page) { 31 | tagDir = config.tag_dir; 32 | if (tagDir[tagDir.length - 1] !== '/') { 33 | tagDir += '/'; 34 | } 35 | 36 | pages.push({ 37 | path: tagDir, 38 | layout: ['tag-index', 'tag', 'archive', 'index'], 39 | posts: locals.posts, 40 | data: { 41 | base: tagDir, 42 | total: 1, 43 | current: 1, 44 | current_url: tagDir, 45 | posts: locals.posts, 46 | prev: 0, 47 | prev_link: '', 48 | next: 0, 49 | next_link: '', 50 | tags: tags 51 | } 52 | }); 53 | } 54 | 55 | return pages; 56 | }; 57 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hexo-generator-tag", 3 | "version": "2.0.0", 4 | "description": "Tag 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 | "repository": "hexojs/hexo-generator-tag", 19 | "homepage": "https://hexo.io/", 20 | "keywords": [ 21 | "hexo", 22 | "generator", 23 | "tag" 24 | ], 25 | "author": "Tommy Chen (https://zespia.tw)", 26 | "license": "MIT", 27 | "devDependencies": { 28 | "c8": "^9.0.0", 29 | "chai": "^4.3.6", 30 | "eslint": "^9.0.0", 31 | "eslint-config-hexo": "^5.0.0", 32 | "hexo": "^7.0.0", 33 | "mocha": "^10.0.0" 34 | }, 35 | "dependencies": { 36 | "hexo-pagination": "3.0.0" 37 | }, 38 | "engines": { 39 | "node": ">=14" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "hexo/test" 3 | } -------------------------------------------------------------------------------- /test/.mocharc.yml: -------------------------------------------------------------------------------- 1 | reporter: spec 2 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const should = require('chai').should(); // eslint-disable-line 4 | const Hexo = require('hexo'); 5 | 6 | describe('Tag generator', () => { 7 | const hexo = new Hexo(__dirname, {silent: true}); 8 | hexo.init(); 9 | const Post = hexo.model('Post'); 10 | const generator = require('../lib/generator').bind(hexo); 11 | let posts = {}; 12 | let locals = {}; 13 | 14 | // Default config 15 | hexo.config.tag_generator = { 16 | per_page: 10 17 | }; 18 | 19 | before(() => { 20 | return Post.insert([ 21 | {source: 'foo', slug: 'foo', date: 1e8}, 22 | {source: 'bar', slug: 'bar', date: 1e8 + 1}, 23 | {source: 'baz', slug: 'baz', date: 1e8 - 1}, 24 | {source: 'boo', slug: 'boo', date: 1e8 + 2} 25 | ]).then(data => { 26 | posts = data; 27 | 28 | return posts[0].setTags(['foo']).then(() => { 29 | return posts[1].setTags(['bar']); 30 | }).then(() => { 31 | return posts[2].setTags(['foo']); 32 | }).then(() => { 33 | return posts[3].setTags(['foo']); 34 | }); 35 | }).then(() => { 36 | locals = hexo.locals.toObject(); 37 | }); 38 | }); 39 | 40 | describe('Disable index page', () => { 41 | it('pagination enabled', () => { 42 | hexo.config.tag_generator.per_page = 2; 43 | 44 | const result = generator(locals); 45 | 46 | result.length.should.eql(3); 47 | 48 | for (let i = 0, len = result.length; i < len; i++) { 49 | result[i].layout.should.eql(['tag', 'archive', 'index']); 50 | } 51 | 52 | result[0].path.should.eql('tags/foo/'); 53 | result[0].data.base.should.eql('tags/foo/'); 54 | result[0].data.total.should.eql(2); 55 | result[0].data.current.should.eql(1); 56 | result[0].data.current_url.should.eql('tags/foo/'); 57 | result[0].data.posts.eq(0)._id.should.eql(posts[3]._id); 58 | result[0].data.posts.eq(1)._id.should.eql(posts[0]._id); 59 | result[0].data.prev.should.eql(0); 60 | result[0].data.prev_link.should.eql(''); 61 | result[0].data.next.should.eql(2); 62 | result[0].data.next_link.should.eql('tags/foo/page/2/'); 63 | result[0].data.tag.should.eql('foo'); 64 | 65 | result[1].path.should.eql('tags/foo/page/2/'); 66 | result[1].data.base.should.eql('tags/foo/'); 67 | result[1].data.total.should.eql(2); 68 | result[1].data.current.should.eql(2); 69 | result[1].data.current_url.should.eql('tags/foo/page/2/'); 70 | result[1].data.posts.eq(0)._id.should.eql(posts[2]._id); 71 | result[1].data.prev.should.eql(1); 72 | result[1].data.prev_link.should.eql('tags/foo/'); 73 | result[1].data.next.should.eql(0); 74 | result[1].data.next_link.should.eql(''); 75 | result[1].data.tag.should.eql('foo'); 76 | 77 | result[2].path.should.eql('tags/bar/'); 78 | result[2].data.base.should.eql('tags/bar/'); 79 | result[2].data.total.should.eql(1); 80 | result[2].data.current.should.eql(1); 81 | result[2].data.current_url.should.eql('tags/bar/'); 82 | result[2].data.posts.eq(0)._id.should.eql(posts[1]._id); 83 | result[2].data.prev.should.eql(0); 84 | result[2].data.prev_link.should.eql(''); 85 | result[2].data.next.should.eql(0); 86 | result[2].data.next_link.should.eql(''); 87 | result[2].data.tag.should.eql('bar'); 88 | 89 | // Restore config 90 | hexo.config.tag_generator.per_page = 10; 91 | }); 92 | 93 | it('pagination disabled', () => { 94 | hexo.config.tag_generator.per_page = 0; 95 | 96 | const result = generator(locals); 97 | 98 | result.length.should.eql(2); 99 | 100 | for (let i = 0, len = result.length; i < len; i++) { 101 | result[i].layout.should.eql(['tag', 'archive', 'index']); 102 | } 103 | 104 | result[0].path.should.eql('tags/foo/'); 105 | result[0].data.base.should.eql('tags/foo/'); 106 | result[0].data.total.should.eql(1); 107 | result[0].data.current.should.eql(1); 108 | result[0].data.current_url.should.eql('tags/foo/'); 109 | result[0].data.posts.eq(0)._id.should.eql(posts[3]._id); 110 | result[0].data.posts.eq(1)._id.should.eql(posts[0]._id); 111 | result[0].data.posts.eq(2)._id.should.eql(posts[2]._id); 112 | result[0].data.prev.should.eql(0); 113 | result[0].data.prev_link.should.eql(''); 114 | result[0].data.next.should.eql(0); 115 | result[0].data.next_link.should.eql(''); 116 | result[0].data.tag.should.eql('foo'); 117 | 118 | result[1].path.should.eql('tags/bar/'); 119 | result[1].data.base.should.eql('tags/bar/'); 120 | result[1].data.total.should.eql(1); 121 | result[1].data.current.should.eql(1); 122 | result[1].data.current_url.should.eql('tags/bar/'); 123 | result[1].data.posts.eq(0)._id.should.eql(posts[1]._id); 124 | result[1].data.prev.should.eql(0); 125 | result[1].data.prev_link.should.eql(''); 126 | result[1].data.next.should.eql(0); 127 | result[1].data.next_link.should.eql(''); 128 | result[1].data.tag.should.eql('bar'); 129 | 130 | // Restore config 131 | hexo.config.tag_generator.per_page = 10; 132 | }); 133 | 134 | it('custom pagination_dir', () => { 135 | hexo.config.tag_generator.per_page = 2; 136 | hexo.config.pagination_dir = 'yo'; 137 | 138 | const result = generator(locals); 139 | 140 | result.map(item => { 141 | return item.path; 142 | }).should.eql(['tags/foo/', 'tags/foo/yo/2/', 'tags/bar/']); 143 | 144 | // Restore config 145 | hexo.config.tag_generator.per_page = 10; 146 | hexo.config.pagination_dir = 'page'; 147 | }); 148 | }); 149 | 150 | describe('Enable index page', () => { 151 | it('pagination enabled', () => { 152 | hexo.config.tag_generator.per_page = 2; 153 | hexo.config.tag_generator.enable_index_page = true; 154 | 155 | const result = generator(locals); 156 | 157 | result.length.should.eql(4); 158 | 159 | for (let i = 0, len = result.length - 1; i < len; i++) { 160 | result[i].layout.should.eql(['tag', 'archive', 'index']); 161 | } 162 | 163 | result[3].layout.should.eql(['tag-index', 'tag', 'archive', 'index']); 164 | 165 | result[0].path.should.eql('tags/foo/'); 166 | result[0].data.base.should.eql('tags/foo/'); 167 | result[0].data.total.should.eql(2); 168 | result[0].data.current.should.eql(1); 169 | result[0].data.current_url.should.eql('tags/foo/'); 170 | result[0].data.posts.eq(0)._id.should.eql(posts[3]._id); 171 | result[0].data.posts.eq(1)._id.should.eql(posts[0]._id); 172 | result[0].data.prev.should.eql(0); 173 | result[0].data.prev_link.should.eql(''); 174 | result[0].data.next.should.eql(2); 175 | result[0].data.next_link.should.eql('tags/foo/page/2/'); 176 | result[0].data.tag.should.eql('foo'); 177 | 178 | result[1].path.should.eql('tags/foo/page/2/'); 179 | result[1].data.base.should.eql('tags/foo/'); 180 | result[1].data.total.should.eql(2); 181 | result[1].data.current.should.eql(2); 182 | result[1].data.current_url.should.eql('tags/foo/page/2/'); 183 | result[1].data.posts.eq(0)._id.should.eql(posts[2]._id); 184 | result[1].data.prev.should.eql(1); 185 | result[1].data.prev_link.should.eql('tags/foo/'); 186 | result[1].data.next.should.eql(0); 187 | result[1].data.next_link.should.eql(''); 188 | result[1].data.tag.should.eql('foo'); 189 | 190 | result[2].path.should.eql('tags/bar/'); 191 | result[2].data.base.should.eql('tags/bar/'); 192 | result[2].data.total.should.eql(1); 193 | result[2].data.current.should.eql(1); 194 | result[2].data.current_url.should.eql('tags/bar/'); 195 | result[2].data.posts.eq(0)._id.should.eql(posts[1]._id); 196 | result[2].data.prev.should.eql(0); 197 | result[2].data.prev_link.should.eql(''); 198 | result[2].data.next.should.eql(0); 199 | result[2].data.next_link.should.eql(''); 200 | result[2].data.tag.should.eql('bar'); 201 | 202 | result[3].path.should.eql('tags/'); 203 | result[3].data.base.should.eql('tags/'); 204 | result[3].data.total.should.eql(1); 205 | result[3].data.current.should.eql(1); 206 | result[3].data.current_url.should.eql('tags/'); 207 | // just all posts 208 | result[3].data.posts.should.eql(locals.posts); 209 | result[3].data.prev.should.eql(0); 210 | result[3].data.prev_link.should.eql(''); 211 | result[3].data.next.should.eql(0); 212 | result[3].data.next_link.should.eql(''); 213 | // no tag, tags instead 214 | (result[3].data.tag === undefined).should.be.true; 215 | result[3].data.tags.should.eql(locals.tags); 216 | 217 | // Restore config 218 | hexo.config.tag_generator.per_page = 10; 219 | }); 220 | 221 | it('pagination disabled', () => { 222 | hexo.config.tag_generator.per_page = 0; 223 | hexo.config.tag_generator.enable_index_page = true; 224 | 225 | const result = generator(locals); 226 | 227 | result.length.should.eql(3); 228 | 229 | for (let i = 0, len = result.length - 1; i < len; i++) { 230 | result[i].layout.should.eql(['tag', 'archive', 'index']); 231 | } 232 | 233 | result[2].layout.should.eql(['tag-index', 'tag', 'archive', 'index']); 234 | 235 | result[0].path.should.eql('tags/foo/'); 236 | result[0].data.base.should.eql('tags/foo/'); 237 | result[0].data.total.should.eql(1); 238 | result[0].data.current.should.eql(1); 239 | result[0].data.current_url.should.eql('tags/foo/'); 240 | result[0].data.posts.eq(0)._id.should.eql(posts[3]._id); 241 | result[0].data.posts.eq(1)._id.should.eql(posts[0]._id); 242 | result[0].data.posts.eq(2)._id.should.eql(posts[2]._id); 243 | result[0].data.prev.should.eql(0); 244 | result[0].data.prev_link.should.eql(''); 245 | result[0].data.next.should.eql(0); 246 | result[0].data.next_link.should.eql(''); 247 | result[0].data.tag.should.eql('foo'); 248 | 249 | result[1].path.should.eql('tags/bar/'); 250 | result[1].data.base.should.eql('tags/bar/'); 251 | result[1].data.total.should.eql(1); 252 | result[1].data.current.should.eql(1); 253 | result[1].data.current_url.should.eql('tags/bar/'); 254 | result[1].data.posts.eq(0)._id.should.eql(posts[1]._id); 255 | result[1].data.prev.should.eql(0); 256 | result[1].data.prev_link.should.eql(''); 257 | result[1].data.next.should.eql(0); 258 | result[1].data.next_link.should.eql(''); 259 | result[1].data.tag.should.eql('bar'); 260 | 261 | result[2].path.should.eql('tags/'); 262 | result[2].data.base.should.eql('tags/'); 263 | result[2].data.total.should.eql(1); 264 | result[2].data.current.should.eql(1); 265 | result[2].data.current_url.should.eql('tags/'); 266 | result[2].data.posts.should.eql(locals.posts); 267 | result[2].data.prev.should.eql(0); 268 | result[2].data.prev_link.should.eql(''); 269 | result[2].data.next.should.eql(0); 270 | result[2].data.next_link.should.eql(''); 271 | (result[2].data.tag === undefined).should.be.true; 272 | result[2].data.tags.should.eql(locals.tags); 273 | 274 | // Restore config 275 | hexo.config.tag_generator.per_page = 10; 276 | }); 277 | 278 | it('custom pagination_dir', () => { 279 | hexo.config.tag_generator.per_page = 2; 280 | hexo.config.pagination_dir = 'yo'; 281 | hexo.config.tag_generator.enable_index_page = true; 282 | 283 | const result = generator(locals); 284 | 285 | result.map(item => { 286 | return item.path; 287 | }).should.eql(['tags/foo/', 'tags/foo/yo/2/', 'tags/bar/', 'tags/']); 288 | 289 | // Restore config 290 | hexo.config.tag_generator.per_page = 10; 291 | hexo.config.pagination_dir = 'page'; 292 | }); 293 | }); 294 | }); 295 | --------------------------------------------------------------------------------