hexojs / hexo-generator-index

Index generator plugin for Hexo.
MIT License
53 stars 45 forks source link

Add a feature to exclude articles based on categories in paging #60

Closed wellmoonloft closed 3 years ago

wellmoonloft commented 3 years ago

_config.yml

index_generator:
  path: ''
  per_page: 10
  order_by: -date
  exclude: true  //set true if you wanna exclude
  exclude_list:
    - novel   //set categories name you wanna exclude
    - gossip    //set categories name you wanna exclude

generator.js

'use strict';

const pagination = require('hexo-pagination');
const { sort } = require('timsort');

module.exports = function(locals) {
  const config = this.config;
  const posts = locals.posts.sort(config.index_generator.order_by);
  const temp_posts = [];

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

  const paginationDir = config.pagination_dir || 'page';
  const path = config.index_generator.path || '';

  if(config.index_generator.exclude){

    posts.forEach(element => {
      var mark=true; 
      element.categories.forEach(function(categories){ 
        config.index_generator.exclude_list.forEach(function(category){
          if(categories.name == category){
            mark=false; 
          }
        });
      }); 
      if(mark){
        temp_posts.push(element);
      }    
    });
  }

  return pagination(path, config.index_generator.exclude ? temp_posts : posts, {
    perPage: config.index_generator.per_page,
    layout: ['index', 'archive'],
    format: paginationDir + '/%d/',
    data: {
      __index: true
    }
  });
};
wellmoonloft commented 3 years ago

I switch to automatic deployment, so I don’t need to modify the main program anymore, thanks