Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wellmoonloft opened this issue Apr 6, 2021 · 1 comment
Closed

Comments

@wellmoonloft
Copy link

_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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant