mirror of
https://git.mirrors.martin98.com/https://github.com/bytedance/deer-flow
synced 2025-08-20 01:29:04 +08:00
feat: Enhance markdown's anti-shake
This commit is contained in:
parent
69d6e16111
commit
28b65d7a93
@ -13,6 +13,7 @@ import "katex/dist/katex.min.css";
|
||||
|
||||
import { Button } from "~/components/ui/button";
|
||||
import { rehypeSplitWordsIntoSpans } from "~/core/rehype";
|
||||
import { autoFixMarkdown } from "~/core/utils/markdown";
|
||||
import { cn } from "~/lib/utils";
|
||||
|
||||
import Image from "./image";
|
||||
@ -62,7 +63,9 @@ export function Markdown({
|
||||
}}
|
||||
{...props}
|
||||
>
|
||||
{dropMarkdownQuote(processKatexInMarkdown(children))}
|
||||
{autoFixMarkdown(
|
||||
dropMarkdownQuote(processKatexInMarkdown(children ?? "")) ?? "",
|
||||
)}
|
||||
</ReactMarkdown>
|
||||
{enableCopy && typeof children === "string" && (
|
||||
<div className="flex">
|
||||
|
57
web/src/core/utils/markdown.ts
Normal file
57
web/src/core/utils/markdown.ts
Normal file
@ -0,0 +1,57 @@
|
||||
export function autoFixMarkdown(markdown: string): string {
|
||||
return autoCloseTrailingLink(markdown);
|
||||
}
|
||||
|
||||
function autoCloseTrailingLink(markdown: string): string {
|
||||
// Fix unclosed Markdown links or images
|
||||
let fixedMarkdown: string = markdown;
|
||||
|
||||
// Fix unclosed image syntax 
|
||||
fixedMarkdown = fixedMarkdown.replace(
|
||||
/!\[([^\]]*)\]\(([^)]*)$/g,
|
||||
(match: string, altText: string, url: string): string => {
|
||||
return ``;
|
||||
},
|
||||
);
|
||||
|
||||
// Fix unclosed link syntax [...](...)
|
||||
fixedMarkdown = fixedMarkdown.replace(
|
||||
/\[([^\]]*)\]\(([^)]*)$/g,
|
||||
(match: string, linkText: string, url: string): string => {
|
||||
return `[${linkText}](${url})`;
|
||||
},
|
||||
);
|
||||
|
||||
// Fix unclosed image syntax ![...]
|
||||
fixedMarkdown = fixedMarkdown.replace(
|
||||
/!\[([^\]]*)$/g,
|
||||
(match: string, altText: string): string => {
|
||||
return `![${altText}]`;
|
||||
},
|
||||
);
|
||||
|
||||
// Fix unclosed link syntax [...]
|
||||
fixedMarkdown = fixedMarkdown.replace(
|
||||
/\[([^\]]*)$/g,
|
||||
(match: string, linkText: string): string => {
|
||||
return `[${linkText}]`;
|
||||
},
|
||||
);
|
||||
|
||||
// Fix unclosed images or links missing ")"
|
||||
fixedMarkdown = fixedMarkdown.replace(
|
||||
/!\[([^\]]*)\]\(([^)]*)$/g,
|
||||
(match: string, altText: string, url: string): string => {
|
||||
return ``;
|
||||
},
|
||||
);
|
||||
|
||||
fixedMarkdown = fixedMarkdown.replace(
|
||||
/\[([^\]]*)\]\(([^)]*)$/g,
|
||||
(match: string, linkText: string, url: string): string => {
|
||||
return `[${linkText}](${url})`;
|
||||
},
|
||||
);
|
||||
|
||||
return fixedMarkdown;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user