diff --git a/lib/generator.js b/lib/generator.js index 0c616b8..928ed63 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -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); @@ -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); @@ -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) { diff --git a/lib/ignore.js b/lib/ignore.js new file mode 100644 index 0000000..cadbab5 --- /dev/null +++ b/lib/ignore.js @@ -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 +};