// 用到的库 github.com/PuerkitoBio/goquery
// 为 content 里面的 img src 增加前缀
// contont 内容
// profix 图片前缀
func replaceContentImgSrc(contont, profix string) (string, error) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(contont))
if err != nil {
return "", err
}
doc.Find("img").Each(func(i int, selection *goquery.Selection) {
imgSrc, ok := selection.Attr("src")
if ok {
selection.SetAttr("src", profix+imgSrc)
}
})
html, err := doc.Html()
if err != nil {
return "", err
}
return html, nil
}
golang 处理富文本内容img,为src增加前缀
摘要
// 用到的库 github.com/PuerkitoBio/goquery // 为 content 里面的 img src 增加前缀 // contont 内容 // profix 图片前缀 func replaceContentImg
本文为本站原创,转载请注明出处。文中观点与所涉产品信息以官方发布为准。



