mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-18 17:51:24 +00:00
fix: markdonw link
This commit is contained in:
@@ -1,32 +0,0 @@
|
|||||||
import React, { useMemo } from 'react';
|
|
||||||
import { Box } from '@chakra-ui/react';
|
|
||||||
import Image from './img/Image';
|
|
||||||
|
|
||||||
const regex = /((http|https|ftp):\/\/[^\s\u4e00-\u9fa5\u3000-\u303f\uff00-\uffef]+)/gi;
|
|
||||||
|
|
||||||
const Link = (props: { href?: string; children?: any }) => {
|
|
||||||
const Html = useMemo(() => {
|
|
||||||
const decText = decodeURIComponent(props.href || '');
|
|
||||||
|
|
||||||
return decText.replace(regex, (match, p1) => {
|
|
||||||
let text = decText === props.children?.[0] ? p1 : props.children?.[0];
|
|
||||||
const isInternal = /^\/#/i.test(p1);
|
|
||||||
const target = isInternal ? '_self' : '_blank';
|
|
||||||
|
|
||||||
if (props?.children?.[0]?.props?.node?.tagName === 'img') {
|
|
||||||
// eslint-disable-next-line @next/next/no-img-element
|
|
||||||
text = `<img src="${props?.children?.[0]?.props?.src}" />`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return `<a href="${p1}" target=${target}>${text}</a>`;
|
|
||||||
});
|
|
||||||
}, [props.children, props.href]);
|
|
||||||
|
|
||||||
return typeof Html === 'string' ? (
|
|
||||||
<Box as={'span'} dangerouslySetInnerHTML={{ __html: Html }} />
|
|
||||||
) : (
|
|
||||||
Html
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default React.memo(Link);
|
|
@@ -9,7 +9,6 @@ import 'katex/dist/katex.min.css';
|
|||||||
import styles from './index.module.scss';
|
import styles from './index.module.scss';
|
||||||
import dynamic from 'next/dynamic';
|
import dynamic from 'next/dynamic';
|
||||||
|
|
||||||
import Link from './Link';
|
|
||||||
import CodeLight from './CodeLight';
|
import CodeLight from './CodeLight';
|
||||||
|
|
||||||
const MermaidCodeBlock = dynamic(() => import('./img/MermaidCodeBlock'));
|
const MermaidCodeBlock = dynamic(() => import('./img/MermaidCodeBlock'));
|
||||||
@@ -50,7 +49,6 @@ const Markdown = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const components = useMemo(
|
const components = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
a: Link,
|
|
||||||
img: Image,
|
img: Image,
|
||||||
pre: 'div',
|
pre: 'div',
|
||||||
p: 'div',
|
p: 'div',
|
||||||
@@ -68,6 +66,7 @@ const Markdown = ({
|
|||||||
rehypePlugins={[RehypeKatex]}
|
rehypePlugins={[RehypeKatex]}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
components={components}
|
components={components}
|
||||||
|
linkTarget={'_blank'}
|
||||||
>
|
>
|
||||||
{source}
|
{source}
|
||||||
</ReactMarkdown>
|
</ReactMarkdown>
|
||||||
|
Reference in New Issue
Block a user