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

Support ignore tag and path #163

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const env = new nunjucks.Environment();
const { join } = require('path');
const { readFileSync } = require('fs');
const { encodeURL, gravatar, full_url_for } = require('hexo-util');
const { isIgnored, ignoreSettings } = require('./ignore');


env.addFilter('uriencode', str => {
return encodeURL(str);
Expand All @@ -18,6 +20,7 @@ module.exports = function(locals, type, path) {
const { config } = this;
const { email, feed, url: urlCfg } = config;
const { icon: iconCfg, limit, order_by, template: templateCfg, type: typeCfg } = feed;
const ignore = ignoreSettings(feed);

env.addFilter('formatUrl', str => {
return full_url_for.call(this, str);
Expand All @@ -32,7 +35,7 @@ module.exports = function(locals, type, path) {

let posts = locals.posts.sort(order_by || '-date');
posts = posts.filter(post => {
return post.draft !== true;
return post.draft !== true && !isIgnored(post, ignore);
});

if (posts.length <= 0) {
Expand Down
54 changes: 54 additions & 0 deletions lib/ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

/* Source:
https://github.com/alexbruno/hexo-generator-json-content
https://github.com/alexbruno/hexo-generator-json-content/blob/90745de5330933f97f4124dcc90c027b061c5819/src/modules/ignore.js
*/

function ignoreSettings(cfg) {
const ignore = cfg.ignore ? cfg.ignore : {};

ignore.paths = ignore.paths
? ignore.paths.map((path) => path.toLowerCase())
: [];

ignore.tags = ignore.tags
? ignore.tags.map((tag) => tag.replace('#', '').toLowerCase())
: [];

return ignore;
}

function isIgnored(content, settings) {
if (content.feed === false) {
return true;
}

if (content.feed === true) {
return false;
}

const pathIgnored = settings.paths.find((path) => content.path.includes(path));

if (pathIgnored) {
return true;
}

const tags = content.tags ? content.tags.map(mapTags) : [];
const tagIgnored = tags.filter((tag) => settings.tags.includes(tag)).length;

if (tagIgnored) {
return true;
}

return false;
}

function mapTags(tag) {
return typeof tag === 'object' ? tag.name.toLowerCase() : tag;
}

module.exports = {
isIgnored: isIgnored,
ignoreSettings: ignoreSettings
};