File: renderers_test.go

package info (click to toggle)
golang-github-johanneskaufmann-html-to-markdown 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,080 kB
  • sloc: makefile: 3
file content (103 lines) | stat: -rw-r--r-- 2,624 bytes parent folder | download
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
package base_test

import (
	"testing"

	"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 TestRenderAsX(t *testing.T) {
	input := `
<h1>heading</h1>
<footer>
	<strong>bold text</strong>
</footer>
	`

	testCases := []struct {
		desc string

		isInline   bool
		renderFunc converter.HandleRenderFunc

		expected string
	}{
		{
			desc: "Default Block",
			renderFunc: func(ctx converter.Context, w converter.Writer, n *html.Node) converter.RenderStatus {
				return converter.RenderTryNext
			},
			expected: "# heading\n\n**bold text**",
		},
		{
			desc:     "Default Inline",
			isInline: true,
			renderFunc: func(ctx converter.Context, w converter.Writer, n *html.Node) converter.RenderStatus {
				return converter.RenderTryNext
			},
			expected: "# heading\n\n**bold text**",
		},

		{
			desc:       "RenderAsHTML Block",
			renderFunc: base.RenderAsHTML,
			expected:   "# heading\n\n<footer><strong>bold text</strong></footer>",
		},
		{
			desc:       "RenderAsHTML Inline",
			isInline:   true,
			renderFunc: base.RenderAsHTML,
			expected:   "# heading\n\n<footer><strong>bold text</strong></footer>",
		},

		{
			desc:       "RenderAsHTMLWrapper Block",
			renderFunc: base.RenderAsHTMLWrapper,
			expected:   "# heading\n\n<footer>\n\n**bold text**\n\n</footer>",
		},
		{
			desc:       "RenderAsHTMLWrapper Inline",
			isInline:   true,
			renderFunc: base.RenderAsHTMLWrapper,
			expected:   "# heading\n\n<footer>\n\n**bold text**\n\n</footer>",
		},

		{
			desc:       "RenderAsPlaintextWrapper Block",
			renderFunc: base.RenderAsPlaintextWrapper,
			expected:   "# heading\n\n**bold text**",
		},
		{
			desc:       "RenderAsPlaintextWrapper Inline",
			isInline:   true,
			renderFunc: base.RenderAsPlaintextWrapper,
			expected:   "# heading\n\n**bold text**",
		},
	}
	for _, tC := range testCases {
		t.Run(tC.desc, func(t *testing.T) {
			conv := converter.NewConverter(
				converter.WithPlugins(
					base.NewBasePlugin(),
					commonmark.NewCommonmarkPlugin(),
				),
			)
			if tC.isInline {
				conv.Register.RendererFor("footer", converter.TagTypeInline, tC.renderFunc, converter.PriorityStandard)
			} else {
				conv.Register.RendererFor("footer", converter.TagTypeBlock, tC.renderFunc, converter.PriorityStandard)
			}

			output, err := conv.ConvertString(input)
			if err != nil {
				t.Fatal(err)
			}
			if output != tC.expected {
				t.Errorf("expected %q but got %q", tC.expected, output)
			}
		})
	}
}