From 6b5b37f6e42d5d266b90b2fd160626458b0548f4 Mon Sep 17 00:00:00 2001 From: fengxxc Date: Wed, 1 Dec 2021 18:19:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9D=97=E5=BC=95=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- format/format.go | 14 +++++++++++++- parse/parse.go | 20 +++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/format/format.go b/format/format.go index a0e8060..919fc74 100644 --- a/format/format.go +++ b/format/format.go @@ -61,6 +61,7 @@ func formatContent(pieces []parse.Piece, depth int) string { case parse.CODE_BLOCK: pieceMdStr = formatCodeBlock(piece) case parse.BLOCK_QUOTES: + pieceMdStr = formatBlockQuote(piece, depth) case parse.O_LIST: pieceMdStr = formatList(piece, depth) case parse.U_LIST: @@ -74,6 +75,17 @@ func formatContent(pieces []parse.Piece, depth int) string { return contentMdStr } +func formatBlockQuote(piece parse.Piece, depth int) string { + var bqMdString string + var prefix string = ">" + for i := 0; i < depth; i++ { + prefix += ">" + } + prefix += " " + bqMdString = prefix + formatContent(piece.Val.([]parse.Piece), depth+1) + " \n" + return bqMdString +} + func formatList(li parse.Piece, depth int) string { var listMdString string var prefix string @@ -85,7 +97,7 @@ func formatList(li parse.Piece, depth int) string { } else if li.Type == parse.O_LIST { prefix += strconv.Itoa(1) + ". " // 写死成1也大丈夫,markdown会自动累加序号 } - listMdString += prefix + formatContent(li.Val.([]parse.Piece), depth+1) + " \n" + listMdString = prefix + formatContent(li.Val.([]parse.Piece), depth+1) + " \n" return listMdString } diff --git a/parse/parse.go b/parse/parse.go index f409b8b..45259d7 100644 --- a/parse/parse.go +++ b/parse/parse.go @@ -39,10 +39,8 @@ func parseSection(s *goquery.Selection) []Piece { } else if s.Is("section") { p = append(p, parseSection(s)...) } else { - // p = Piece{NORMAL_TEXT, s.Text(), nil} - // TODO + p = append(p, Piece{NORMAL_TEXT, s.Text(), nil}) } - // fmt.Printf("%+v\n", t) piece = append(piece, p...) }) return piece @@ -81,8 +79,7 @@ func parsePre(s *goquery.Selection) []Piece { func parseUl(s *goquery.Selection) []Piece { var list []Piece s.Find("li").Each(func(i int, s *goquery.Selection) { - li := Piece{U_LIST, parseSection(s), nil} - list = append(list, li) + list = append(list, Piece{U_LIST, parseSection(s), nil}) }) return list } @@ -90,12 +87,19 @@ func parseUl(s *goquery.Selection) []Piece { func parseOl(s *goquery.Selection) []Piece { var list []Piece s.Find("li").Each(func(i int, s *goquery.Selection) { - li := Piece{O_LIST, parseSection(s), nil} - list = append(list, li) + list = append(list, Piece{O_LIST, parseSection(s), nil}) }) return list } +func parseBlockQuote(s *goquery.Selection) []Piece { + var bq []Piece + s.Children().Each(func(i int, s *goquery.Selection) { + bq = append(bq, Piece{BLOCK_QUOTES, parseSection(s), nil}) + }) + return bq +} + func ParseFromReader(r io.Reader) Article { var article Article doc, err := goquery.NewDocumentFromReader(r) @@ -139,6 +143,8 @@ func ParseFromReader(r io.Reader) Article { pieces = append(pieces, parseOl(s)...) } else if s.Is("ul") { pieces = append(pieces, parseUl(s)...) + } else if s.Is("blockquote") { + pieces = append(pieces, parseBlockQuote(s)...) } // sections = append(sections, paragraph) pieces = append(pieces, Piece{BR, nil, nil})