fix: add _blank to all markdown link

This commit is contained in:
Tristan Yang
2023-10-26 17:27:36 +08:00
parent 6b2b638b1a
commit 8a9ea9aa03
2 changed files with 13 additions and 3 deletions
+2 -2
View File
@@ -29,8 +29,8 @@ const ImagePreview = ({ container, context = "chat" }: Props) => {
evt.stopPropagation();
const target = evt.target as HTMLImageElement;
if (!target) return;
// 图片
if (target.nodeName == "IMG") {
// 图片 并且没被 a 标签包裹
if (target.nodeName == "IMG"&&target.parentElement?.tagName!=="A") {
const urlObj = new URL(target.src);
const originUrl = `${urlObj.origin}${urlObj.pathname}?file_path=${urlObj.searchParams.get(
"file_path"
+11 -1
View File
@@ -1,6 +1,7 @@
import { FC, useRef } from "react";
import { FC, useRef, useEffect } from "react";
import "prismjs/themes/prism.css";
// @ts-ignore
import codeSyntaxHighlight from "@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight-all.js";
import { Viewer } from "@toast-ui/react-editor";
@@ -19,6 +20,15 @@ interface IProps {
}
const MarkdownRender: FC<IProps> = ({ content }) => {
const mdContainer = useRef<HTMLDivElement | null>(null);
useEffect(() => {
if (mdContainer.current) {
const links = mdContainer.current.querySelectorAll("a");
[...links].forEach((link) => {
link.setAttribute("target", "_blank");
});
}
}, [mdContainer]);
return (
<>
<ImagePreview container={mdContainer.current} context="markdown" />