Add icons to external links
Este conteúdo não está disponível em sua língua ainda.
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.
Prerequisites
Section titled “Prerequisites”- An Astro project using Markdown for content pages.
Recipe
Section titled “Recipe”-
Install both the
rehype-external-linksplugin and@astrojs/markdown-remark.Terminal window npm install rehype-external-links @astrojs/markdown-remarkTerminal window pnpm add rehype-external-links @astrojs/markdown-remarkTerminal window yarn add rehype-external-links @astrojs/markdown-remark -
Configure the plugin in your
astro.config.mjsfile.Import
unified()and define it as the Markdown processor to support remark plugins. Then, pass torehypePluginsan array containing your importedrehypeExternalLinksplugin and an options object with acontentproperty. Set this property’stypetotextif you want to add plain text to the end of the link. To add HTML to the end of the link instead, set the propertytypetoraw.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: ' 🔗' }}],]}),},});The value of the
contentproperty is not represented in the accessibility tree. As such, it’s best to make clear that the link is external in the surrounding content, rather than relying on the icon alone.