feat: add heading tokenizer (#1723)

This commit is contained in:
boojack
2023-05-23 19:52:31 +08:00
committed by GitHub
parent 616b8b0ee6
commit fa53a2550a
5 changed files with 191 additions and 50 deletions

View File

@ -1,6 +1,32 @@
package tokenizer
func tokenize(text string) []*Token {
type TokenType = string
const (
Underline TokenType = "_"
Star TokenType = "*"
Hash TokenType = "#"
Newline TokenType = "\n"
Space TokenType = " "
)
const (
Text TokenType = ""
)
type Token struct {
Type TokenType
Value string
}
func NewToken(tp, text string) *Token {
return &Token{
Type: tp,
Value: text,
}
}
func Tokenize(text string) []*Token {
tokens := []*Token{}
for _, c := range text {
switch c {
@ -8,6 +34,8 @@ func tokenize(text string) []*Token {
tokens = append(tokens, NewToken(Underline, "_"))
case '*':
tokens = append(tokens, NewToken(Star, "*"))
case '#':
tokens = append(tokens, NewToken(Hash, "#"))
case '\n':
tokens = append(tokens, NewToken(Newline, "\n"))
case ' ':