mirror of https://github.com/usememos/memos
chore: implement part of nodes
parent
dd83782522
commit
b20e0097cf
@ -1,38 +1,51 @@
|
||||
package parser
|
||||
|
||||
import "github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||
import (
|
||||
"github.com/usememos/memos/plugin/gomark/ast"
|
||||
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||
)
|
||||
|
||||
type CodeParser struct {
|
||||
Content string
|
||||
}
|
||||
type CodeParser struct{}
|
||||
|
||||
var defaultCodeParser = &CodeParser{}
|
||||
|
||||
func NewCodeParser() *CodeParser {
|
||||
return &CodeParser{}
|
||||
return defaultCodeParser
|
||||
}
|
||||
|
||||
func (*CodeParser) Match(tokens []*tokenizer.Token) *CodeParser {
|
||||
func (*CodeParser) Match(tokens []*tokenizer.Token) (int, bool) {
|
||||
if len(tokens) < 3 {
|
||||
return nil
|
||||
return 0, false
|
||||
}
|
||||
if tokens[0].Type != tokenizer.Backtick {
|
||||
return nil
|
||||
return 0, false
|
||||
}
|
||||
|
||||
content, matched := "", false
|
||||
contentTokens, matched := []*tokenizer.Token{}, false
|
||||
for _, token := range tokens[1:] {
|
||||
if token.Type == tokenizer.Newline {
|
||||
return nil
|
||||
return 0, false
|
||||
}
|
||||
if token.Type == tokenizer.Backtick {
|
||||
matched = true
|
||||
break
|
||||
}
|
||||
content += token.Value
|
||||
contentTokens = append(contentTokens, token)
|
||||
}
|
||||
if !matched || len(content) == 0 {
|
||||
if !matched || len(contentTokens) == 0 {
|
||||
return 0, false
|
||||
}
|
||||
return len(contentTokens) + 2, true
|
||||
}
|
||||
|
||||
func (p *CodeParser) Parse(tokens []*tokenizer.Token) ast.Node {
|
||||
size, ok := p.Match(tokens)
|
||||
if size == 0 || !ok {
|
||||
return nil
|
||||
}
|
||||
return &CodeParser{
|
||||
Content: content,
|
||||
|
||||
contentTokens := tokens[1 : size-1]
|
||||
return &ast.Code{
|
||||
Content: tokenizer.Stringify(contentTokens),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue