mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-23 14:14:46 +08:00
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
|
|
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;
|
||
|
|
}
|