75 lines
1.9 KiB
Go
Raw Normal View History

2023-12-13 23:50:05 +08:00
package html
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/plugin/gomark/parser"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
2023-12-16 11:57:36 +08:00
func TestHTMLRenderer(t *testing.T) {
2023-12-13 23:50:05 +08:00
tests := []struct {
text string
expected string
}{
{
text: "Hello world!",
expected: `<p>Hello world!</p>`,
},
2023-12-16 09:12:55 +08:00
{
text: "# Hello world!",
expected: `<h1>Hello world!</h1>`,
},
2023-12-13 23:50:05 +08:00
{
text: "> Hello\n> world!",
2023-12-16 11:34:55 +08:00
expected: `<blockquote><p>Hello</p><p>world!</p></blockquote>`,
2023-12-13 23:50:05 +08:00
},
2023-12-14 00:04:20 +08:00
{
text: "*Hello* world!",
expected: `<p><em>Hello</em> world!</p>`,
},
2023-12-16 11:34:55 +08:00
{
text: "Hello world!\n\nNew paragraph.",
expected: "<p>Hello world!</p><br><p>New paragraph.</p>",
},
2023-12-14 00:04:20 +08:00
{
text: "**Hello** world!",
expected: `<p><strong>Hello</strong> world!</p>`,
},
2023-12-16 08:51:29 +08:00
{
text: "#article #memo",
expected: `<p><span>#article</span> <span>#memo</span></p>`,
},
{
text: "#article \\#memo",
expected: `<p><span>#article</span> \#memo</p>`,
},
2023-12-16 09:01:19 +08:00
{
text: "* Hello\n* world!",
expected: `<ul><li>Hello</li><li>world!</li></ul>`,
},
{
text: "1. Hello\n2. world\n* !",
expected: `<ol><li>Hello</li><li>world</li></ol><ul><li>!</li></ul>`,
},
2023-12-16 12:48:52 +08:00
{
text: "- [ ] hello\n- [x] world",
expected: `<ul><li><input type="checkbox" disabled>hello</li><li><input type="checkbox" checked disabled>world</li></ul>`,
},
{
text: "1. ordered\n* unorder\n- [ ] checkbox\n- [x] checked",
expected: `<ol><li>ordered</li></ol><ul><li>unorder</li></ul><ul><li><input type="checkbox" disabled>checkbox</li><li><input type="checkbox" checked disabled>checked</li></ul>`,
},
2023-12-13 23:50:05 +08:00
}
for _, test := range tests {
tokens := tokenizer.Tokenize(test.text)
nodes, err := parser.Parse(tokens)
require.NoError(t, err)
2023-12-16 11:57:36 +08:00
actual := NewHTMLRenderer().Render(nodes)
2023-12-14 00:04:20 +08:00
require.Equal(t, test.expected, actual)
2023-12-13 23:50:05 +08:00
}
}