package richtext
import (
"fmt"
"regexp"
"strings"
)
func RemoveRichText(text string) string {
// Regular expression to replace
and
tags with newline
reBr := regexp.MustCompile(`<(br|p)(\s[^>]*)?>`)
text = reBr.ReplaceAllString(text, "\n")
// Add four full-width spaces after each newline
reNewline := regexp.MustCompile(`\n`)
text = reNewline.ReplaceAllString(text, "\n ") // 两个全角空格等于四个半角空格,即四个汉字的距离
// Regular expression to replace with empty string
reNbsp := regexp.MustCompile(` `)
text = reNbsp.ReplaceAllString(text, "")
// Regular expression to match other HTML tags
re := regexp.MustCompile("<[^>]*>")
plainText := re.ReplaceAllString(text, "")
plainText = strings.TrimSpace(plainText)
// plainText = " " + plainText
return plainText
}
func ExtractImageURLs(text string) []string {
re := regexp.MustCompile(`]+\bsrc=["']([^"']+)["']`)
matches := re.FindAllStringSubmatch(text, -1)
var urls []string
for _, match := range matches {
urls = append(urls, match[1])
}
return urls
}
func ConvertToRichText(plainText string, imageURLs []string) string {
// 将纯文本分割成段落
paragraphs := strings.Split(plainText, "\n")
// 创建一个StringBuilder来构建富文本
var richText strings.Builder
// 将每个段落包装在
标签中 for _, paragraph := range paragraphs { richText.WriteString("
") richText.WriteString(paragraph) richText.WriteString("
") } // 将每个图片URL包装在