Skip to content

Commit a0f50e0

Browse files
committed
fix: support list items with attributes
1 parent 506f541 commit a0f50e0

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

html2text.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type options struct {
3131
func newOptions() *options {
3232
// apply defaults
3333
return &options{
34-
lbr: WIN_LBR,
34+
lbr: WIN_LBR,
3535
keepSpaces: false,
3636
}
3737
}
@@ -261,7 +261,7 @@ func HTML2TextWithOptions(html string, reqOpts ...Option) string {
261261

262262
if tagNameLowercase == "/ul" || tagNameLowercase == "/ol" {
263263
outBuf.WriteString(opts.lbr)
264-
} else if tagNameLowercase == "li" || tagNameLowercase == "li/" {
264+
} else if tagNameLowercase == "li" || tagNameLowercase == "li/" || strings.HasPrefix(tagNameLowercase, "li ") {
265265
if opts.listPrefix != "" {
266266
outBuf.WriteString(opts.lbr + opts.listPrefix)
267267
} else {

html2text_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ func TestHTML2Text(t *testing.T) {
127127
So(HTML2Text(`list of items<ul><li>One</li><li>Two</li><li>Three</li></ul>`), ShouldEqual, "list of items\r\nOne\r\nTwo\r\nThree\r\n")
128128
})
129129

130+
Convey("List with classes", func() {
131+
So(HTML2Text(`list of items<ul><li class="menu-item">One</li><li class="menu-item">Two</li><li class="menu-item">Three</li></ul>`), ShouldEqual, "list of items\r\nOne\r\nTwo\r\nThree\r\n")
132+
So(HTML2Text(`list of items<ol><li class="menu-item">One</li><li class="menu-item">Two</li><li class="menu-item">Three</li></ol>`), ShouldEqual, "list of items\r\nOne\r\nTwo\r\nThree\r\n")
133+
})
134+
130135
Convey("Optional list support", func() {
131136
So(HTML2TextWithOptions(`list of items<ul><li>One</li><li>Two</li><li>Three</li></ul>`, WithListSupport()), ShouldEqual, "list of items\r\n - One\r\n - Two\r\n - Three\r\n")
132137
So(HTML2TextWithOptions(`list of items<ol><li>One</li><li>Two</li><li>Three</li></ol>`, WithListSupport()), ShouldEqual, "list of items\r\n - One\r\n - Two\r\n - Three\r\n")

0 commit comments

Comments
 (0)