Skip to content

Commit

Permalink
release: 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW authored Sep 1, 2019
2 parents 3a8b1f9 + 02acdd0 commit 4222da1
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage/
tmp/
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "hexo"
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

.DS_Store
node_modules/
package-lock.json
tmp/
*.log
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
12 changes: 12 additions & 0 deletions .travis.yml
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
32 changes: 32 additions & 0 deletions README.md
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`.
15 changes: 15 additions & 0 deletions index.js
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'));
}
}
55 changes: 55 additions & 0 deletions lib/filter.js
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;
};
38 changes: 38 additions & 0 deletions package.json
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"
}
}

0 comments on commit 4222da1

Please sign in to comment.