From 531b38144d348d874da94f07825aab71036632fa Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Fri, 24 Jul 2020 10:33:49 +0100 Subject: [PATCH 1/2] feat: add 'noreferrer' option BREAKING CHANGE: 'noreferrer' is no longer added by default --- README.md | 2 ++ index.js | 3 ++- lib/filter.js | 9 +++++--- package.json | 1 + test/index.js | 57 ++++++++++++++++++++++++++++++++++++++++----------- 5 files changed, 56 insertions(+), 16 deletions(-) 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 ba020b9..23a1b52 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 01213a4..b493fb3 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "eslint": "^7.5.0", "eslint-config-hexo": "^4.1.0", "hexo": "hexojs/hexo", + "hexo-util": "^2.2.0", "mocha": "^8.0.1" } } 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'); + }); + + }); }); From 266fda20f0f8e06b288b77635f61a7d79c9c6275 Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Sat, 25 Jul 2020 05:44:15 +0100 Subject: [PATCH 2/2] docs: add 'noreferrer' option --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d67adbd..8569ac8 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Add nofollow attribute to all external links automatically. -`hexo-filter-nofollow` add `rel="noopener external nofollow noreferrer"` to all external links for security, privacy and SEO. [Read more](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types). +`hexo-filter-nofollow` add `rel="noopener external nofollow"` to all external links for security, privacy and SEO. [Read more](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types). ## Installations @@ -33,4 +33,5 @@ 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. +- **noreferrer** - Add noreferrer to `rel` attribute. Prevents browser from sending [referer](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer) to external links. + - Disabling this option (default) does not remove existing `noreferrer` value.