memos/plugin/gomark/parser/ordered_list.go

48 lines
1 KiB
Go
Raw Normal View History

2023-12-16 08:51:29 +08:00
package parser
import (
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
type OrderedListParser struct{}
func NewOrderedListParser() *OrderedListParser {
return &OrderedListParser{}
}
2024-01-23 21:27:05 +08:00
func (*OrderedListParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
2024-01-23 21:23:40 +08:00
matchedTokens := tokenizer.GetFirstLine(tokens)
2024-01-14 22:19:03 +08:00
indent := 0
2024-01-23 21:23:40 +08:00
for _, token := range matchedTokens {
2024-01-14 22:19:03 +08:00
if token.Type == tokenizer.Space {
indent++
} else {
break
}
}
2024-01-23 21:23:40 +08:00
if len(matchedTokens) < indent+3 {
return nil, 0
2023-12-16 08:51:29 +08:00
}
2024-01-23 21:23:40 +08:00
corsor := indent
if matchedTokens[corsor].Type != tokenizer.Number || matchedTokens[corsor+1].Type != tokenizer.Dot || matchedTokens[corsor+2].Type != tokenizer.Space {
return nil, 0
2023-12-16 08:51:29 +08:00
}
2024-01-23 21:23:40 +08:00
contentTokens := matchedTokens[corsor+3:]
2023-12-16 08:51:29 +08:00
if len(contentTokens) == 0 {
2024-01-23 21:23:40 +08:00
return nil, 0
2023-12-16 08:51:29 +08:00
}
children, err := ParseInline(contentTokens)
if err != nil {
2024-01-23 21:23:40 +08:00
return nil, 0
2023-12-16 08:51:29 +08:00
}
return &ast.OrderedList{
2024-01-23 21:23:40 +08:00
Number: matchedTokens[indent].Value,
2024-01-14 22:19:03 +08:00
Indent: indent,
2023-12-16 08:51:29 +08:00
Children: children,
2024-01-23 21:23:40 +08:00
}, indent + 3 + len(contentTokens)
2023-12-16 08:51:29 +08:00
}