From afe1b8a8bd311ff710448fe91bbc3b41344bdc83 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 28 Aug 2019 21:37:09 +0800 Subject: [PATCH 1/7] init: bring up a new project --- .gitignore | 6 ++++++ .npmrc | 1 + README.md | 3 ++- package.json | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100755 .gitignore create mode 100755 .npmrc create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..4863e8a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ + +.DS_Store +node_modules/ +package-lock.json +tmp/ +*.log \ No newline at end of file diff --git a/.npmrc b/.npmrc new file mode 100755 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/README.md b/README.md index 1b023ee..82aaba4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # hexo-filter-nofollow -Add nofollow attribute to all external links automatically. + +(WIP) Add nofollow attribute to all external links automatically. diff --git a/package.json b/package.json new file mode 100644 index 0000000..a90ab1a --- /dev/null +++ b/package.json @@ -0,0 +1,36 @@ +{ + "name": "hexo-filter-nofollow", + "version": "1.1.0", + "description": "Add nofollow attribute to all external links automatically", + "main": "index.js", + "directories": { + "lib": "./lib" + }, + "files": [ + "lib/", + "index.js" + ], + "engines": { + "node": ">= 8.6.0" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/hexojs/hexo-filter-nofollow.git" + }, + "keywords": [ + "hexo", + "seo", + "hexo-plugin", + "hexo-filter", + "nofollow" + ], + "author": "SukkaW (https://skk.moe)", + "license": "MIT", + "bugs": { + "url": "https://github.com/hexojs/hexo-filter-nofollow/issues" + }, + "homepage": "https://github.com/hexojs/hexo-filter-nofollow#readme" +} From afea093f630ad6ddd1f0a0f8e043bd456e10466b Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 28 Aug 2019 22:54:02 +0800 Subject: [PATCH 2/7] feat(filter): bring up filter function --- index.js | 15 ++++++++++++++ lib/filter.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100755 index.js create mode 100755 lib/filter.js diff --git a/index.js b/index.js new file mode 100755 index 0000000..306b9e4 --- /dev/null +++ b/index.js @@ -0,0 +1,15 @@ +/* global hexo */ + +'use strict'; + +if (hexo.config.nofollow && hexo.config.nofollow.enable) { + const config = hexo.config.nofollow; + + if (!config.field) config.field = 'site'; + + if (config.field === 'post') { + hexo.extend.filter.register('after_post_render', require('./lib/filter')); + } else { + hexo.extend.filter.register('after_render:html', require('./lib/filter')); + } +} diff --git a/lib/filter.js b/lib/filter.js new file mode 100755 index 0000000..7904676 --- /dev/null +++ b/lib/filter.js @@ -0,0 +1,56 @@ +'use strict'; + +const urlFn = require('url'); + +function isExternal(url, config) { + let exclude = config.nofollow.exclude; + const data = urlFn.parse(url); + const host = data.hostname; + const sitehost = urlFn.parse(config.url).hostname || config.url; + + if (!data.protocol || !sitehost) return false; + + if (exclude && !Array.isArray(exclude)) exclude = [exclude]; + + if (exclude && exclude.length) { + for (let i of exclude) { + if (sitehost === i) return false; + } + } + + if (host !== sitehost) return true; + + return false; +} + +module.exports = function(data) { + const hexo = this; + const config = hexo.config; + + const filterExternal = (data) => { + const noFollow = 'external nofollow noreferrer'; + + // https://github.com/hexojs/hexo/pull/3685 + return data.replace(//gi, (str, hrefStr, href) => { + if (!isExternal(href, hexo.config)) return str; + + if (/rel=/gi.test(str)) { + return str.replace(/rel="(.*?)"/gi, (relStr, rel) => { + // Avoid duplicate attribute + if (!/(external|nofollow|noreferrer)/gi.test(rel)) relStr = relStr.replace(rel, `${rel} ${noFollow}`); + return relStr; + }); + } + + return str.replace(hrefStr, `${hrefStr} rel="${noFollow}"`); + }); + }; + + if (config.nofollow.field === 'post') { + data.content = filterExternal(data.content); + } else { + data = filterExternal(data); + } + + return data; +}; From 6ac596354a06413062ccb3456e2d63049c5ada1a Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 28 Aug 2019 22:54:43 +0800 Subject: [PATCH 3/7] chore(eslint): add eslint & eslint-config-hexo --- .eslintignore | 3 +++ .eslintrc | 3 +++ package.json | 9 +++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100755 .eslintignore create mode 100755 .eslintrc diff --git a/.eslintignore b/.eslintignore new file mode 100755 index 0000000..f6b9638 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,3 @@ +node_modules/ +coverage/ +tmp/ \ No newline at end of file diff --git a/.eslintrc b/.eslintrc new file mode 100755 index 0000000..d6d70b6 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": "hexo" +} \ No newline at end of file diff --git a/package.json b/package.json index a90ab1a..46db10e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexo-filter-nofollow", - "version": "1.1.0", + "version": "2.0.0", "description": "Add nofollow attribute to all external links automatically", "main": "index.js", "directories": { @@ -14,6 +14,7 @@ "node": ">= 8.6.0" }, "scripts": { + "eslint": "eslint .", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -32,5 +33,9 @@ "bugs": { "url": "https://github.com/hexojs/hexo-filter-nofollow/issues" }, - "homepage": "https://github.com/hexojs/hexo-filter-nofollow#readme" + "homepage": "https://github.com/hexojs/hexo-filter-nofollow#readme", + "devDependencies": { + "eslint": "^6.2.2", + "eslint-config-hexo": "^3.0.0" + } } From 72ba9597cda4439980b6193b2862a5fa9e0824c9 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 28 Aug 2019 22:55:26 +0800 Subject: [PATCH 4/7] docs(readme): update --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 82aaba4..31af89b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,31 @@ # hexo-filter-nofollow +[![npm version](https://badge.fury.io/js/hexo-filter-nofollow.svg)](https://www.npmjs.com/package/hexo-filter-nofollow) + (WIP) Add nofollow attribute to all external links automatically. + +`hexo-filter-nofollow` add `rel="external nofollow noopener noreferrer"` to all external links for security, privacy and SEO. [Read more](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types). + +## Installations + +```bash +$ npm i hexo-filter-nofollow --save +``` + +## Options + +```yaml +nofollow: + enable: true + field: site # Can be 'site' or 'post', , Default value is 'site'. + exclude: + - 'exclude1.com' + - 'exclude2.com' +``` + +- **enable** - Enable the plugin. Default value is `false`. +- **field** - The scope you want the plugin to proceed. fault value is `site`. + - 'post' - Only add nofollow attribute to external links of your post content + - '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`. From 61f82b24e409bd21036cee9a1450efde308a6d39 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 28 Aug 2019 23:29:52 +0800 Subject: [PATCH 5/7] ci: add eslint to travis --- .travis.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100755 index 0000000..1582576 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +sudo: false +language: node_js +node_js: + - "8" + - "10" + - "node" # latest stable Node.js release + +cache: + directories: + - "node_modules" # cache the modules for faster build + +script: + - npm install + - npm run eslint From 308306765242e0505b554ee2deb3aedff283420c Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 28 Aug 2019 23:50:02 +0800 Subject: [PATCH 6/7] docs(readme): add badges --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 31af89b..99c030c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # hexo-filter-nofollow [![npm version](https://badge.fury.io/js/hexo-filter-nofollow.svg)](https://www.npmjs.com/package/hexo-filter-nofollow) +[![npm license](https://img.shields.io/npm/l/hexo-filter-nofollow)](./LICENSE) +[![travis status](https://api.travis-ci.com/hexojs/hexo-filter-nofollow.svg?branch=master)](https://travis-ci.com/hexojs/hexo-filter-nofollow) +![npm download](https://img.shields.io/npm/dt/hexo-filter-nofollow) (WIP) Add nofollow attribute to all external links automatically. From 02acdd020fd77eb18b9a803d48e02cc8ad349279 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 29 Aug 2019 17:15:01 +0800 Subject: [PATCH 7/7] fix: apply suggestions from code review by @curbengh & @YoshinoriN --- .eslintignore | 1 - .travis.yml | 4 +--- README.md | 10 +++++----- lib/filter.js | 9 ++++----- package.json | 5 +---- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/.eslintignore b/.eslintignore index f6b9638..e1c1079 100755 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,2 @@ -node_modules/ coverage/ tmp/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 1582576..bd3698a 100755 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,7 @@ node_js: - "10" - "node" # latest stable Node.js release -cache: - directories: - - "node_modules" # cache the modules for faster build +cache: npm script: - npm install diff --git a/README.md b/README.md index 99c030c..21cf558 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ [![travis status](https://api.travis-ci.com/hexojs/hexo-filter-nofollow.svg?branch=master)](https://travis-ci.com/hexojs/hexo-filter-nofollow) ![npm download](https://img.shields.io/npm/dt/hexo-filter-nofollow) -(WIP) Add nofollow attribute to all external links automatically. +Add nofollow attribute to all external links automatically. -`hexo-filter-nofollow` add `rel="external nofollow noopener 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="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). ## Installations @@ -20,15 +20,15 @@ $ npm i hexo-filter-nofollow --save ```yaml nofollow: enable: true - field: site # Can be 'site' or 'post', , Default value is 'site'. + field: site exclude: - 'exclude1.com' - 'exclude2.com' ``` - **enable** - Enable the plugin. Default value is `false`. -- **field** - The scope you want the plugin to proceed. fault value is `site`. - - 'post' - Only add nofollow attribute to external links of your post content +- **field** - The scope you want the plugin to proceed, can be 'site' or 'post'. Default value is `site`. + - 'post' - Only add nofollow attribute to external links in your post content - '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`. diff --git a/lib/filter.js b/lib/filter.js index 7904676..0efb99b 100755 --- a/lib/filter.js +++ b/lib/filter.js @@ -1,19 +1,19 @@ 'use strict'; -const urlFn = require('url'); +const { parse } = require('url'); function isExternal(url, config) { let exclude = config.nofollow.exclude; - const data = urlFn.parse(url); + const data = parse(url); const host = data.hostname; - const sitehost = urlFn.parse(config.url).hostname || config.url; + const sitehost = parse(config.url).hostname || config.url; if (!data.protocol || !sitehost) return false; if (exclude && !Array.isArray(exclude)) exclude = [exclude]; if (exclude && exclude.length) { - for (let i of exclude) { + for (const i of exclude) { if (sitehost === i) return false; } } @@ -30,7 +30,6 @@ module.exports = function(data) { const filterExternal = (data) => { const noFollow = 'external nofollow noreferrer'; - // https://github.com/hexojs/hexo/pull/3685 return data.replace(//gi, (str, hrefStr, href) => { if (!isExternal(href, hexo.config)) return str; diff --git a/package.json b/package.json index 46db10e..a1188c8 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,7 @@ "eslint": "eslint .", "test": "echo \"Error: no test specified\" && exit 1" }, - "repository": { - "type": "git", - "url": "git+https://github.com/hexojs/hexo-filter-nofollow.git" - }, + "repository": "hexojs/hexo-filter-nofollow", "keywords": [ "hexo", "seo",