memos/plugin/gomark/parser/code_block.go

73 lines
1.8 KiB
Go
Raw Normal View History

package parser
2023-12-12 23:24:02 +08:00
import (
2024-01-27 19:09:10 +08:00
"slices"
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{}
}
2024-01-23 21:23:40 +08:00
func (*CodeBlockParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
2024-01-27 21:38:07 +08:00
rows := tokenizer.Split(tokens, tokenizer.NewLine)
2024-01-27 19:09:10 +08:00
if len(rows) < 3 {
2024-01-23 21:23:40 +08:00
return nil, 0
}
2024-01-27 19:09:10 +08:00
firstRow := rows[0]
if len(firstRow) < 3 {
2024-01-23 21:23:40 +08:00
return nil, 0
}
2024-01-27 19:09:10 +08:00
if firstRow[0].Type != tokenizer.Backtick || firstRow[1].Type != tokenizer.Backtick || firstRow[2].Type != tokenizer.Backtick {
2024-01-23 21:23:40 +08:00
return nil, 0
}
2024-01-27 19:09:10 +08:00
languageTokens := []*tokenizer.Token{}
if len(firstRow) > 3 {
languageTokens = firstRow[3:]
// Check if language is valid.
availableLanguageTokenTypes := []tokenizer.TokenType{tokenizer.Text, tokenizer.Number, tokenizer.Underscore}
for _, token := range languageTokens {
if !slices.Contains(availableLanguageTokenTypes, token.Type) {
return nil, 0
}
}
}
2024-01-27 19:09:10 +08:00
contentRows := [][]*tokenizer.Token{}
2023-12-12 23:24:02 +08:00
matched := false
2024-01-27 19:09:10 +08:00
for _, row := range rows[1:] {
if len(row) == 3 && row[0].Type == tokenizer.Backtick && row[1].Type == tokenizer.Backtick && row[2].Type == tokenizer.Backtick {
matched = true
break
}
2024-01-27 19:09:10 +08:00
contentRows = append(contentRows, row)
}
if !matched {
2024-01-23 21:23:40 +08:00
return nil, 0
}
2024-01-27 19:09:10 +08:00
contentTokens := []*tokenizer.Token{}
for index, row := range contentRows {
contentTokens = append(contentTokens, row...)
if index != len(contentRows)-1 {
contentTokens = append(contentTokens, &tokenizer.Token{
2024-01-27 21:38:07 +08:00
Type: tokenizer.NewLine,
2024-01-27 19:09:10 +08:00
Value: "\n",
})
}
2023-12-12 23:24:02 +08:00
}
2024-01-27 19:09:10 +08:00
return &ast.CodeBlock{
Content: tokenizer.Stringify(contentTokens),
Language: tokenizer.Stringify(languageTokens),
}, 4 + len(languageTokens) + len(contentTokens) + 4
}