- Category
- Headings & links
- Maintained by
- rehype team
- License
- MIT
- Module format
- ESM, default export
npm install rehype-external-linksWhat 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
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))<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 addmailto.contentandcontentProperties: append content (for example a visually hidden “opens in a new tab” label) inside external links.propertiesandtest: extra attributes, and a filter for which links to change.
Official references
- rehype-external-links readme (API and all options) (opens in a new tab)
- rehype-external-links on npm (current version) (opens in a new tab)
Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.