共计 430 个字符,预计需要花费 2 分钟才能阅读完成。
// 用到的库 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
}
正文完