mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-30 02:12:38 +00:00
37 lines
1010 B
TypeScript
37 lines
1010 B
TypeScript
import { simpleText } from './tools';
|
|
|
|
/* Delete redundant text in markdown */
|
|
export const simpleMarkdownText = (rawText: string) => {
|
|
rawText = simpleText(rawText);
|
|
|
|
// Remove a line feed from a hyperlink or picture
|
|
rawText = rawText.replace(/\[([^\]]+)\]\((.+?)\)/g, (match, linkText, url) => {
|
|
const cleanedLinkText = linkText.replace(/\n/g, ' ').trim();
|
|
|
|
if (!url) {
|
|
return '';
|
|
}
|
|
|
|
return `[${cleanedLinkText}](${url})`;
|
|
});
|
|
|
|
// replace special \.* ……
|
|
const reg1 = /\\([-.!`_(){}\[\]])/g;
|
|
if (reg1.test(rawText)) {
|
|
rawText = rawText.replace(/\\([`!*()+-_\[\]{}\\.])/g, '$1');
|
|
}
|
|
|
|
// replace \\n
|
|
rawText = rawText.replace(/\\\\n/g, '\\n');
|
|
|
|
// Remove headings and code blocks front spaces
|
|
['####', '###', '##', '#', '```', '~~~'].forEach((item, i) => {
|
|
const reg = new RegExp(`\\n\\s*${item}`, 'g');
|
|
if (reg.test(rawText)) {
|
|
rawText = rawText.replace(new RegExp(`(\\n)\\s*(${item})`, 'g'), '$1$2');
|
|
}
|
|
});
|
|
|
|
return rawText.trim();
|
|
};
|