rehype-external-links

Adds rel (and optionally target) attributes to links that point to other sites.

Category
Headings & links
Maintained by
rehype team
License
MIT
Module format
ESM, default export
Install
npm install rehype-external-links

What it does

rehype-external-links finds absolute links, by default those using http or https, and adds attributes to them. Relative links such as /docs/ are left alone. Out of the box it adds rel="nofollow" and no target.

Open in a new tab, safely

external-links.mjs
import {rehype} from 'rehype'
import rehypeExternalLinks from 'rehype-external-links'

const file = await rehype()
  .data('settings', {fragment: true})
  .use(rehypeExternalLinks, {target: '_blank', rel: ['noopener', 'noreferrer']})
  .process('<a href="https://example.com">Out</a> <a href="/docs/">In</a>')

console.log(String(file))
Output node external-links.mjs
<a href="https://example.com" rel="noopener noreferrer" target="_blank">Out</a> <a href="/docs/">In</a>

When you set target: '_blank', the readme recommends adding noopener and noreferrer to rel to avoid exploitation of the window.opener API, as in this example. Passing rel replaces the default nofollow; include it in the list if you still want it.

Options

  • rel: array or string of link types, or a function per element. An empty array sets none.
  • target: '_blank', '_parent', '_self' or '_top', or a function.
  • protocols (default ['http', 'https']): which absolute URLs count as external, for example add mailto.
  • content and contentProperties: append content (for example a visually hidden “opens in a new tab” label) inside external links.
  • properties and test: extra attributes, and a filter for which links to change.

Official references

Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.

Use ↑ ↓ to move, Enter to open.