External links in Eleventy

Published on April 29, 2025 - Estimated reading time is 2 minutes
Eleventy

When building websites with Eleventy, you may want to ensure that external links open in a new tab and include proper attributes for privacy and accessibility. This can be achieved by adding a transform to your Eleventy configuration.

The following transform modifies all external links in your HTML output to include the target="_blank" and rel="noreferrer" attributes. This ensures that external links open in a new tab and prevent potential privacy risks associated with referrer leakage.

eleventyConfig.addTransform("external-links", (content, outputPath) => {
  if (outputPath.endsWith(".html")) {
    return content.replace(
      /<a href="(http[s]:\/\/)(?!www\.ludeeus\.dev)([^"]+)"/g,
      '<a href="$1$2" target="_blank" rel="noreferrer"'
    );
  }
  return content;
});

Explanation

Why use this transform?

When to use this transform

This transform is particularly useful for blogs, documentation sites, or any website where external links are common. It ensures a consistent and privacy-focused user experience across your site.

Additional considerations

For more information on Eleventy transforms, refer to the Eleventy documentation.