Skip to content

Commit

Permalink
fix: merge list nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Sep 17, 2024
1 parent a60fe17 commit 3ccacc4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package parser

import (
"slices"

"github.com/usememos/gomark/ast"
"github.com/usememos/gomark/parser/tokenizer"
)
Expand Down Expand Up @@ -104,10 +106,13 @@ func mergeListItemNodes(nodes []ast.Node) []ast.Node {
}
result := []ast.Node{}
for i := 0; i < len(nodes); i++ {
var prevNode, prevResultNode ast.Node
var prevNode, nextNode, prevResultNode ast.Node
if i > 0 {
prevNode = nodes[i-1]
}
if i < len(nodes)-1 {
nextNode = nodes[i+1]
}
if len(result) > 0 {
prevResultNode = result[len(result)-1]
}
Expand All @@ -121,7 +126,11 @@ func mergeListItemNodes(nodes []ast.Node) []ast.Node {
}
prevResultNode.(*ast.List).Children = append(prevResultNode.(*ast.List).Children, nodes[i])
case *ast.LineBreak:
if prevResultNode != nil && prevResultNode.Type() == ast.ListNode && (prevNode == nil || prevNode.Type() != ast.LineBreakNode) {
if prevResultNode != nil && prevResultNode.Type() == ast.ListNode &&
// Check if the prev node is not a line break node.
(prevNode == nil || prevNode.Type() != ast.LineBreakNode) &&
// Check if the next node is a list item node.
(nextNode == nil || slices.Contains([]ast.NodeType{ast.OrderedListItemNode, ast.UnorderedListItemNode, ast.TaskListItemNode}, nextNode.Type())) {
prevResultNode.(*ast.List).Children = append(prevResultNode.(*ast.List).Children, nodes[i])
} else {
result = append(result, nodes[i])
Expand Down
2 changes: 1 addition & 1 deletion parser/tests/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ func TestParser(t *testing.T) {
},
},
},
&ast.LineBreak{},
},
},
&ast.LineBreak{},
&ast.LineBreak{},
&ast.List{
Children: []ast.Node{
&ast.OrderedListItem{
Expand Down

0 comments on commit 3ccacc4

Please sign in to comment.