diff --git a/README.md b/README.md index f850823..d67adbd 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ nofollow: exclude: - 'exclude1.com' - 'exclude2.com' + noreferrer: false ``` - **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..0aeb464 100755 --- a/index.js +++ b/index.js @@ -5,7 +5,8 @@ hexo.config.nofollow = Object.assign({ enable: true, field: 'site', - exclude: [] + exclude: [], + noreferrer: false }, hexo.config.nofollow); const config = hexo.config.nofollow; diff --git a/lib/filter.js b/lib/filter.js index 5e70272..2732dad 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 + ? ['noopener', 'external', 'nofollow'] + : ['noopener', 'external', 'nofollow', 'noreferrer']; 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..04afe4b 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: false + } + }); + + beforeEach(() => { + hexo.config = deepMerge({}, defaultCfg); + }); describe('Default', () => { const content = [ @@ -36,18 +47,18 @@ describe('hexo-filter-nofollow', () => { const expected = [ '# External link test', '1. External link', - 'Hexo', + 'Hexo', '2. External link with existed "rel" Attribute', - 'Hexo', - 'Hexo', + 'Hexo', + 'Hexo', '3. External link with existing "rel=noopener", "rel=external" or "rel=noreferrer"', - 'Hexo', + 'Hexo', 'Hexo', 'Hexo', 'Hexo', '4. External link with Other Attributes', - 'Hexo', - 'Hexo', + 'Hexo', + 'Hexo', '5. Internal link', 'Link', '6. Ignore links don\'t have "href" attribute', @@ -92,12 +103,12 @@ describe('hexo-filter-nofollow', () => { result.should.eql([ '# Exclude link test', '1. External link', - 'Hexo', + 'Hexo', '2. Ignore links whose hostname is same as config', 'Example Domain', '3. Ignore links whose hostname is included in exclude', 'Example Domain', - 'Example Domain' + 'Example Domain' ].join('\n')); }); @@ -109,7 +120,7 @@ describe('hexo-filter-nofollow', () => { result.should.eql([ '# Exclude link test', '1. External link', - 'Hexo', + 'Hexo', '2. Ignore links whose hostname is same as config', 'Example Domain', '3. Ignore links whose hostname is included in exclude', @@ -118,4 +129,26 @@ describe('hexo-filter-nofollow', () => { ].join('\n')); }); }); + + describe('noreferrer', () => { + it('Default', () => { + const result = nofollowFilter('Hexo'); + + result.should.eql('Hexo'); + }); + + it('Retain original noreferrer', () => { + const result = nofollowFilter('Hexo'); + + result.should.eql('Hexo'); + }); + + it('Enable', () => { + hexo.config.nofollow.noreferrer = true; + const result = nofollowFilter('Hexo'); + + result.should.eql('Hexo'); + }); + + }); });