Skip to content

Commit

Permalink
feat: add 'noreferrer' option
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Jul 24, 2020
1 parent ae79530 commit 4cb2390
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ nofollow:
exclude:
- 'exclude1.com'
- 'exclude2.com'
noreferrer: true
```
- **enable** - Enable the plugin. Default value is `true`.
Expand All @@ -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.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -34,7 +35,9 @@ module.exports = function(data) {
return data.replace(/<a.*?(href=['"](.*?)['"]).*?>/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) => {
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
39 changes: 36 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -118,4 +129,26 @@ describe('hexo-filter-nofollow', () => {
].join('\n'));
});
});

describe('noreferrer', () => {
it('Default', () => {
const result = nofollowFilter('<a href="https://hexo.io/">Hexo</a>');

result.should.eql('<a href="https://hexo.io/" rel="noopener external nofollow noreferrer">Hexo</a>');
});

it('Disable', () => {
hexo.config.nofollow.noreferrer = false;
const result = nofollowFilter('<a href="https://hexo.io/">Hexo</a>');

result.should.eql('<a href="https://hexo.io/" rel="noopener external nofollow">Hexo</a>');
});

it('Retain original noreferrer', () => {
hexo.config.nofollow.noreferrer = false;
const result = nofollowFilter('<a href="https://hexo.io/" rel="noreferrer">Hexo</a>');

result.should.eql('<a href="https://hexo.io/" rel="noopener external nofollow noreferrer">Hexo</a>');
});
});
});

0 comments on commit 4cb2390

Please sign in to comment.