File: commonmark_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 (335 lines) | stat: -rw-r--r-- 9,571 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package commonmark_test

import (
	"bytes"
	"testing"

	htmltomarkdown "github.com/JohannesKaufmann/html-to-markdown/v2"
	"github.com/JohannesKaufmann/html-to-markdown/v2/converter"
	"github.com/JohannesKaufmann/html-to-markdown/v2/internal/tester"
	"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/base"
	"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/commonmark"
)

func TestGoldenFiles(t *testing.T) {
	goldenFileConvert := func(htmlInput []byte) ([]byte, error) {
		conv := converter.NewConverter(
			converter.WithPlugins(
				base.NewBasePlugin(),
				commonmark.NewCommonmarkPlugin(),
			),
		)

		// It makes the testcases easier to read if we keep the <!-- comment --> as raw html block.
		// To override the setting from the base it needs to run *early*
		conv.Register.RendererFor("#comment", converter.TagTypeBlock, base.RenderAsHTML, converter.PriorityEarly)

		return conv.ConvertReader(bytes.NewReader(htmlInput))
	}
	roundTripConvert := func(html []byte) (markdown []byte, err error) {
		// For the golden files we are keeping #comment as a block
		// but collapse treats it as an inline element (which it is).
		//
		// So this testcase would cause problems.
		// "<div>before    <!-- -->    after</div>"

		md, err := htmltomarkdown.ConvertString(string(html))

		return []byte(md), err
	}

	tester.GoldenFiles(t, goldenFileConvert, roundTripConvert)
}

func TestOptionFunc(t *testing.T) {
	testCases := []struct {
		desc     string
		input    string
		options  []commonmark.OptionFunc
		expected string
	}{
		// - - - - - - - - - - Italic & Bold - - - - - - - - - - //
		{
			desc: "WithEmDelimiter",
			options: []commonmark.OptionFunc{
				commonmark.WithEmDelimiter("_"),
			},
			input:    `<em>italic</em>`,
			expected: `_italic_`,
		},
		{
			desc: "WithStrongDelimiter",
			options: []commonmark.OptionFunc{
				commonmark.WithStrongDelimiter("__"),
			},
			input:    `<b>bold</b>`,
			expected: `__bold__`,
		},

		// - - - - - - - - - - Horizontal Rule - - - - - - - - - - //
		{
			desc: "WithHorizontalRule(***)",
			options: []commonmark.OptionFunc{
				commonmark.WithHorizontalRule("***"),
			},
			input:    `<hr />`,
			expected: `***`,
		},
		{
			desc: "WithHorizontalRule(******)",
			options: []commonmark.OptionFunc{
				commonmark.WithHorizontalRule("******"),
			},
			input:    `<hr />`,
			expected: `******`,
		},
		{
			desc: "WithHorizontalRule(---)",
			options: []commonmark.OptionFunc{
				commonmark.WithHorizontalRule("---"),
			},
			input:    `<hr />`,
			expected: `---`,
		},
		{
			desc: "WithHorizontalRule(___)",
			options: []commonmark.OptionFunc{
				commonmark.WithHorizontalRule("___"),
			},
			input:    `<hr />`,
			expected: `___`,
		},

		// - - - - - - - - - - List - - - - - - - - - - //
		{
			desc: "WithBulletListMarker(+)",
			options: []commonmark.OptionFunc{
				commonmark.WithBulletListMarker("+"),
			},
			input:    `<ul><li>list item</li></ul>`,
			expected: `+ list item`,
		},
		{
			desc: "WithBulletListMarker(*)",
			options: []commonmark.OptionFunc{
				commonmark.WithBulletListMarker("*"),
			},
			input:    `<ul><li>list a</li></ul>  <ul><li>list b</li></ul>`,
			expected: "* list a\n\n<!--THE END-->\n\n* list b",
		},
		{
			desc: "WithBulletListMarker(*) and WithListEndComment(false)",
			options: []commonmark.OptionFunc{
				commonmark.WithBulletListMarker("*"),
				commonmark.WithListEndComment(false),
			},
			input:    `<ul><li>list a</li></ul>  <ul><li>list b</li></ul>`,
			expected: "* list a\n\n* list b",
		},

		// - - - - - - - - - - Code - - - - - - - - - - //
		{
			desc: "WithCodeBlockFence",
			options: []commonmark.OptionFunc{
				commonmark.WithCodeBlockFence("~~~"),
			},
			input:    `<pre><code>hello world</code></pre>`,
			expected: "~~~\nhello world\n~~~",
		},

		// - - - - - - - - - - Heading - - - - - - - - - - //
		{
			desc: "WithHeadingStyle(atx)",
			options: []commonmark.OptionFunc{
				commonmark.WithHeadingStyle("atx"),
			},
			input:    `<h1>important<br/>heading</h1>`,
			expected: "# important heading",
		},
		{
			desc: "WithHeadingStyle(setext)",
			options: []commonmark.OptionFunc{
				commonmark.WithHeadingStyle("setext"),
			},
			input:    `<h1>important<br/>heading</h1>`,
			expected: "important  \nheading\n===========",
		},

		// - - - - - - - - - - Link - - - - - - - - - - //
		{
			desc: "WithLinkEmptyHrefBehavior(render)",
			options: []commonmark.OptionFunc{
				commonmark.WithLinkEmptyHrefBehavior("render"),
			},
			input:    `<a href="">the link content</a>`,
			expected: "[the link content]()",
		},
		{
			desc: "WithLinkEmptyHrefBehavior(skip)",
			options: []commonmark.OptionFunc{
				commonmark.WithLinkEmptyHrefBehavior("skip"),
			},
			input:    `<a href="">the link content</a>`,
			expected: "the link content",
		},
		// - - - //
		{
			desc: "WithLinkEmptyContentBehavior(render)",
			options: []commonmark.OptionFunc{
				commonmark.WithLinkEmptyContentBehavior("render"),
			},
			input:    `<a href="/page"></a>`,
			expected: "[](/page)",
		},
		{
			desc: "WithLinkEmptyContentBehavior(skip)",
			options: []commonmark.OptionFunc{
				commonmark.WithLinkEmptyContentBehavior("skip"),
			},
			input:    `<a href="/page"></a>`,
			expected: "",
		},

		// TODO: handle other link styles
		// {
		// 	desc: "WithLinkStyle(LinkInlined)",
		// 	options: []commonmark.OptionFunc{
		// 		commonmark.WithLinkStyle(commonmark.LinkInlined),
		// 	},
		// 	input:    `<a href="/about">link</a>`,
		// 	expected: "[link](/about)",
		// },
	}
	for _, tC := range testCases {
		t.Run(tC.desc, func(t *testing.T) {
			conv := converter.NewConverter(
				converter.WithPlugins(
					base.NewBasePlugin(),
					commonmark.NewCommonmarkPlugin(
						tC.options...,
					),
				),
			)

			output, err := conv.ConvertString(tC.input)
			if err != nil {
				t.Error(err)
			}

			if output != tC.expected {
				t.Errorf("expected %q but got %q", tC.expected, output)
			}
		})
	}
}

func TestOptionFunc_ValidationError(t *testing.T) {
	testCases := []struct {
		desc          string
		options       []commonmark.OptionFunc
		expectedError string
	}{
		{
			desc: "WithEmDelimiter(__)",
			options: []commonmark.OptionFunc{
				commonmark.WithEmDelimiter("__"),
			},
			expectedError: `error while initializing "commonmark" plugin: invalid value for EmDelimiter:"__" must be exactly 1 character of "*" or "_"`,
		},
		{
			desc: "WithEmDelimiter(**)",
			options: []commonmark.OptionFunc{
				commonmark.WithEmDelimiter("**"),
			},
			expectedError: `error while initializing "commonmark" plugin: invalid value for EmDelimiter:"**" must be exactly 1 character of "*" or "_"`,
		},

		{
			desc: "WithStrongDelimiter(_)",
			options: []commonmark.OptionFunc{
				commonmark.WithStrongDelimiter("_"),
			},
			expectedError: `error while initializing "commonmark" plugin: invalid value for StrongDelimiter:"_" must be exactly 2 characters of "**" or "__"`,
		},
		{
			desc: "WithStrongDelimiter(*)",
			options: []commonmark.OptionFunc{
				commonmark.WithStrongDelimiter("*"),
			},
			expectedError: `error while initializing "commonmark" plugin: invalid value for StrongDelimiter:"*" must be exactly 2 characters of "**" or "__"`,
		},

		{
			desc: "WithHorizontalRule(* *)",
			options: []commonmark.OptionFunc{
				commonmark.WithHorizontalRule("* *"),
			},
			expectedError: `error while initializing "commonmark" plugin: invalid value for HorizontalRule:"* *" must be at least 3 characters of "*", "_" or "-"`,
		},
		{
			desc: "WithHorizontalRule(+++)",
			options: []commonmark.OptionFunc{
				commonmark.WithHorizontalRule("+++"),
			},
			expectedError: `error while initializing "commonmark" plugin: invalid value for HorizontalRule:"+++" must be at least 3 characters of "*", "_" or "-"`,
		},

		{
			desc: "WithBulletListMarker(_)",
			options: []commonmark.OptionFunc{
				commonmark.WithBulletListMarker("_"),
			},
			expectedError: `error while initializing "commonmark" plugin: invalid value for BulletListMarker:"_" must be one of "-", "+" or "*"`,
		},

		{
			desc: "WithCodeBlockFence(~~)",
			options: []commonmark.OptionFunc{
				commonmark.WithCodeBlockFence("~~"),
			},
			expectedError: "error while initializing \"commonmark\" plugin: invalid value for CodeBlockFence:\"~~\" must be one of \"```\" or \"~~~\"",
		},

		{
			desc: "WithHeadingStyle(ATX)",
			options: []commonmark.OptionFunc{
				commonmark.WithHeadingStyle("ATX"),
			},
			expectedError: `error while initializing "commonmark" plugin: invalid value for HeadingStyle:"ATX" must be one of "atx" or "setext"`,
		},
		{
			desc: "WithHeadingStyle(misspelling settext)",
			options: []commonmark.OptionFunc{
				commonmark.WithHeadingStyle("settext"),
			},
			expectedError: `error while initializing "commonmark" plugin: invalid value for HeadingStyle:"settext" must be one of "atx" or "setext"`,
		},
	}
	for _, tC := range testCases {
		t.Run(tC.desc, func(t *testing.T) {
			conv := converter.NewConverter(
				converter.WithPlugins(
					base.NewBasePlugin(),
					commonmark.NewCommonmarkPlugin(
						tC.options...,
					),
				),
			)

			_, err := conv.ConvertString("<strong>bold text</strong>")
			if err == nil {
				t.Fatal("expected an error but got nil")
			}

			_, isValidateConfigError := err.(*commonmark.ValidateConfigError)
			if !isValidateConfigError {
				// t.Error("the error is not of type ValidateConfigError")
			}

			actual := err.Error()
			if actual != tC.expectedError {
				t.Errorf("expected %q but got %q", tC.expectedError, actual)
			}
		})
	}
}