Skip to content

Commit 0e300ae

Browse files
committed
fix: open external links in new tabs
1 parent 9d37251 commit 0e300ae

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/utils/markdownUtils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,28 @@ import { marked } from "marked";
22
import { markedHighlight } from "marked-highlight";
33
import hljs from "highlight.js";
44

5+
// Create custom renderer
6+
const renderer = new marked.Renderer();
7+
8+
interface MarkedLink {
9+
href: string;
10+
title?: string | null;
11+
text: string;
12+
}
13+
14+
// Override link rendering to open external links in new tabs
15+
renderer.link = ({ href, title, text }: MarkedLink) => {
16+
const isExternal = href && (href.startsWith('http://') || href.startsWith('https://'));
17+
const attrs = isExternal ? ' target="_blank" rel="noopener noreferrer"' : '';
18+
const titleAttr = title ? ` title="${title}"` : '';
19+
return `<a href="${href}"${attrs}${titleAttr}>${text}</a>`;
20+
};
21+
522
marked.setOptions({
623
gfm: true,
724
breaks: true,
825
silent: true,
26+
renderer: renderer,
927
});
1028

1129
marked.use(

0 commit comments

Comments
 (0)