bold.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package parser
  2. import (
  3. "github.com/usememos/memos/plugin/gomark/parser/tokenizer"
  4. )
  5. type BoldParser struct {
  6. ContentTokens []*tokenizer.Token
  7. }
  8. func NewBoldParser() *BoldParser {
  9. return &BoldParser{}
  10. }
  11. func (*BoldParser) Match(tokens []*tokenizer.Token) *BoldParser {
  12. if len(tokens) < 5 {
  13. return nil
  14. }
  15. prefixTokens := tokens[:2]
  16. if prefixTokens[0].Type != prefixTokens[1].Type {
  17. return nil
  18. }
  19. prefixTokenType := prefixTokens[0].Type
  20. if prefixTokenType != tokenizer.Star && prefixTokenType != tokenizer.Underline {
  21. return nil
  22. }
  23. contentTokens := []*tokenizer.Token{}
  24. cursor, matched := 2, false
  25. for ; cursor < len(tokens)-1; cursor++ {
  26. token, nextToken := tokens[cursor], tokens[cursor+1]
  27. if token.Type == tokenizer.Newline || nextToken.Type == tokenizer.Newline {
  28. return nil
  29. }
  30. if token.Type == prefixTokenType && nextToken.Type == prefixTokenType {
  31. matched = true
  32. break
  33. }
  34. contentTokens = append(contentTokens, token)
  35. }
  36. if !matched {
  37. return nil
  38. }
  39. return &BoldParser{
  40. ContentTokens: contentTokens,
  41. }
  42. }