mirror of https://github.com/usememos/memos
feat: tag parser (#1745)
parent
dbc85fe7e4
commit
a07d11e820
@ -0,0 +1,34 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import "github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||||
|
|
||||||
|
type TagParser struct {
|
||||||
|
ContentTokens []*tokenizer.Token
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTagParser() *TagParser {
|
||||||
|
return &TagParser{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*TagParser) Match(tokens []*tokenizer.Token) *TagParser {
|
||||||
|
if len(tokens) < 2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if tokens[0].Type != tokenizer.Hash {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
contentTokens := []*tokenizer.Token{}
|
||||||
|
for _, token := range tokens[1:] {
|
||||||
|
if token.Type == tokenizer.Newline || token.Type == tokenizer.Space || token.Type == tokenizer.Hash {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
contentTokens = append(contentTokens, token)
|
||||||
|
}
|
||||||
|
if len(contentTokens) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &TagParser{
|
||||||
|
ContentTokens: contentTokens,
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTagParser(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
text string
|
||||||
|
tag *TagParser
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
text: "*Hello world",
|
||||||
|
tag: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "# Hello World",
|
||||||
|
tag: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "#tag",
|
||||||
|
tag: &TagParser{
|
||||||
|
ContentTokens: []*tokenizer.Token{
|
||||||
|
{
|
||||||
|
Type: tokenizer.Text,
|
||||||
|
Value: "tag",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "#tag/subtag",
|
||||||
|
tag: &TagParser{
|
||||||
|
ContentTokens: []*tokenizer.Token{
|
||||||
|
{
|
||||||
|
Type: tokenizer.Text,
|
||||||
|
Value: "tag/subtag",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
tokens := tokenizer.Tokenize(test.text)
|
||||||
|
require.Equal(t, test.tag, NewTagParser().Match(tokens))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue