Skip to content

Add Icons to External Links

1 min read

This guides shows how to add icons to the end of each external link, so that visitors will know they are leaving your site.

  1. Start by installing the rehype-external-links package using your favorite package manager:

    Terminal window
    npm add rehype-external-links
  2. Import the plugin into your astro.config.mjs file:

    astro.config.mjs
    // ...
    import rehypeExternalLinks from 'rehype-external-links'
    export default defineConfig({
    // ...
    markdown: {
    rehypePlugins: [
    [
    rehypeExternalLinks,
    {
    content: { type: 'text', value: '' }
    }
    ],
    ]
    },
    })

    You can add rel (and target) to external links like this:

    // ...
    markdown: {
    rehypePlugins: [
    [
    rehypeExternalLinks,
    {
    target: "_blank",
    rel: ["nofollow", "noopener"],
    content: { type: "text", value: "" },
    },
    ],
    ]
    },
src/content/docs/example.mdx
[Google](https://gooogle.com)

The code above generates the following on the page:

Google

  1. Add icons to external links
  2. rehype-external-links
  3. Icons for external links