chore: handle newline in block parsers

This commit is contained in:
Steven
2023-12-14 22:55:46 +08:00
parent e0290b94b4
commit 6763dab4e5
4 changed files with 13 additions and 3 deletions

View File

@@ -36,11 +36,11 @@ func (*HeadingParser) Match(tokens []*tokenizer.Token) (int, bool) {
cursor++
contentTokens := []*tokenizer.Token{}
for _, token := range tokens[cursor:] {
contentTokens = append(contentTokens, token)
cursor++
if token.Type == tokenizer.Newline {
break
}
contentTokens = append(contentTokens, token)
cursor++
}
if len(contentTokens) == 0 {
return 0, false

View File

@@ -53,6 +53,7 @@ Hello World`,
&ast.Text{
Content: "123 ",
},
&ast.LineBreak{},
},
},
},

View File

@@ -26,8 +26,11 @@ func (*HorizontalRuleParser) Match(tokens []*tokenizer.Token) (int, bool) {
if len(tokens) > 3 && tokens[3].Type != tokenizer.Newline {
return 0, false
}
if len(tokens) == 3 {
return 3, true
}
return 4, true
}
func (p *HorizontalRuleParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
size, ok := p.Match(tokens)

View File

@@ -20,6 +20,12 @@ func TestHorizontalRuleParser(t *testing.T) {
Symbol: "-",
},
},
{
text: "---\naaa",
horizontalRule: &ast.HorizontalRule{
Symbol: "-",
},
},
{
text: "****",
horizontalRule: nil,