Skip to content

Commit 1811b65

Browse files
committed
open external links in new tab in mdx and md, with plugin rehype-external-links
1 parent a73f000 commit 1811b65

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

astro.config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import icon from 'astro-icon';
66
import { defineConfig } from 'astro/config';
77

88
// must use relative imports, and their entire import subtrees
9+
import { rehypeExternalLinks } from './plugins/rehype-external-links';
910
import { remarkReadingTime } from './plugins/remark-reading-time.mjs';
1011
//
1112
// all relative imports in subtree
@@ -15,7 +16,9 @@ import { expressiveCodeIntegration } from './src/libs/integrations/expressive-co
1516
import { sitemapIntegration } from './src/libs/integrations/sitemap';
1617

1718
const { SITE_URL } = PROCESS_ENV;
19+
1820
const remarkPlugins = [remarkReadingTime];
21+
const rehypePlugins = [rehypeExternalLinks];
1922

2023
export default defineConfig({
2124
site: SITE_URL,
@@ -29,15 +32,15 @@ export default defineConfig({
2932
expressiveCodeIntegration(),
3033
sitemapIntegration(),
3134
react(),
32-
mdx(),
35+
mdx({ rehypePlugins }),
3336
// applyBaseStyles: false prevents double loading of tailwind
3437
tailwind({ applyBaseStyles: false }),
3538
icon({ iconDir: 'src/assets/icons' }),
3639
partytown({
3740
config: { forward: ['dataLayer.push'] },
3841
}),
3942
],
40-
markdown: { remarkPlugins },
43+
markdown: { remarkPlugins, rehypePlugins },
4144
vite: {
4245
build: {
4346
sourcemap: false,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"react-dom": "^19.1.0",
5959
"react-icons": "^5.5.0",
6060
"reading-time": "^1.5.0",
61+
"rehype-external-links": "^3.0.0",
6162
"sharp": "0.34.1",
6263
"tailwind-clip-path": "^1.0.0",
6364
"tailwind-merge": "^2.6.0",

plugins/rehype-external-links.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import rehypeExternalLinksPlugin from 'rehype-external-links';
2+
3+
import type { Plugin } from 'unified';
4+
5+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6+
export const rehypeExternalLinks: [Plugin<any[], any>, any] = [
7+
rehypeExternalLinksPlugin,
8+
{ target: '_blank', rel: ['noopener', 'noreferrer'] },
9+
];

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3827,6 +3827,11 @@ iron-webcrypto@^1.2.1:
38273827
resolved "https://registry.yarnpkg.com/iron-webcrypto/-/iron-webcrypto-1.2.1.tgz#aa60ff2aa10550630f4c0b11fd2442becdb35a6f"
38283828
integrity sha512-feOM6FaSr6rEABp/eDfVseKyTMDt+KGpeB35SkVn9Tyn0CqvVsY3EwI0v5i8nMHyJnzCIQf7nsy3p41TPkJZhg==
38293829

3830+
is-absolute-url@^4.0.0:
3831+
version "4.0.1"
3832+
resolved "https://registry.yarnpkg.com/is-absolute-url/-/is-absolute-url-4.0.1.tgz#16e4d487d4fded05cfe0685e53ec86804a5e94dc"
3833+
integrity sha512-/51/TKE88Lmm7Gc4/8btclNXWS+g50wXhYJq8HWIBAGUBnoAdRu1aXeh364t/O7wXDAcTJDP8PNuNKWUDWie+A==
3834+
38303835
is-alphabetical@^2.0.0:
38313836
version "2.0.1"
38323837
resolved "https://registry.yarnpkg.com/is-alphabetical/-/is-alphabetical-2.0.1.tgz#01072053ea7c1036df3c7d19a6daaec7f19e789b"
@@ -5748,6 +5753,18 @@ rehype-expressive-code@^0.41.2:
57485753
dependencies:
57495754
expressive-code "^0.41.2"
57505755

5756+
rehype-external-links@^3.0.0:
5757+
version "3.0.0"
5758+
resolved "https://registry.yarnpkg.com/rehype-external-links/-/rehype-external-links-3.0.0.tgz#2b28b5cda1932f83f045b6f80a3e1b15f168c6f6"
5759+
integrity sha512-yp+e5N9V3C6bwBeAC4n796kc86M4gJCdlVhiMTxIrJG5UHDMh+PJANf9heqORJbt1nrCbDwIlAZKjANIaVBbvw==
5760+
dependencies:
5761+
"@types/hast" "^3.0.0"
5762+
"@ungap/structured-clone" "^1.0.0"
5763+
hast-util-is-element "^3.0.0"
5764+
is-absolute-url "^4.0.0"
5765+
space-separated-tokens "^2.0.0"
5766+
unist-util-visit "^5.0.0"
5767+
57515768
rehype-parse@^9.0.0:
57525769
version "9.0.1"
57535770
resolved "https://registry.yarnpkg.com/rehype-parse/-/rehype-parse-9.0.1.tgz#9993bda129acc64c417a9d3654a7be38b2a94c20"

0 commit comments

Comments
 (0)