metalsmith-safe-links

Metalsmith plugin to strip protocal and hostname from local links and to add target and rel attributes to external links

Usage no npm install needed!

<script type="module">
  import metalsmithSafeLinks from 'https://cdn.skypack.dev/metalsmith-safe-links';
</script>

README

metalsmith-safe-links

Metalsmith plugin to strip <protocol://hostname> from local links and to add target and rel attributes to external links.

metalsmith: plugin npm: version license: ISC

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

werner@glinka.co

License

Code released under the ISC license.