metalsmith-safe-links
v1.4.3
Published
Metalsmith plugin to strip protocol and hostname from local links and to add target and rel attributes to external links
Downloads
127
Readme
metalsmith-safe-links
Metalsmith plugin to strip <protocol://hostname>
from local links and to add target and rel attributes to external links.
As markdown syntax only allows for alt and title attributes, content editors normally must use HTML to add other link attributes. This plugin negates the use of HTML for links in a markdown document.
This plugin must be used after markdown has been transformed into html
Installation
npm i metalsmith-safe-links --save
Usage
const metalsmith = require('metalsmith');
const layouts = require('@metalsmith/layouts')
const metalsmithSafeLinks = require('metalsmith-safe-links');
metalsmith(__dirname)
.use(layouts())
.use(metalsmithSafeLinks({
hostnames: ["www.livesite.com", "stagingsite.com"]
}))
.build();
Options
hostNames
An array of hostnames. The plugin will strip <protocol://hostname>
from all links with these names.
Metalsmith(__dirname)
.use(metalsmithSafeLinks({
hostnames: ["www.livesite.com", "stagingsite.com"]
}))
Example
An internal markdown link
[Go to this page](https://www.livesite.com/this-page/)
will be transformed into
<a href="/this-page/">Go to this page</a>
An external markdown link
[Go to this site](https://www.externalsite.com/)
will be transformed into
<a href="https://www.externalsite.com/" target="_blank" rel="noopener noreferrer">Go to this site</a>
Debug
To enable debug logs, set the DEBUG
environment variable to metalsmith-safe-links
:
DEBUG=metalsmith-safe-links
CLI usage
To use this plugin with the Metalsmith CLI, add metalsmith-safe-links
to the plugins
key in your metalsmith.json
file:
{
"plugins": [
{
"metalsmith-safe-links": {
"hostnames": ["www.livesite.com", "stagingsite.com"]
}
}
]
}
Authors
License
Code released under the ISC license.