mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: implement escaping character node
This commit is contained in:
41
plugin/gomark/parser/escaping_character.go
Normal file
41
plugin/gomark/parser/escaping_character.go
Normal file
@ -0,0 +1,41 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/usememos/memos/plugin/gomark/ast"
|
||||
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||
)
|
||||
|
||||
type EscapingCharacterParser struct{}
|
||||
|
||||
func NewEscapingCharacterParser() *EscapingCharacterParser {
|
||||
return &EscapingCharacterParser{}
|
||||
}
|
||||
|
||||
func (*EscapingCharacterParser) Match(tokens []*tokenizer.Token) (int, bool) {
|
||||
if len(tokens) == 0 {
|
||||
return 0, false
|
||||
}
|
||||
if tokens[0].Type != tokenizer.Backslash {
|
||||
return 0, false
|
||||
}
|
||||
if len(tokens) == 1 {
|
||||
return 0, false
|
||||
}
|
||||
if tokens[1].Type == tokenizer.Newline || tokens[1].Type == tokenizer.Space || tokens[1].Type == tokenizer.Text || tokens[1].Type == tokenizer.Number {
|
||||
return 0, false
|
||||
}
|
||||
return 2, true
|
||||
}
|
||||
|
||||
func (p *EscapingCharacterParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
|
||||
size, ok := p.Match(tokens)
|
||||
if size == 0 || !ok {
|
||||
return nil, errors.New("not matched")
|
||||
}
|
||||
|
||||
return &ast.EscapingCharacter{
|
||||
Symbol: tokens[1].Value,
|
||||
}, nil
|
||||
}
|
Reference in New Issue
Block a user