Skip to content

Commit e7e2994

Browse files
committed
feat: 외부 링크인 경우 새 창에서 열리도록, 내부 링크인 경우 react-router-dom을 사용하도록 수정
1 parent 249956c commit e7e2994

File tree

1 file changed

+12
-0
lines changed
  • packages/common/src/components

1 file changed

+12
-0
lines changed

packages/common/src/components/mdx.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { MDXComponents } from "mdx/types";
1515
import muiComponents from "mui-mdx-components";
1616
import * as React from "react";
1717
import * as runtime from "react/jsx-runtime";
18+
import { Link } from "react-router-dom";
1819
import remarkGfm from "remark-gfm";
1920
import * as R from "remeda";
2021

@@ -46,6 +47,16 @@ const REGISTERED_KEYWORDS = [
4647
"}",
4748
];
4849

50+
const EXTERNAL_PROTOCOLS = ["http://", "https://", "mailto:", "tel:"];
51+
52+
const LinkHandler: React.FC<{ href: string }> = ({ href, ...props }) => {
53+
// If the href starts with "http" or "https", it's an external link
54+
if (EXTERNAL_PROTOCOLS.some((protocol) => href.startsWith(protocol)))
55+
return <a href={href} target="_blank" rel="noopener noreferrer" {...props} />;
56+
57+
return <Link to={href} {...props} />;
58+
};
59+
4960
const CustomMDXComponents: MDXComponents = {
5061
h1: (props) => <h1 style={{ margin: 0 }} {...props} />,
5162
h2: (props) => <h2 style={{ margin: 0 }} {...props} />,
@@ -54,6 +65,7 @@ const CustomMDXComponents: MDXComponents = {
5465
h5: (props) => <h5 style={{ margin: 0 }} {...props} />,
5566
h6: (props) => <h6 style={{ margin: 0 }} {...props} />,
5667
strong: (props) => <strong {...props} />,
68+
a: (props) => <LinkHandler {...props} />,
5769
hr: (props) => <StyledDivider {...props} />,
5870
em: (props) => <em {...props} />,
5971
ul: (props) => <ul {...props} />,

0 commit comments

Comments
 (0)