rehype plugin to automatically add `target` and `rel` attributes to external links
npm install rehype-external-links[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
[rehype][] plugin to add rel (and target) to external links.
* What is this?
* When should I use this?
* Install
* Use
* API
* [unified().use(rehypeExternalLinks[, options])](#unifieduserehypeexternallinks-options)
* CreateContent
* CreateProperties
* CreateRel
* CreateTarget
* Options
* Target
* Types
* Compatibility
* Security
* Contribute
* License
This package is a [unified][] ([rehype][]) plugin to add rel (and target)
attributes to external links.
It is particularly useful when displaying user content on your reputable site,
because users could link to disreputable sources (spam, scams, etc), as search
engines and other bots will discredit your site for linking to them (or
legitimize their sites).
In short: linking to something signals trust, but you can’t trust users.
This plugin adds certain rel attributes to prevent that from happening.
unified is a project that transforms content with abstract syntax trees
(ASTs).
rehype adds support for HTML to unified.
hast is the HTML AST that rehype uses.
This is a rehype plugin that adds rel (and target) to s in the AST.
This project is useful when you want to display user content from authors you
don’t trust (such as comments), as they might include links you don’t endorse,
on your website.
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
``sh`
npm install rehype-external-links
In Deno with [esm.sh][esmsh]:
`js`
import rehypeExternalLinks from 'https://esm.sh/rehype-external-links@3'
In browsers with [esm.sh][esmsh]:
`html`
Say our module example.js contains:
`js
import rehypeExternalLinks from 'rehype-external-links'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeExternalLinks, {rel: ['nofollow']})
.use(rehypeStringify)
.process('rehype')
console.log(String(file))
`
…then running node example.js yields:
`html`
This package exports no identifiers.
The default export is [rehypeExternalLinks][api-rehype-external-links].
Automatically add rel (and target?) to external links.
###### Parameters
* options ([Options][api-options], optional)
— configuration
###### Returns
Transform ([Transformer][unified-transformer]).
###### Notes
You should [likely not configure target][css-tricks].
You should at least set rel to ['nofollow'].target
When using a , add noopener and noreferrer to avoid exploitationwindow.opener
of the API.
When using a target, you should set content to adhere to accessibility
guidelines by [giving users advanced warning when opening a new window][g201].
Create a target for the element (TypeScript type).
###### Parameters
* element ([Element][hast-element])
— element to check
###### Returns
Content to add (Array or Node, optional).
Create properties for an element (TypeScript type).
###### Parameters
* element ([Element][hast-element])
— element to check
###### Returns
Properties to add ([Properties][hast-properties], optional).
Create a rel for the element (TypeScript type).
###### Parameters
* element ([Element][hast-element])
— element to check
###### Returns
rel to use (Array, optional).
Create a target for the element (TypeScript type).
###### Parameters
* element ([Element][hast-element])
— element to check
###### Returns
target to use ([Target][api-target], optional).
Configuration (TypeScript type).
###### Fields
* content (Array, [CreateContent][api-create-content], or Node,
optional)
— content to insert at the end of external links; will be inserted in a
element; useful for improving accessibility by giving userscontentProperties
advanced warning when opening a new window
* ([CreateProperties][api-create-properties] orProperties
[][hast-properties], optional)span
— properties to add to the wrapping contentproperties
* ([CreateProperties][api-create-properties] orProperties
[][hast-properties], optional)protocols
— properties to add to the link itself
* (Array, default: ['http', 'https'])mailto
— protocols to see as external, such as or telrel
* (Array, [CreateRel][api-create-rel], or string,['nofollow']
default: )[]
— [link types][mdn-rel] to hint about the referenced documents; pass an
empty array () to not set rels on links; when using a target, add noopenernoreferrer
and to avoid exploitation of the window.opener APItarget
* ([CreateTarget][api-create-target] or [Target][api-target],target
optional)
— how to display referenced documents; the default (nothing) is to not set
s on linkstest
* ([Test][is-test], optional)hast-util-is-element
— extra test to define which external link elements are modified; any test
that can be given to is supported
Target (TypeScript type).
###### Type
`ts`
type Target = '_blank' | '_parent' | '_self' | '_top'
This package is fully typed with [TypeScript][].
It exports the additional types
[CreateContent][api-create-content],CreateProperties
[][api-create-properties],CreateRel
[][api-create-rel],CreateTarget
[][api-create-target],Options
[][api-options], andTarget
[][api-target].
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, rehype-external-links@^3,
compatible with Node.js 16.
This plugin works with rehype-parse version 3+, rehype-stringify version 3+,rehype version 4+, and unified version 6+.
Improper use of rehype-external-links can open you up to a
[cross-site scripting (XSS)][xss] attack.
Either do not combine this plugin with user content or use
[rehype-sanitize][rehype-sanitize].
See [contributing.md][contributing] in [rehypejs/.github][health] for wayssupport.md`][support] for ways to get help.
to get started.
See [
This project has a [code of conduct][coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.
[MIT][license] © [Titus Wormer][author]
[build-badge]: https://github.com/rehypejs/rehype-external-links/workflows/main/badge.svg
[build]: https://github.com/rehypejs/rehype-external-links/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/rehypejs/rehype-external-links.svg
[coverage]: https://codecov.io/github/rehypejs/rehype-external-links
[downloads-badge]: https://img.shields.io/npm/dm/rehype-external-links.svg
[downloads]: https://www.npmjs.com/package/rehype-external-links
[size-badge]: https://img.shields.io/bundlejs/size/rehype-external-links
[size]: https://bundlejs.com/?q=rehype-external-links
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[collective]: https://opencollective.com/unified
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[chat]: https://github.com/rehypejs/rehype/discussions
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[health]: https://github.com/rehypejs/.github
[contributing]: https://github.com/rehypejs/.github/blob/HEAD/contributing.md
[support]: https://github.com/rehypejs/.github/blob/HEAD/support.md
[coc]: https://github.com/rehypejs/.github/blob/HEAD/code-of-conduct.md
[license]: license
[author]: https://wooorm.com
[hast-properties]: https://github.com/syntax-tree/hast#properties
[is-test]: https://github.com/syntax-tree/hast-util-is-element#test
[mdn-rel]: https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types
[rehype]: https://github.com/rehypejs/rehype
[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize
[typescript]: https://www.typescriptlang.org
[unified]: https://github.com/unifiedjs/unified
[unified-transformer]: https://github.com/unifiedjs/unified#transformer
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
[hast-element]: https://github.com/syntax-tree/hast#element
[g201]: https://www.w3.org/WAI/WCAG21/Techniques/general/G201
[css-tricks]: https://css-tricks.com/use-target_blank/
[api-create-content]: #createcontent
[api-create-properties]: #createproperties
[api-create-rel]: #createrel
[api-create-target]: #createtarget
[api-options]: #options
[api-target]: #target
[api-rehype-external-links]: #unifieduserehypeexternallinks-options