From 3cb289b099655dc8cf65745e55d6b3f74467f394 Mon Sep 17 00:00:00 2001 From: fengxxc <744320491@qq.com> Date: Sun, 30 Oct 2022 12:54:01 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20base64=E5=9B=BE=E7=89=87=E6=94=B9?= =?UTF-8?q?=E6=88=90=E5=9C=A8=E4=B8=8B=E6=96=B9=E5=BC=95=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E5=BD=A2=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- format/format.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/format/format.go b/format/format.go index a41dc28..91a0b3f 100644 --- a/format/format.go +++ b/format/format.go @@ -8,6 +8,7 @@ import ( "github.com/fengxxc/wechatmp2markdown/parse" ) +// Format format article func Format(article parse.Article) string { var result string var titleMdStr string = formatTitle(article.Title) @@ -21,6 +22,7 @@ func Format(article parse.Article) string { return result } +// FormatAndSave fomat article and save to local file func FormatAndSave(article parse.Article, filePath string) error { var result string = Format(article) return ioutil.WriteFile(filePath, []byte(result), 0644) @@ -45,6 +47,7 @@ func formatTags(tags string) string { func formatContent(pieces []parse.Piece, depth int) string { var contentMdStr string + var base64Imgs []string for _, piece := range pieces { var pieceMdStr string switch piece.Type { @@ -61,7 +64,8 @@ func formatContent(pieces []parse.Piece, depth int) string { case parse.BOLD_ITALIC_TEXT: pieceMdStr = "***" + piece.Val.(string) + "***" case parse.IMAGE: - pieceMdStr = formatImage(piece) + pieceMdStr = formatImageRefer(piece, len(base64Imgs)) + base64Imgs = append(base64Imgs, piece.Val.(string)) case parse.TABLE: // TODO case parse.CODE_INLINE: @@ -81,6 +85,9 @@ func formatContent(pieces []parse.Piece, depth int) string { } contentMdStr += pieceMdStr } + for i := 0; i < len(base64Imgs); i++ { + contentMdStr += "\n[" + strconv.Itoa(i) + "]:" + "data:image/png;base64," + base64Imgs[i] + } return contentMdStr } @@ -121,11 +128,15 @@ func formatCodeBlock(piece parse.Piece) string { return codeMdStr } -func formatImage(piece parse.Piece) string { +func formatImageInline(piece parse.Piece, index int) string { // return "![" + piece.Attrs["alt"] + "](" + piece.Attrs["src"] + " \"" + piece.Attrs["title"] + "\")" return "![" + piece.Attrs["alt"] + "](data:image/png;base64," + piece.Val.(string) + ")" } +func formatImageRefer(piece parse.Piece, index int) string { + return "![" + piece.Attrs["alt"] + "][" + strconv.Itoa(index) + "]" +} + func formatLink(piece parse.Piece) string { var linkMdStr string = "[" + piece.Val.(string) + "](" + piece.Attrs["href"] + ")" return linkMdStr