refactor(ai): 改进 stripMarkdown 函数以更全面地处理 Markdown 元素
This commit is contained in:
parent
f88a8c609a
commit
327a83b8ad
1 changed files with 42 additions and 6 deletions
|
@ -179,19 +179,55 @@ func (a *AI) GetMsg() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func stripMarkdown(text string) string {
|
func stripMarkdown(text string) string {
|
||||||
|
|
||||||
|
println("before:", text)
|
||||||
// 移除代码块,但保留代码内容
|
// 移除代码块,但保留代码内容
|
||||||
re := regexp.MustCompile("```(.*?)```")
|
re := regexp.MustCompile("(?s)```.*?```")
|
||||||
|
text = re.ReplaceAllStringFunc(text, func(match string) string {
|
||||||
|
return strings.TrimPrefix(strings.TrimSuffix(match, "```"), "```")
|
||||||
|
})
|
||||||
|
|
||||||
|
// 移除行内代码
|
||||||
|
re = regexp.MustCompile("`([^`]+)`")
|
||||||
text = re.ReplaceAllString(text, "$1")
|
text = re.ReplaceAllString(text, "$1")
|
||||||
|
|
||||||
// 移除粗体
|
// 移除标题
|
||||||
re = regexp.MustCompile(`\*\*(.*?)\*\*`)
|
re = regexp.MustCompile(`(?m)^#{1,6}\s+(.+)$`)
|
||||||
text = re.ReplaceAllString(text, "$1")
|
text = re.ReplaceAllString(text, "$1")
|
||||||
|
|
||||||
// 移除下划线
|
// 移除粗体和斜体
|
||||||
re = regexp.MustCompile("__(.*?)__")
|
re = regexp.MustCompile(`(\*\*|__)(.+?)(\*\*|__)`)
|
||||||
|
text = re.ReplaceAllString(text, "$2")
|
||||||
|
re = regexp.MustCompile(`(\*|_)(.+?)(\*|_)`)
|
||||||
|
text = re.ReplaceAllString(text, "$2")
|
||||||
|
|
||||||
|
// 移除链接,保留链接文本
|
||||||
|
re = regexp.MustCompile(`\[([^\]]+)\]\([^\)]+\)`)
|
||||||
text = re.ReplaceAllString(text, "$1")
|
text = re.ReplaceAllString(text, "$1")
|
||||||
|
|
||||||
return text
|
// 移除图片
|
||||||
|
re = regexp.MustCompile(`!\[([^\]]*)\]\([^\)]+\)`)
|
||||||
|
text = re.ReplaceAllString(text, "$1")
|
||||||
|
|
||||||
|
// 移除水平线
|
||||||
|
re = regexp.MustCompile(`(?m)^-{3,}|_{3,}|\*{3,}$`)
|
||||||
|
text = re.ReplaceAllString(text, "")
|
||||||
|
|
||||||
|
// 移除块引用
|
||||||
|
re = regexp.MustCompile(`(?m)^>\s+(.+)`)
|
||||||
|
text = re.ReplaceAllString(text, "$1")
|
||||||
|
|
||||||
|
// 移除列表标记
|
||||||
|
re = regexp.MustCompile(`(?m)^[ \t]*[\*\-+][ \t]+`)
|
||||||
|
text = re.ReplaceAllString(text, "")
|
||||||
|
re = regexp.MustCompile(`(?m)^[ \t]*\d+\.[ \t]+`)
|
||||||
|
text = re.ReplaceAllString(text, "")
|
||||||
|
|
||||||
|
// 移除多余的空行
|
||||||
|
re = regexp.MustCompile(`\n{3,}`)
|
||||||
|
text = re.ReplaceAllString(text, "\n\n")
|
||||||
|
println("after:", text)
|
||||||
|
return strings.TrimSpace(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfig() (string, string, string) {
|
func getConfig() (string, string, string) {
|
||||||
|
|
Loading…
Reference in a new issue