-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb0bb0b
commit 8095bbd
Showing
11 changed files
with
73 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/content/blog/astro-plugin-open-external-links-in-new-tab.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
issue: 12 | ||
|
||
author: Daniele Salvagni | ||
title: 'An Astro plugin to open external links in a new tab' | ||
pubDate: 'Jul 11, 2024' | ||
emoji: 🛠️ | ||
|
||
description: > | ||
Creating a simple Rehype plugin to automatically add target="_blank" to all | ||
external links in Astro. | ||
--- | ||
|
||
By default, when writing markdown content in [Astro](https://astro.build/), all | ||
links are rendered to `<a>` tags that open in the same tab. This is not ideal | ||
for external links, as you might want to keep the user on your site. | ||
|
||
The solution is pretty simple, and it does involve writing a simple rehype | ||
plugin. | ||
|
||
## Writing the Rehype plugin | ||
|
||
To put it simply, a [rehype](https://github.com/rehypejs/rehype) plugin is just | ||
a function that takes the AST (Abstract Syntax Tree) of the HTML as input and | ||
modifies it in some way. | ||
|
||
The following [visits](https://github.com/syntax-tree/unist-util-visit) all the | ||
elements in the tree while adding `target="_blank"` to external links: | ||
|
||
```ts | ||
// src/plugins/targetBlank.ts | ||
|
||
import type { RehypePlugin } from '@astrojs/markdown-remark'; | ||
import { visit } from 'unist-util-visit'; | ||
import type { Element } from 'hast'; | ||
|
||
export const targetBlank: RehypePlugin = ({ domain = '' } = {}) => { | ||
return (tree) => { | ||
visit(tree, 'element', (e: Element) => { | ||
if ( | ||
e.tagName === 'a' && | ||
e.properties?.href && | ||
isExternal(e.properties.href.toString(), domain) | ||
) { | ||
e.properties!['target'] = '_blank'; | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
const isExternal = (url: string, domain: string) => | ||
url.startsWith('http') && !url.includes(domain); | ||
``` | ||
|
||
### Enabling the plugin | ||
|
||
To enable the plugin, update `astro.config.ts` with the following: | ||
|
||
```ts | ||
// astro.config.ts | ||
|
||
import { targetBlank } from './src/plugins/targetBlank'; | ||
|
||
export default defineConfig({ | ||
// ... | ||
markdown: { | ||
rehypePlugins: [[targetBlank, { domain: 'yourdomain.com' }]], | ||
}, | ||
}); | ||
``` | ||
|
||
And that's it! Now all external links will open in a new tab. |
2 changes: 0 additions & 2 deletions
2
src/content/blog/aws-sam-and-typescript-building-functions-and-layers.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
--- | ||
# layout: '../../layouts/BlogPost.astro' | ||
# collection: blog | ||
issue: 7 | ||
|
||
author: Daniele Salvagni | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
--- | ||
# layout: '../../layouts/BlogPost.astro' | ||
# collection: blog | ||
issue: 8 | ||
|
||
author: Daniele Salvagni | ||
|
2 changes: 0 additions & 2 deletions
2
src/content/blog/building-custom-effects-inside-a-1590a-enclosure.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
--- | ||
# layout: '../../layouts/BlogPost.astro' | ||
# collection: blog | ||
issue: 10 | ||
|
||
author: Daniele Salvagni | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
--- | ||
# layout: '../../layouts/BlogPost.astro' | ||
# collection: blog | ||
issue: 6 | ||
|
||
author: Daniele Salvagni | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
--- | ||
# layout: '../../layouts/BlogPost.astro' | ||
# collection: blog | ||
issue: 9 | ||
|
||
author: Daniele Salvagni | ||
|
2 changes: 0 additions & 2 deletions
2
src/content/blog/hosting-comments-on-github-for-static-websites.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
--- | ||
# layout: '../../layouts/BlogPost.astro' | ||
# collection: blog | ||
issue: 3 | ||
|
||
author: Daniele Salvagni | ||
|
2 changes: 0 additions & 2 deletions
2
src/content/blog/paginators-in-the-aws-sdk-for-javascript-v3.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
--- | ||
# layout: '../../layouts/BlogPost.astro' | ||
# collection: blog | ||
issue: 11 | ||
|
||
author: Daniele Salvagni | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
--- | ||
# layout: '../../layouts/BlogPost.astro' | ||
# collection: blog | ||
issue: 5 | ||
|
||
author: Daniele Salvagni | ||
|
2 changes: 0 additions & 2 deletions
2
src/content/blog/unfair-gamers-com-a-community-10-years-later.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
--- | ||
# layout: '../../layouts/BlogPost.astro' | ||
# collection: blog | ||
issue: 4 | ||
|
||
author: Daniele Salvagni | ||
|