feat: 文章中的图片转成base64存在生成的markdown里

This commit is contained in:
fengxxc
2022-10-23 00:52:11 +08:00
parent cf2b343af0
commit ef6d319545
2 changed files with 26 additions and 2 deletions

View File

@@ -122,7 +122,8 @@ func formatCodeBlock(piece parse.Piece) string {
}
func formatImage(piece parse.Piece) string {
return "![" + piece.Attrs["alt"] + "](" + piece.Attrs["src"] + " \"" + piece.Attrs["title"] + "\")"
// return "![" + piece.Attrs["alt"] + "](" + piece.Attrs["src"] + " \"" + piece.Attrs["title"] + "\")"
return "![" + piece.Attrs["alt"] + "](data:image/png;base64," + piece.Val.(string) + ")"
}
func formatLink(piece parse.Piece) string {

View File

@@ -2,6 +2,7 @@ package parse
import (
"bytes"
"encoding/base64"
"io"
"io/ioutil"
"log"
@@ -25,7 +26,8 @@ func parseSection(s *goquery.Selection) []Piece {
attr["src"], _ = sc.Attr("data-src")
attr["alt"], _ = sc.Attr("alt")
attr["title"], _ = sc.Attr("title")
pieces = append(pieces, Piece{IMAGE, "", attr}, Piece{BR, nil, nil})
base64Image := img2base64(fetchImgFile(attr["src"]))
pieces = append(pieces, Piece{IMAGE, base64Image, attr}, Piece{BR, nil, nil})
} else if sc.Is("ol") {
pieces = append(pieces, parseList(sc, O_LIST)...)
} else if sc.Is("ul") {
@@ -193,3 +195,24 @@ func removeBrAndBlank(s string) string {
}
return strings.Replace(string(sb), "\n", " ", -1)
}
func fetchImgFile(url string) []byte {
res, err := http.Get(url)
if err != nil {
log.Fatalf("get Image from url %s error: %s", url, err.Error())
return nil
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("get Image from url %s error: %d %s", url, res.StatusCode, res.Status)
}
content, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("read image Response error: %s", err.Error())
}
return content
}
func img2base64(content []byte) string {
return base64.StdEncoding.EncodeToString(content)
}