1.0.0 • Published 4 years ago

posthtml-outlinks v1.0.0

Weekly downloads
146
License
MIT
Repository
github
Last release
4 years ago

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).

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

NameTypeDefaultPropertyDescription
excludeHosts{Array}[]RequiredDomains to exclude from processing
noTarget{Array}[]OptionalDomains to which do not add target="_blank"
noRel{Array}[]OptionalDomains 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.

Author

Arthur Gareginyan