What is remark-external-links?
The remark-external-links package is a plugin for the Remark Markdown processor that automatically modifies links in Markdown files. It is primarily used to add attributes like target and rel to external links, enhancing security and usability without manual HTML tagging.
What are remark-external-links's main functionalities?
Adding attributes to external links
This feature allows users to automatically add specific attributes such as target='_blank' and rel='nofollow noopener noreferrer' to external links in Markdown content. This enhances security by preventing tabnapping and improves SEO through 'nofollow'. The code sample demonstrates how to set up the plugin with Remark to process a simple Markdown string.
const remark = require('remark');
const html = require('remark-html');
const externalLinks = require('remark-external-links');
remark()
.use(externalLinks, {target: '_blank', rel: ['nofollow', 'noopener', 'noreferrer']})
.use(html)
.process('Check out [Google](https://google.com)!', function (err, file) {
console.log(String(file));
});
Other packages similar to remark-external-links
rehype-external-links
Similar to remark-external-links, rehype-external-links is a plugin but for Rehype, which processes HTML instead of Markdown. It offers similar functionalities in terms of adding attributes to external links in HTML content, making it a suitable alternative for projects that work directly with HTML rather than Markdown.
markdown-it-external-links
This is a plugin for the markdown-it parser, which is another popular Markdown processor. Like remark-external-links, it allows the addition of attributes to external links in Markdown files. The main difference lies in the base Markdown processor (markdown-it vs. remark), which might affect the choice depending on other project dependencies and personal preference for Markdown syntax extensions.
remark plugin to automatically add target
and rel
attributes
to external links.
Install
npm:
npm install remark-external-links
Use
Say we have the following file, example.js
:
var remark = require('remark')
var html = require('remark-html')
var externalLinks = require('remark-external-links')
remark()
.use(externalLinks, {target: false, rel: ['nofollow']})
.use(html)
.process('[remark](https://github.com/remarkjs/remark)', function(err, file) {
if (err) throw err
console.log(String(file))
})
Now, running node example
yields:
<p><a href="https://github.com/remarkjs/remark" rel="nofollow">remark</a></p>
API
Automatically add target
and rel
attributes to external links.
options
options.target
How to display referenced documents (string?
: _self
, _blank
, _parent
,
or _top
, default: _blank
).
Pass false
to not set target
s on links.
options.rel
Link types to hint about the referenced documents (Array.<string>
or string
, default: ['nofollow', 'noopener', 'noreferrer']
).
Pass false
to not set rel
s on links.
When using a target
, add noopener
and noreferrer
to avoid
exploitation of the window.opener
API.
options.protocols
Protocols to check, such as mailto
or tel
(Array.<string>
, default:
['http', 'https']
).
options.content
hast content to insert at the end of external links
(Node or Children).
Will be inserted in a <span>
element.
Useful for improving accessibility by giving users advanced warning when
opening a new window.
Contribute
See contributing.md
in remarkjs/.github
for ways
to get started.
See support.md
for ways to get help.
This project has a Code of Conduct.
By interacting with this repository, organisation, or community you agree to
abide by its terms.
License
MIT © Cédric Delpoux