diff --git a/README.md b/README.md index f850823..0dd797f 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ nofollow: exclude: - 'exclude1.com' - 'exclude2.com' + noreferrer: true ``` - **enable** - Enable the plugin. Default value is `true`. @@ -32,3 +33,4 @@ nofollow: - 'site' - Add nofollow attribute to external links of whole sites - **exclude** - Exclude hostname. Specify subdomain when applicable, including `www`. - 'exclude1.com' does not apply to `www.exclude1.com` nor `en.exclude1.com`. +- **noreferrer** - Add noreferrer to `rel` attribute. diff --git a/index.js b/index.js index 9f05fd5..3dad53c 100755 --- a/index.js +++ b/index.js @@ -5,7 +5,8 @@ hexo.config.nofollow = Object.assign({ enable: true, field: 'site', - exclude: [] + exclude: [], + noreferrer: true }, hexo.config.nofollow); const config = hexo.config.nofollow; diff --git a/lib/filter.js b/lib/filter.js index 5e70272..de83673 100755 --- a/lib/filter.js +++ b/lib/filter.js @@ -24,8 +24,9 @@ function isExternal(url, config) { module.exports = function(data) { const hexo = this; const config = hexo.config; + const { exclude: excludeCfg, field, noreferrer } = config.nofollow; - const exclude = config.nofollow.exclude; + const exclude = excludeCfg; if (exclude && !Array.isArray(exclude)) { config.nofollow.exclude = [exclude]; } @@ -34,7 +35,9 @@ module.exports = function(data) { return data.replace(//gi, (str, hrefStr, href) => { if (!isExternal(href, config)) return str; - let noFollow = ['noopener', 'external', 'nofollow', 'noreferrer']; + let noFollow = noreferrer || noreferrer == null + ? ['noopener', 'external', 'nofollow', 'noreferrer'] + : ['noopener', 'external', 'nofollow']; if (/rel=/gi.test(str)) { str = str.replace(/\srel="(.*?)"/gi, (relStr, rel) => { @@ -51,7 +54,7 @@ module.exports = function(data) { }); }; - if (config.nofollow.field === 'post') { + if (field === 'post') { data.content = filterExternal(data.content); } else { data = filterExternal(data); diff --git a/package.json b/package.json index 97a0535..a86c0b4 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "eslint": "^6.2.2", "eslint-config-hexo": "^3.0.0", "hexo": "hexojs/hexo", + "hexo-util": "^2.2.0", "mocha": "^6.2.0" } } diff --git a/test/index.js b/test/index.js index 161b609..c9b5a5e 100755 --- a/test/index.js +++ b/test/index.js @@ -1,15 +1,26 @@ 'use strict'; require('chai').should(); +const Hexo = require('hexo'); +const { deepMerge } = require('hexo-util'); describe('hexo-filter-nofollow', () => { - const Hexo = require('hexo'); const hexo = new Hexo(); - const nofollowFilter = require('../lib/filter').bind(hexo); hexo.config.url = 'https://example.com'; - hexo.config.nofollow = {}; + const defaultCfg = deepMerge(hexo.config, { + nofollow: { + enable: true, + field: 'site', + exclude: [], + noreferrer: true + } + }); + + beforeEach(() => { + hexo.config = deepMerge({}, defaultCfg); + }); describe('Default', () => { const content = [ @@ -118,4 +129,26 @@ describe('hexo-filter-nofollow', () => { ].join('\n')); }); }); + + describe('noreferrer', () => { + it('Default', () => { + const result = nofollowFilter('Hexo'); + + result.should.eql('Hexo'); + }); + + it('Disable', () => { + hexo.config.nofollow.noreferrer = false; + const result = nofollowFilter('Hexo'); + + result.should.eql('Hexo'); + }); + + it('Retain original noreferrer', () => { + hexo.config.nofollow.noreferrer = false; + const result = nofollowFilter('Hexo'); + + result.should.eql('Hexo'); + }); + }); });