posthtml-noopener
is a PostHTML plugin to add rel="noopener noreferrer"
to links that open in a new tab.
Anchor links with the target="_blank"
attribute are recommended to include a rel="noopener"
or rel="noreferrer"
attribute to protect against cross-origin sites from exploiting window.opener
. By default, this plugin includes both attribute values.
Before:
<a href="http://example.com/" target="_blank">Link</a>
After:
<a href="http://example.com/" target="_blank" rel="noopener noreferrer">Link</a>
# Yarn
yarn add -D posthtml-noopener
# NPM
npm i -D posthtml-noopener
# pnpm
pnpm i -D posthtml-noopener
const fs = require("fs");
const posthtml = require("posthtml");
const { noopener } = require("posthtml-noopener");
const html = fs.readFileSync("./index.html");
posthtml()
.use(noopener())
.process(html)
.then((result) => fs.writeFileSync("./after.html", result.html));
See the PostHTML Guidelines.