2023-05-18 21:33:18 +08:00
|
|
|
package parser
|
2023-05-23 19:52:31 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2023-12-13 09:06:47 +08:00
|
|
|
"github.com/usememos/memos/plugin/gomark/ast"
|
2023-05-23 19:52:31 +08:00
|
|
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHeadingParser(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
text string
|
2023-12-13 09:06:47 +08:00
|
|
|
heading ast.Node
|
2023-05-23 19:52:31 +08:00
|
|
|
}{
|
|
|
|
{
|
2023-05-26 08:43:37 +08:00
|
|
|
text: "*Hello world",
|
2023-05-23 19:52:31 +08:00
|
|
|
heading: nil,
|
|
|
|
},
|
|
|
|
{
|
2023-05-26 08:43:37 +08:00
|
|
|
text: "## Hello World",
|
2023-12-13 09:06:47 +08:00
|
|
|
heading: &ast.Heading{
|
2023-05-23 19:52:31 +08:00
|
|
|
Level: 2,
|
2023-12-13 09:06:47 +08:00
|
|
|
Children: []ast.Node{
|
|
|
|
&ast.Text{
|
|
|
|
Content: "Hello World",
|
2023-05-23 19:52:31 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: "# # Hello World",
|
2023-12-13 09:06:47 +08:00
|
|
|
heading: &ast.Heading{
|
2023-05-23 19:52:31 +08:00
|
|
|
Level: 1,
|
2023-12-13 09:06:47 +08:00
|
|
|
Children: []ast.Node{
|
|
|
|
&ast.Text{
|
|
|
|
Content: "# Hello World",
|
2023-05-23 19:52:31 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-05-26 08:43:37 +08:00
|
|
|
text: " # 123123 Hello World",
|
2023-05-23 19:52:31 +08:00
|
|
|
heading: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: `# 123
|
2023-05-26 08:43:37 +08:00
|
|
|
Hello World`,
|
2023-12-13 09:06:47 +08:00
|
|
|
heading: &ast.Heading{
|
2023-05-23 19:52:31 +08:00
|
|
|
Level: 1,
|
2023-12-13 09:06:47 +08:00
|
|
|
Children: []ast.Node{
|
|
|
|
&ast.Text{
|
|
|
|
Content: "123 ",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: "### **Hello** World",
|
|
|
|
heading: &ast.Heading{
|
|
|
|
Level: 3,
|
|
|
|
Children: []ast.Node{
|
|
|
|
&ast.Bold{
|
|
|
|
Symbol: "*",
|
|
|
|
Content: "Hello",
|
2023-05-23 19:52:31 +08:00
|
|
|
},
|
2023-12-13 09:06:47 +08:00
|
|
|
&ast.Text{
|
|
|
|
Content: " World",
|
2023-05-23 19:52:31 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
tokens := tokenizer.Tokenize(test.text)
|
2023-12-13 09:06:47 +08:00
|
|
|
require.Equal(t, test.heading, NewHeadingParser().Parse(tokens))
|
2023-05-23 19:52:31 +08:00
|
|
|
}
|
|
|
|
}
|