mirror of https://github.com/usememos/memos
chore: implement task list parser
parent
1c7fb77e05
commit
bb42042db4
@ -0,0 +1,69 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/usememos/memos/plugin/gomark/ast"
|
||||
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||
)
|
||||
|
||||
type TaskListParser struct{}
|
||||
|
||||
func NewTaskListParser() *TaskListParser {
|
||||
return &TaskListParser{}
|
||||
}
|
||||
|
||||
func (*TaskListParser) Match(tokens []*tokenizer.Token) (int, bool) {
|
||||
if len(tokens) < 7 {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
symbolToken := tokens[0]
|
||||
if symbolToken.Type != tokenizer.Hyphen && symbolToken.Type != tokenizer.Asterisk && symbolToken.Type != tokenizer.PlusSign {
|
||||
return 0, false
|
||||
}
|
||||
if tokens[1].Type != tokenizer.Space {
|
||||
return 0, false
|
||||
}
|
||||
if tokens[2].Type != tokenizer.LeftSquareBracket || (tokens[3].Type != tokenizer.Space && tokens[3].Value != "x") || tokens[4].Type != tokenizer.RightSquareBracket {
|
||||
return 0, false
|
||||
}
|
||||
if tokens[5].Type != tokenizer.Space {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
contentTokens := []*tokenizer.Token{}
|
||||
for _, token := range tokens[6:] {
|
||||
contentTokens = append(contentTokens, token)
|
||||
if token.Type == tokenizer.Newline {
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(contentTokens) == 0 {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return len(contentTokens) + 6, true
|
||||
}
|
||||
|
||||
func (p *TaskListParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
|
||||
size, ok := p.Match(tokens)
|
||||
if size == 0 || !ok {
|
||||
return nil, errors.New("not matched")
|
||||
}
|
||||
|
||||
symbolToken := tokens[0]
|
||||
contentTokens := tokens[6:size]
|
||||
if contentTokens[len(contentTokens)-1].Type == tokenizer.Newline {
|
||||
contentTokens = contentTokens[:len(contentTokens)-1]
|
||||
}
|
||||
children, err := ParseInline(contentTokens)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ast.TaskList{
|
||||
Symbol: symbolToken.Type,
|
||||
Complete: tokens[3].Value == "x",
|
||||
Children: children,
|
||||
}, nil
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/usememos/memos/plugin/gomark/ast"
|
||||
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||
)
|
||||
|
||||
func TestTaskListParser(t *testing.T) {
|
||||
tests := []struct {
|
||||
text string
|
||||
node ast.Node
|
||||
}{
|
||||
{
|
||||
text: "*asd",
|
||||
node: nil,
|
||||
},
|
||||
{
|
||||
text: "+ [ ] Hello World",
|
||||
node: &ast.TaskList{
|
||||
Symbol: tokenizer.PlusSign,
|
||||
Complete: false,
|
||||
Children: []ast.Node{
|
||||
&ast.Text{
|
||||
Content: "Hello World",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
text: "* [x] **Hello**",
|
||||
node: &ast.TaskList{
|
||||
Symbol: tokenizer.Asterisk,
|
||||
Complete: true,
|
||||
Children: []ast.Node{
|
||||
&ast.Bold{
|
||||
Symbol: "*",
|
||||
Children: []ast.Node{
|
||||
&ast.Text{
|
||||
Content: "Hello",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
tokens := tokenizer.Tokenize(test.text)
|
||||
node, _ := NewTaskListParser().Parse(tokens)
|
||||
require.Equal(t, StringifyNodes([]ast.Node{test.node}), StringifyNodes([]ast.Node{node}))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue