feat: 支持解析表格(简单粗暴的原生html方式)

This commit is contained in:
fengxxc
2023-08-27 12:55:50 +08:00
parent c266359a76
commit cafe91bbc1
4 changed files with 22 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
BUILD_ENV := CGO_ENABLED=0
APP=wechatmp2markdown
VERSION=v1.1.4
VERSION=v1.1.5
# linux or mac 环境编译
# make [cmd]

View File

@@ -87,7 +87,7 @@ mingw32-make win-build-win32
编译好的文件在`./build`目录下
## TODO
- [ ] 支持解析表格元素(table tag)
- [x] 支持解析表格元素(table tag)
## 最后
抓紧时间窗口。努力记录。黑暗中记得光的模样。

View File

@@ -174,7 +174,7 @@ func formatContent(pieces []parse.Piece, depth int) (string, map[string][]byte)
pieceMdStr = formatImageRefer(piece, len(base64Imgs))
base64Imgs = append(base64Imgs, piece.Val.(string))
case parse.TABLE:
// TODO
pieceMdStr = formatTable(piece)
case parse.CODE_INLINE:
// TODO
case parse.CODE_BLOCK:
@@ -201,6 +201,15 @@ func formatContent(pieces []parse.Piece, depth int) (string, map[string][]byte)
return contentMdStr, saveImageBytes
}
func formatTable(piece parse.Piece) string {
var tableMdStr string
if piece.Attrs != nil && piece.Attrs["type"] == "native" {
tableMdStr = piece.Val.(string)
}
// TODO
return tableMdStr
}
func formatBlockQuote(piece parse.Piece, depth int) (string, map[string][]byte) {
var bqMdString string
var prefix string = ">"

View File

@@ -64,6 +64,8 @@ func parseSection(s *goquery.Selection, imagePolicy ImagePolicy, lastPieceType P
pieces = append(pieces, parseBlockQuote(sc, imagePolicy)...)
} else if sc.Is("strong") {
pieces = append(pieces, parseStrong(sc)...)
} else if sc.Is("table") {
pieces = append(pieces, parseTable(sc)...)
} else {
if sc.Text() != "" {
pieces = append(pieces, Piece{NORMAL_TEXT, sc.Text(), nil})
@@ -123,6 +125,14 @@ func parseBlockQuote(s *goquery.Selection, imagePolicy ImagePolicy) []Piece {
return bq
}
func parseTable(s *goquery.Selection) []Piece {
// 先简单粗暴把原生的挪过去
var table []Piece
html, _ := s.Html()
table = append(table, Piece{TABLE, "<table>" + html + "</table>", map[string]string{"type": "native"}})
return table
}
func parseStrong(s *goquery.Selection) []Piece {
var bt []Piece
bt = append(bt, Piece{BOLD_TEXT, strings.TrimSpace(s.Text()), nil})