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-14 22:21:23 +08:00
|
|
|
func TestHTMLRender(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>`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: "> Hello\n> world!",
|
|
|
|
expected: `<blockquote>Hello<br>world!</blockquote>`,
|
|
|
|
},
|
2023-12-14 00:04:20 +08:00
|
|
|
{
|
|
|
|
text: "*Hello* world!",
|
|
|
|
expected: `<p><em>Hello</em> world!</p>`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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>`,
|
|
|
|
},
|
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-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-14 22:21:23 +08:00
|
|
|
actual := NewHTMLRender().Render(nodes)
|
2023-12-14 00:04:20 +08:00
|
|
|
require.Equal(t, test.expected, actual)
|
2023-12-13 23:50:05 +08:00
|
|
|
}
|
|
|
|
}
|