mirror of
				https://github.com/usememos/memos.git
				synced 2025-10-28 07:19:08 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			58 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			58 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package parser
 | |
| 
 | |
| import (
 | |
| 	"errors"
 | |
| 
 | |
| 	"github.com/usememos/memos/plugin/gomark/ast"
 | |
| 	"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
 | |
| )
 | |
| 
 | |
| type UnorderedListParser struct{}
 | |
| 
 | |
| func NewUnorderedListParser() *UnorderedListParser {
 | |
| 	return &UnorderedListParser{}
 | |
| }
 | |
| 
 | |
| func (*UnorderedListParser) Match(tokens []*tokenizer.Token) (int, bool) {
 | |
| 	if len(tokens) < 3 {
 | |
| 		return 0, false
 | |
| 	}
 | |
| 	symbolToken := tokens[0]
 | |
| 	if (symbolToken.Type != tokenizer.Hyphen && symbolToken.Type != tokenizer.Asterisk && symbolToken.Type != tokenizer.PlusSign) || tokens[1].Type != tokenizer.Space {
 | |
| 		return 0, false
 | |
| 	}
 | |
| 
 | |
| 	contentTokens := []*tokenizer.Token{}
 | |
| 	for _, token := range tokens[2:] {
 | |
| 		contentTokens = append(contentTokens, token)
 | |
| 		if token.Type == tokenizer.Newline {
 | |
| 			break
 | |
| 		}
 | |
| 	}
 | |
| 	if len(contentTokens) == 0 {
 | |
| 		return 0, false
 | |
| 	}
 | |
| 
 | |
| 	return len(contentTokens) + 2, true
 | |
| }
 | |
| 
 | |
| func (p *UnorderedListParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
 | |
| 	size, ok := p.Match(tokens)
 | |
| 	if size == 0 || !ok {
 | |
| 		return nil, errors.New("not matched")
 | |
| 	}
 | |
| 
 | |
| 	symbolToken := tokens[0]
 | |
| 	contentTokens := tokens[2:size]
 | |
| 	if contentTokens[len(contentTokens)-1].Type == tokenizer.Newline {
 | |
| 		contentTokens = contentTokens[:len(contentTokens)-1]
 | |
| 	}
 | |
| 	children, err := ParseInline(contentTokens)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return &ast.UnorderedList{
 | |
| 		Symbol:   symbolToken.Type,
 | |
| 		Children: children,
 | |
| 	}, nil
 | |
| }
 |