memos/plugin/gomark/parser/paragraph.go
2023-12-12 23:24:02 +08:00

46 lines
1,014 B
Go

package parser
import (
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
type ParagraphParser struct {
ContentTokens []*tokenizer.Token
}
var defaultParagraphParser = &ParagraphParser{}
func NewParagraphParser() *ParagraphParser {
return defaultParagraphParser
}
func (*ParagraphParser) Match(tokens []*tokenizer.Token) (int, bool) {
contentTokens := []*tokenizer.Token{}
cursor := 0
for ; cursor < len(tokens); cursor++ {
token := tokens[cursor]
if token.Type == tokenizer.Newline {
break
}
contentTokens = append(contentTokens, token)
}
if len(contentTokens) == 0 {
return 0, false
}
return len(contentTokens), true
}
func (p *ParagraphParser) Parse(tokens []*tokenizer.Token) ast.Node {
size, ok := p.Match(tokens)
if size == 0 || !ok {
return nil
}
contentTokens := tokens[:size]
children := ParseInline(contentTokens, []InlineParser{
NewBoldParser(),
NewTextParser(),
})
return ast.NewParagraph(children)
}