Salta ai contenuti

Add icons to external links

Questi contenuti non sono ancora disponibili nella tua lingua.

Using a rehype plugin, you can identify and modify links in your Markdown files that point to external sites. This example adds icons to the end of each external link, so that visitors will know they are leaving your site.

  • An Astro project using Markdown for content pages.
  1. Install both the rehype-external-links plugin and @astrojs/markdown-remark.

    Terminal window
    npm install rehype-external-links @astrojs/markdown-remark
  2. Configure the plugin in your astro.config.mjs file.

    Import unified() and define it as the Markdown processor to support remark plugins. Then, pass to rehypePlugins an array containing your imported rehypeExternalLinks plugin and an options object with a content property. Set this property’s type to text if you want to add plain text to the end of the link. To add HTML to the end of the link instead, set the property type to raw.

    astro.config.mjs
    import { unified } from '@astrojs/markdown-remark';
    import { defineConfig } from 'astro/config';
    import rehypeExternalLinks from 'rehype-external-links';
    export default defineConfig({
    // ...
    markdown: {
    processor: unified({
    rehypePlugins: [
    [
    rehypeExternalLinks,
    {
    content: { type: 'text', value: ' 🔗' }
    }
    ],
    ]
    }),
    },
    });
Contribuisci Comunità Sponsor