2023-05-23 15:49:32 +03:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BoldParser struct {
|
|
|
|
ContentTokens []*tokenizer.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBoldParser() *BoldParser {
|
|
|
|
return &BoldParser{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*BoldParser) Match(tokens []*tokenizer.Token) *BoldParser {
|
|
|
|
if len(tokens) < 5 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixTokens := tokens[:2]
|
2023-05-23 16:11:01 +03:00
|
|
|
if prefixTokens[0].Type != prefixTokens[1].Type {
|
2023-05-23 15:49:32 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
prefixTokenType := prefixTokens[0].Type
|
2023-05-23 16:11:01 +03:00
|
|
|
if prefixTokenType != tokenizer.Star && prefixTokenType != tokenizer.Underline {
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-23 15:49:32 +03:00
|
|
|
|
|
|
|
contentTokens := []*tokenizer.Token{}
|
2023-05-23 16:11:01 +03:00
|
|
|
cursor, matched := 2, false
|
2023-05-23 15:49:32 +03:00
|
|
|
for ; cursor < len(tokens)-1; cursor++ {
|
|
|
|
token, nextToken := tokens[cursor], tokens[cursor+1]
|
|
|
|
if token.Type == tokenizer.Newline || nextToken.Type == tokenizer.Newline {
|
2023-05-23 16:11:01 +03:00
|
|
|
return nil
|
2023-05-23 15:49:32 +03:00
|
|
|
}
|
|
|
|
if token.Type == prefixTokenType && nextToken.Type == prefixTokenType {
|
2023-05-23 16:11:01 +03:00
|
|
|
matched = true
|
2023-05-23 15:49:32 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
contentTokens = append(contentTokens, token)
|
|
|
|
}
|
2023-05-23 16:11:01 +03:00
|
|
|
if !matched {
|
2023-05-23 15:49:32 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BoldParser{
|
|
|
|
ContentTokens: contentTokens,
|
|
|
|
}
|
|
|
|
}
|