From 9aca881d9121d2381c1ac1d3c37435cfb7a3e2ad Mon Sep 17 00:00:00 2001 From: johnnyjoy Date: Sat, 28 Sep 2024 21:41:59 +0800 Subject: [PATCH] fix: merge list nodes --- parser/parser.go | 2 ++ parser/tests/list_test.go | 60 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/parser/parser.go b/parser/parser.go index d333e4c..e27999e 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -151,6 +151,8 @@ func mergeListItemNodes(nodes []ast.Node) []ast.Node { } } else { result = append(result, node) + // Reset the stack if the current node is not a list item node. + stack = nil } } diff --git a/parser/tests/list_test.go b/parser/tests/list_test.go index 4dc8934..5ed245b 100644 --- a/parser/tests/list_test.go +++ b/parser/tests/list_test.go @@ -16,6 +16,26 @@ func TestListParser(t *testing.T) { text string nodes []ast.Node }{ + { + text: "1. hello\n\n", + nodes: []ast.Node{ + &ast.List{ + Kind: ast.OrderedList, + Children: []ast.Node{ + &ast.OrderedListItem{ + Number: "1", + Children: []ast.Node{ + &ast.Text{ + Content: "hello", + }, + }, + }, + &ast.LineBreak{}, + &ast.LineBreak{}, + }, + }, + }, + }, { text: "1. hello\n2. world", nodes: []ast.Node{ @@ -164,6 +184,46 @@ func TestListParser(t *testing.T) { }, }, }, + { + text: "* hello\nparagraph\n* world", + nodes: []ast.Node{ + &ast.List{ + Kind: ast.UnorderedList, + Children: []ast.Node{ + &ast.UnorderedListItem{ + Symbol: "*", + Children: []ast.Node{ + &ast.Text{ + Content: "hello", + }, + }, + }, + &ast.LineBreak{}, + }, + }, + &ast.Paragraph{ + Children: []ast.Node{ + &ast.Text{ + Content: "paragraph", + }, + }, + }, + &ast.LineBreak{}, + &ast.List{ + Kind: ast.UnorderedList, + Children: []ast.Node{ + &ast.UnorderedListItem{ + Symbol: "*", + Children: []ast.Node{ + &ast.Text{ + Content: "world", + }, + }, + }, + }, + }, + }, + }, } for _, test := range tests {