feat: implement subscript and superscript parsers

This commit is contained in:
Steven 2024-01-19 23:06:22 +08:00
parent 1f5899d238
commit 7236552b6c
8 changed files with 235 additions and 0 deletions

View File

@ -30,6 +30,8 @@ const (
EscapingCharacterNode
MathNode
HighlightNode
SubscriptNode
SuperscriptNode
)
type Node interface {

View File

@ -205,3 +205,31 @@ func (*Highlight) Type() NodeType {
func (n *Highlight) Restore() string {
return fmt.Sprintf("==%s==", n.Content)
}
type Subscript struct {
BaseInline
Content string
}
func (*Subscript) Type() NodeType {
return SubscriptNode
}
func (n *Subscript) Restore() string {
return fmt.Sprintf("~%s~", n.Content)
}
type Superscript struct {
BaseInline
Content string
}
func (*Superscript) Type() NodeType {
return SuperscriptNode
}
func (n *Superscript) Restore() string {
return fmt.Sprintf("^%s^", n.Content)
}

View File

@ -83,6 +83,8 @@ var defaultInlineParsers = []InlineParser{
NewItalicParser(),
NewHighlightParser(),
NewCodeParser(),
NewSubscriptParser(),
NewSuperscriptParser(),
NewMathParser(),
NewTagParser(),
NewStrikethroughParser(),

View File

@ -0,0 +1,53 @@
package parser
import (
"errors"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
type SubscriptParser struct{}
func NewSubscriptParser() *SubscriptParser {
return &SubscriptParser{}
}
func (*SubscriptParser) Match(tokens []*tokenizer.Token) (int, bool) {
if len(tokens) < 3 {
return 0, false
}
if tokens[0].Type != tokenizer.Tilde {
return 0, false
}
contentTokens := []*tokenizer.Token{}
matched := false
for _, token := range tokens[1:] {
if token.Type == tokenizer.Newline {
return 0, false
}
if token.Type == tokenizer.Tilde {
matched = true
break
}
contentTokens = append(contentTokens, token)
}
if !matched || len(contentTokens) == 0 {
return 0, false
}
return len(contentTokens) + 2, true
}
func (p *SubscriptParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
size, ok := p.Match(tokens)
if size == 0 || !ok {
return nil, errors.New("not matched")
}
contentTokens := tokens[1 : size-1]
return &ast.Subscript{
Content: tokenizer.Stringify(contentTokens),
}, nil
}

View File

@ -0,0 +1,47 @@
package parser
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
"github.com/usememos/memos/plugin/gomark/restore"
)
func TestSubscriptParser(t *testing.T) {
tests := []struct {
text string
subscript ast.Node
}{
{
text: "~Hello world!",
subscript: nil,
},
{
text: "~Hello~",
subscript: &ast.Subscript{
Content: "Hello",
},
},
{
text: "~ Hello ~",
subscript: &ast.Subscript{
Content: " Hello ",
},
},
{
text: "~1~ Hello ~ ~",
subscript: &ast.Subscript{
Content: "1",
},
},
}
for _, test := range tests {
tokens := tokenizer.Tokenize(test.text)
node, _ := NewSubscriptParser().Parse(tokens)
require.Equal(t, restore.Restore([]ast.Node{test.subscript}), restore.Restore([]ast.Node{node}))
}
}

View File

@ -0,0 +1,53 @@
package parser
import (
"errors"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
type SuperscriptParser struct{}
func NewSuperscriptParser() *SuperscriptParser {
return &SuperscriptParser{}
}
func (*SuperscriptParser) Match(tokens []*tokenizer.Token) (int, bool) {
if len(tokens) < 3 {
return 0, false
}
if tokens[0].Type != tokenizer.Caret {
return 0, false
}
contentTokens := []*tokenizer.Token{}
matched := false
for _, token := range tokens[1:] {
if token.Type == tokenizer.Newline {
return 0, false
}
if token.Type == tokenizer.Caret {
matched = true
break
}
contentTokens = append(contentTokens, token)
}
if !matched || len(contentTokens) == 0 {
return 0, false
}
return len(contentTokens) + 2, true
}
func (p *SuperscriptParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
size, ok := p.Match(tokens)
if size == 0 || !ok {
return nil, errors.New("not matched")
}
contentTokens := tokens[1 : size-1]
return &ast.Superscript{
Content: tokenizer.Stringify(contentTokens),
}, nil
}

View File

@ -0,0 +1,47 @@
package parser
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
"github.com/usememos/memos/plugin/gomark/restore"
)
func TestSuperscriptParser(t *testing.T) {
tests := []struct {
text string
superscript ast.Node
}{
{
text: "^Hello world!",
superscript: nil,
},
{
text: "^Hello^",
superscript: &ast.Superscript{
Content: "Hello",
},
},
{
text: "^ Hello ^",
superscript: &ast.Superscript{
Content: " Hello ",
},
},
{
text: "^1^ Hello ^ ^",
superscript: &ast.Superscript{
Content: "1",
},
},
}
for _, test := range tests {
tokens := tokenizer.Tokenize(test.text)
node, _ := NewSuperscriptParser().Parse(tokens)
require.Equal(t, restore.Restore([]ast.Node{test.superscript}), restore.Restore([]ast.Node{node}))
}
}

View File

@ -22,6 +22,7 @@ const (
EqualSign TokenType = "="
Pipe TokenType = "|"
Colon TokenType = ":"
Caret TokenType = "^"
Backslash TokenType = "\\"
Newline TokenType = "\n"
Space TokenType = " "
@ -86,6 +87,8 @@ func Tokenize(text string) []*Token {
tokens = append(tokens, NewToken(Pipe, "|"))
case ':':
tokens = append(tokens, NewToken(Colon, ":"))
case '^':
tokens = append(tokens, NewToken(Caret, "^"))
case '\\':
tokens = append(tokens, NewToken(Backslash, `\`))
case '\n':