memos/plugin/gomark/parser/paragraph.go

47 lines
1,014 B
Go
Raw Normal View History

package parser
2023-12-12 23:24:02 +08:00
import (
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
type ParagraphParser struct {
ContentTokens []*tokenizer.Token
}
2023-12-12 23:24:02 +08:00
var defaultParagraphParser = &ParagraphParser{}
func NewParagraphParser() *ParagraphParser {
2023-12-12 23:24:02 +08:00
return defaultParagraphParser
}
2023-12-12 23:24:02 +08:00
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 {
2023-12-12 23:24:02 +08:00
return 0, false
}
2023-12-12 23:24:02 +08:00
return len(contentTokens), true
}
2023-12-12 23:24:02 +08:00
func (p *ParagraphParser) Parse(tokens []*tokenizer.Token) ast.Node {
size, ok := p.Match(tokens)
if size == 0 || !ok {
return nil
}
2023-12-12 23:24:02 +08:00
contentTokens := tokens[:size]
children := ParseInline(contentTokens, []InlineParser{
NewBoldParser(),
NewTextParser(),
})
return ast.NewParagraph(children)
}