diff --git a/ast/block.go b/ast/block.go index 62b91af..14116c0 100644 --- a/ast/block.go +++ b/ast/block.go @@ -113,9 +113,18 @@ func (n *Blockquote) Restore() string { return result } +type ListKind string + +const ( + UnorderedList ListKind = "ul" + OrderedList ListKind = "ol" + DescrpitionList ListKind = "dl" +) + type List struct { BaseBlock + Kind ListKind Children []Node } diff --git a/parser/parser.go b/parser/parser.go index b1ee93a..b412299 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -118,9 +118,17 @@ func mergeListItemNodes(nodes []ast.Node) []ast.Node { } switch nodes[i].(type) { case *ast.OrderedListItem, *ast.UnorderedListItem, *ast.TaskListItem: - if prevResultNode == nil || prevResultNode.Type() != ast.ListNode { + var listKind ast.ListKind + switch nodes[i].(type) { + case *ast.OrderedListItem: + listKind = ast.OrderedList + case *ast.UnorderedListItem, *ast.TaskListItem: + listKind = ast.UnorderedList + } + if prevResultNode == nil || prevResultNode.Type() != ast.ListNode || prevResultNode.(*ast.List).Kind != listKind { prevResultNode = &ast.List{ BaseBlock: ast.BaseBlock{}, + Kind: listKind, } result = append(result, prevResultNode) } diff --git a/parser/tests/parser_test.go b/parser/tests/parser_test.go index 0ff2f43..e0902b8 100644 --- a/parser/tests/parser_test.go +++ b/parser/tests/parser_test.go @@ -1,6 +1,7 @@ package tests import ( + "fmt" "testing" "github.com/stretchr/testify/require" @@ -159,6 +160,7 @@ func TestParser(t *testing.T) { text: "1. hello\n- [ ] world", nodes: []ast.Node{ &ast.List{ + Kind: ast.OrderedList, Children: []ast.Node{ &ast.OrderedListItem{ Number: "1", @@ -169,6 +171,11 @@ func TestParser(t *testing.T) { }, }, &ast.LineBreak{}, + }, + }, + &ast.List{ + Kind: ast.UnorderedList, + Children: []ast.Node{ &ast.TaskListItem{ Symbol: tokenizer.Hyphen, Complete: false, @@ -186,6 +193,7 @@ func TestParser(t *testing.T) { text: "- [ ] hello\n- [x] world", nodes: []ast.Node{ &ast.List{ + Kind: ast.UnorderedList, Children: []ast.Node{ &ast.TaskListItem{ Symbol: tokenizer.Hyphen, @@ -289,6 +297,7 @@ func TestParser(t *testing.T) { text: "* unordered list item 1\n* unordered list item 2", nodes: []ast.Node{ &ast.List{ + Kind: ast.UnorderedList, Children: []ast.Node{ &ast.UnorderedListItem{ Symbol: tokenizer.Asterisk, @@ -315,6 +324,7 @@ func TestParser(t *testing.T) { text: "* unordered list item\n\n1. ordered list item", nodes: []ast.Node{ &ast.List{ + Kind: ast.UnorderedList, Children: []ast.Node{ &ast.UnorderedListItem{ Symbol: tokenizer.Asterisk, @@ -329,6 +339,7 @@ func TestParser(t *testing.T) { &ast.LineBreak{}, &ast.LineBreak{}, &ast.List{ + Kind: ast.OrderedList, Children: []ast.Node{ &ast.OrderedListItem{ Number: "1", @@ -347,6 +358,6 @@ func TestParser(t *testing.T) { for _, test := range tests { tokens := tokenizer.Tokenize(test.text) nodes, _ := parser.Parse(tokens) - require.Equal(t, test.nodes, nodes) + require.Equal(t, test.nodes, nodes, fmt.Sprintf("Test case: %s", test.text)) } } diff --git a/renderer/html/html.go b/renderer/html/html.go index d2d318b..cae6534 100644 --- a/renderer/html/html.go +++ b/renderer/html/html.go @@ -152,11 +152,25 @@ func (r *HTMLRenderer) renderBlockquote(node *ast.Blockquote) { } func (r *HTMLRenderer) renderList(node *ast.List) { - r.output.WriteString("
") + switch node.Kind { + case ast.OrderedList: + r.output.WriteString("
    ") + case ast.UnorderedList: + r.output.WriteString("
") + case ast.UnorderedList: + r.output.WriteString("") + case ast.DescrpitionList: + r.output.WriteString("
") + } } func (r *HTMLRenderer) renderUnorderedListItem(node *ast.UnorderedListItem) { diff --git a/renderer/html/html_test.go b/renderer/html/html_test.go index 9052ab9..6eb4363 100644 --- a/renderer/html/html_test.go +++ b/renderer/html/html_test.go @@ -1,6 +1,7 @@ package html import ( + "fmt" "testing" "github.com/stretchr/testify/require" @@ -48,19 +49,11 @@ func TestHTMLRenderer(t *testing.T) { }, { text: "* Hello\n* world!", - expected: `
  • Hello

  • world!
  • `, - }, - { - text: "1. Hello\n2. world\n* !", - expected: `
  • Hello

  • world

  • !
  • `, + expected: ``, }, { text: "- [ ] hello\n- [x] world", - expected: `
  • hello

  • world
  • `, - }, - { - text: "1. ordered\n* unorder\n- [ ] checkbox\n- [x] checked", - expected: `
  • ordered

  • unorder

  • checkbox

  • checked
  • `, + expected: ``, }, } @@ -69,6 +62,6 @@ func TestHTMLRenderer(t *testing.T) { nodes, err := parser.Parse(tokens) require.NoError(t, err) actual := NewHTMLRenderer().Render(nodes) - require.Equal(t, test.expected, actual) + require.Equal(t, test.expected, actual, fmt.Sprintf("Test case: %s", test.text)) } }