mirror of
https://github.com/fengxxc/wechatmp2markdown.git
synced 2026-03-07 13:10:48 +08:00
feat: 块引用的支持
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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})
|
||||
|
||||
Reference in New Issue
Block a user