Skip to content

PostHTML plugin to add `target="_blank"` and `rel="nofollow noopener noreferrer"` to all external links (configurable).

License

Notifications You must be signed in to change notification settings

ArthurGareginyan/posthtml-outlinks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PostHTML OutLinks

posthtml-outlinks is a PostHTML plugin to automatically add target="_blank" and the rel attribute with the nofollow, noopener, noreferrer value to all external links (configurable).

www.npmjs.com/package/posthtml-outlinks

Before:

<a href="http://example.com/">External Link</a>
<a href="/">Internal Link</a>

After:

<a href="http://example.com/" target="_blank" rel="nofollow noopener noreferrer">External Link</a>
<a href="/">Internal Link</a>

Install

Method #1

npm i posthtml posthtml-outlinks

Method #2 (for Gulp)

npm i gulp-posthtml posthtml-outlinks --save-dev

Usage

Method #1

const fs = require('fs');
const posthtml = require('posthtml');
const posthtmlOutlinks = require('posthtml-outlinks');

let options = {
    excludeHosts: [
        'example.com',
        'www.example.com'
    ],
    noTarget: [],
    noRel: []
};

posthtml()
    .use(posthtmlOutlinks(options))
    .process(html)
    .then(result => fs.writeFileSync('./after.html', result.html));

Method #2 (for Gulp / New method )

const
    { src, dest }    = require('gulp'),
    posthtml         = require('gulp-posthtml'),
    posthtmlOutlinks = require('posthtml-outlinks');

function posthtml() {
    let plugins = [
        posthtmlOutlinks({
            excludeHosts: [
                'example.com',
                'www.example.com'
            ],
            noTarget: [],
            noRel: []
        })
    ];
    return src(`./build/*.html`)
        .pipe(posthtml(plugins))
        .pipe(dest(`./build/`));
}

Method #3 (for Gulp / Old method)

const gulp = require('gulp');
const posthtml = require('gulp-posthtml');
const posthtmlOutlinks = require('posthtml-outlinks');

const config = () => ({
    plugins: [
        posthtmlOutlinks({
            excludeHosts : [
                'example.com',
                'www.example.com'
            ],
            noTarget : [],
            noRel : []
        })
    ]
});

gulp.task('posthtml', () => gulp.src('./build/\*.html')
    .pipe(posthtml(config))
    .pipe(gulp.dest('./build')));

Options

Name Type Default Property Description
excludeHosts {Array} [] Required Domains to exclude from processing
noTarget {Array} [] Optional Domains to which do not add target="_blank"
noRel {Array} [] Optional Domains to which do not add rel="..."

Note! The domain name of your current website must be specified using the excludeHosts option.

Contributing

Welcome and thanks! I appreciate you taking the initiative to contribute to this project.

Contributing isn’t limited to just code. I encourage you to contribute in the way that best fits your abilities, by writing tutorials, making translation to your native language, giving a demo at your local meetup, helping other users with their support questions, or revising the documentation for this project.

Please take a moment to read the guidelines in the CONTRIBUTING.md and PostHTML Guidelines. Following them helps to communicate that you respect the time of the other contributors to the project. In turn, they’ll do their best to reciprocate that respect when working with you, across timezones and around the world.

Security Vulnerabilities

If you discover a security vulnerability within this plugin, please send an email to me. All security vulnerabilities will be promptly addressed.

License

This plugin is open-sourced software licensed under the MIT and is distributed free of charge.

Commercial licensing (e.g. for projects that can’t use an open-source license) is available upon request.

Author

Arthur Gareginyan

About

PostHTML plugin to add `target="_blank"` and `rel="nofollow noopener noreferrer"` to all external links (configurable).

Resources

License

Stars

Watchers

Forks

Packages

No packages published