memos/plugin/gomark/parser/code_block.go

80 lines
1.8 KiB
Go
Raw Normal View History

package parser
2023-12-12 23:24:02 +08:00
import (
2023-12-13 23:50:05 +08:00
"errors"
2023-12-12 23:24:02 +08:00
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
type CodeBlockParser struct {
Language string
Content string
}
func NewCodeBlockParser() *CodeBlockParser {
2023-12-13 21:00:13 +08:00
return &CodeBlockParser{}
}
2023-12-12 23:24:02 +08:00
func (*CodeBlockParser) Match(tokens []*tokenizer.Token) (int, bool) {
if len(tokens) < 9 {
2023-12-12 23:24:02 +08:00
return 0, false
}
if tokens[0].Type != tokenizer.Backtick || tokens[1].Type != tokenizer.Backtick || tokens[2].Type != tokenizer.Backtick {
2023-12-12 23:24:02 +08:00
return 0, false
}
if tokens[3].Type != tokenizer.Newline && tokens[4].Type != tokenizer.Newline {
2023-12-12 23:24:02 +08:00
return 0, false
}
2023-12-12 23:24:02 +08:00
cursor := 4
if tokens[3].Type != tokenizer.Newline {
cursor = 5
}
2023-12-12 23:24:02 +08:00
matched := false
for ; cursor < len(tokens)-3; cursor++ {
if tokens[cursor].Type == tokenizer.Newline && tokens[cursor+1].Type == tokenizer.Backtick && tokens[cursor+2].Type == tokenizer.Backtick && tokens[cursor+3].Type == tokenizer.Backtick {
if cursor+3 == len(tokens)-1 {
2023-12-12 23:24:02 +08:00
cursor += 4
matched = true
break
} else if tokens[cursor+4].Type == tokenizer.Newline {
2023-12-12 23:24:02 +08:00
cursor += 5
matched = true
break
}
}
}
if !matched {
2023-12-12 23:24:02 +08:00
return 0, false
}
return cursor, true
}
2023-12-13 23:50:05 +08:00
func (p *CodeBlockParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
2023-12-12 23:24:02 +08:00
size, ok := p.Match(tokens)
if size == 0 || !ok {
2023-12-13 23:50:05 +08:00
return nil, errors.New("not matched")
}
2023-12-12 23:24:02 +08:00
languageToken := tokens[3]
contentStart, contentEnd := 5, size-4
if languageToken.Type == tokenizer.Newline {
languageToken = nil
contentStart = 4
}
if tokens[size-1].Type == tokenizer.Newline {
contentEnd = size - 5
}
codeBlock := &ast.CodeBlock{
Content: tokenizer.Stringify(tokens[contentStart:contentEnd]),
}
if languageToken != nil {
codeBlock.Language = languageToken.String()
}
2023-12-13 23:50:05 +08:00
return codeBlock, nil
}