feat: implement code block parser (#1727)

This commit is contained in:
boojack
2023-05-24 00:31:37 +08:00
committed by GitHub
parent 42c653e1a4
commit 65890bc257
5 changed files with 191 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package parser
import "github.com/usememos/memos/plugin/gomark/parser/tokenizer"
type CodeParser struct {
Content string
}
func NewCodeParser() *CodeParser {
return &CodeParser{}
}
func (*CodeParser) Match(tokens []*tokenizer.Token) *CodeParser {
if len(tokens) < 3 {
return nil
}
if tokens[0].Type != tokenizer.Backtick {
return nil
}
content, matched := "", false
for _, token := range tokens[1:] {
if token.Type == tokenizer.Newline {
return nil
}
if token.Type == tokenizer.Backtick {
matched = true
break
}
content += token.Value
}
if !matched || len(content) == 0 {
return nil
}
return &CodeParser{
Content: content,
}
}