-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
coverage/ | ||
tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "hexo" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
.DS_Store | ||
node_modules/ | ||
package-lock.json | ||
tmp/ | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- "8" | ||
- "10" | ||
- "node" # latest stable Node.js release | ||
|
||
cache: npm | ||
|
||
script: | ||
- npm install | ||
- npm run eslint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,34 @@ | ||
# 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) | ||
|
||
Add nofollow attribute to all external links automatically. | ||
|
||
`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 | ||
|
||
```bash | ||
$ npm i hexo-filter-nofollow --save | ||
``` | ||
|
||
## Options | ||
|
||
```yaml | ||
nofollow: | ||
enable: true | ||
field: site | ||
exclude: | ||
- 'exclude1.com' | ||
- 'exclude2.com' | ||
``` | ||
- **enable** - Enable the plugin. Default value is `false`. | ||
- **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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
const { parse } = require('url'); | ||
|
||
function isExternal(url, config) { | ||
let exclude = config.nofollow.exclude; | ||
const data = parse(url); | ||
const host = data.hostname; | ||
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 (const 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'; | ||
|
||
return data.replace(/<a.*?(href=['"](.*?)['"]).*?>/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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "hexo-filter-nofollow", | ||
"version": "2.0.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": { | ||
"eslint": "eslint .", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": "hexojs/hexo-filter-nofollow", | ||
"keywords": [ | ||
"hexo", | ||
"seo", | ||
"hexo-plugin", | ||
"hexo-filter", | ||
"nofollow" | ||
], | ||
"author": "SukkaW <isukkaw@gmail.com> (https://skk.moe)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/hexojs/hexo-filter-nofollow/issues" | ||
}, | ||
"homepage": "https://github.com/hexojs/hexo-filter-nofollow#readme", | ||
"devDependencies": { | ||
"eslint": "^6.2.2", | ||
"eslint-config-hexo": "^3.0.0" | ||
} | ||
} |