Skip to content

Commit

Permalink
link text
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu committed Jun 20, 2023
1 parent 3166376 commit 6fb312c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions client/src/components/Markdown/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { Box } from '@chakra-ui/react';

const regex = /((http|https|ftp):\/\/[^\s\u4e00-\u9fa5\u3000-\u303f\uff00-\uffef]+)/gi;

const Link = ({ href }: { href?: string }) => {
const decText = decodeURIComponent(href || '');
const Link = (props: { href?: string; children?: React.ReactNode[] }) => {
const decText = decodeURIComponent(props.href || '');
const replaceText = decText.replace(regex, (match, p1) => {
const text = decText === props.children?.[0] ? p1 : props.children?.[0];
const isInternal = /^\/#/i.test(p1);
const target = isInternal ? '_self' : '_blank';
return `<a href="${p1}" target=${target}>${p1}</a>`;
return `<a href="${p1}" target=${target}>${text}</a>`;
});

return <Box as={'span'} dangerouslySetInnerHTML={{ __html: replaceText }} />;
Expand Down

0 comments on commit 6fb312c

Please sign in to comment.