-
Notifications
You must be signed in to change notification settings - Fork 129
/
convert_test.go
163 lines (140 loc) · 3.43 KB
/
convert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package htmltomarkdown_test
import (
"fmt"
"log"
"strings"
"sync"
"testing"
htmltomarkdown "github.com/JohannesKaufmann/html-to-markdown/v2"
"github.com/JohannesKaufmann/html-to-markdown/v2/converter"
"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/base"
"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/commonmark"
"golang.org/x/net/html"
)
func ExampleConvertString() {
input := `<strong>Bold Text</strong>`
markdown, err := htmltomarkdown.ConvertString(input)
if err != nil {
log.Fatal(err)
}
fmt.Println(markdown)
// Output: **Bold Text**
}
func ExampleWithDomain() {
input := `<img src="/assets/image.png" />`
markdown, err := htmltomarkdown.ConvertString(
input,
// Provide a different domain for every convert call:
converter.WithDomain("https://example.com"),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(markdown)
// Output: ![](https://example.com/assets/image.png)
}
func ExampleConvertNode() {
input := `<strong>Bold Text</strong>`
doc, err := html.Parse(strings.NewReader(input))
if err != nil {
log.Fatal(err)
}
markdown, err := htmltomarkdown.ConvertNode(doc)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(markdown))
// Output: **Bold Text**
}
func TestConvertString_WindowsCarriageReturn(t *testing.T) {
testCases := []struct {
desc string
input string
expected string
}{
{
desc: "just newlines",
input: "\r\n\r\n\r\n\r\n",
expected: "",
},
{
desc: "inside strong",
input: "<strong>Bold\r\n\r\n\r\n\r\nText</strong>",
expected: "**Bold Text**",
},
{
desc: "inside paragraph",
input: "<p>Some\r\n\r\n\r\n\r\nText</p>",
expected: "Some Text",
},
{
desc: "inside list",
input: "<ul><li>Some\r\n\r\n\r\n\r\nText</li></ul>",
expected: "- Some Text",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
output, err := htmltomarkdown.ConvertString(tC.input)
if err != nil {
t.Fatal(err)
}
if output != tC.expected {
t.Errorf("expected %q but got %q", tC.expected, output)
}
})
}
}
func TestDataRaceDetector(t *testing.T) {
conv := converter.NewConverter(
converter.WithPlugins(
base.NewBasePlugin(),
commonmark.NewCommonmarkPlugin(),
),
)
input := `<i>italic text</i>`
var wg sync.WaitGroup
for i := 0; i < 500; i++ {
wg.Add(1)
go func() {
conv.Register.EscapedChar('~')
conv.Register.UnEscaper(
func(chars []byte, index int) int { return -1 },
converter.PriorityStandard,
)
conv.Register.PreRenderer(
func(ctx converter.Context, doc *html.Node) {},
converter.PriorityStandard,
)
conv.Register.TextTransformer(
func(ctx converter.Context, content string) string { return content },
converter.PriorityStandard,
)
conv.Register.Renderer(
func(ctx converter.Context, w converter.Writer, n *html.Node) converter.RenderStatus {
return converter.RenderTryNext
},
converter.PriorityStandard,
)
conv.Register.PostRenderer(
func(ctx converter.Context, content []byte) []byte {
return content
},
converter.PriorityStandard,
)
conv.Register.TagType("script", converter.TagTypeBlock, converter.PriorityStandard)
output, err := conv.ConvertString(input, converter.WithDomain("example.com"))
if err != nil {
t.Error(err)
}
_ = output
output2, err := conv.ConvertString(input)
if err != nil {
t.Error(err)
}
_ = output2
wg.Done()
}()
}
wg.Wait()
}