Skip to content

Commit f5d2442

Browse files
committed
feat: add list kind
1 parent e2e133b commit f5d2442

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

ast/block.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,18 @@ func (n *Blockquote) Restore() string {
113113
return result
114114
}
115115

116+
type ListKind string
117+
118+
const (
119+
UnorderedList ListKind = "ul"
120+
OrderedList ListKind = "ol"
121+
DescrpitionList ListKind = "dl"
122+
)
123+
116124
type List struct {
117125
BaseBlock
118126

127+
Kind ListKind
119128
Children []Node
120129
}
121130

parser/parser.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,17 @@ func mergeListItemNodes(nodes []ast.Node) []ast.Node {
118118
}
119119
switch nodes[i].(type) {
120120
case *ast.OrderedListItem, *ast.UnorderedListItem, *ast.TaskListItem:
121-
if prevResultNode == nil || prevResultNode.Type() != ast.ListNode {
121+
var listKind ast.ListKind
122+
switch nodes[i].(type) {
123+
case *ast.OrderedListItem:
124+
listKind = ast.OrderedList
125+
case *ast.UnorderedListItem, *ast.TaskListItem:
126+
listKind = ast.UnorderedList
127+
}
128+
if prevResultNode == nil || prevResultNode.Type() != ast.ListNode || prevResultNode.(*ast.List).Kind != listKind {
122129
prevResultNode = &ast.List{
123130
BaseBlock: ast.BaseBlock{},
131+
Kind: listKind,
124132
}
125133
result = append(result, prevResultNode)
126134
}

parser/tests/parser_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tests
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/require"
@@ -159,6 +160,7 @@ func TestParser(t *testing.T) {
159160
text: "1. hello\n- [ ] world",
160161
nodes: []ast.Node{
161162
&ast.List{
163+
Kind: ast.OrderedList,
162164
Children: []ast.Node{
163165
&ast.OrderedListItem{
164166
Number: "1",
@@ -169,6 +171,11 @@ func TestParser(t *testing.T) {
169171
},
170172
},
171173
&ast.LineBreak{},
174+
},
175+
},
176+
&ast.List{
177+
Kind: ast.UnorderedList,
178+
Children: []ast.Node{
172179
&ast.TaskListItem{
173180
Symbol: tokenizer.Hyphen,
174181
Complete: false,
@@ -186,6 +193,7 @@ func TestParser(t *testing.T) {
186193
text: "- [ ] hello\n- [x] world",
187194
nodes: []ast.Node{
188195
&ast.List{
196+
Kind: ast.UnorderedList,
189197
Children: []ast.Node{
190198
&ast.TaskListItem{
191199
Symbol: tokenizer.Hyphen,
@@ -289,6 +297,7 @@ func TestParser(t *testing.T) {
289297
text: "* unordered list item 1\n* unordered list item 2",
290298
nodes: []ast.Node{
291299
&ast.List{
300+
Kind: ast.UnorderedList,
292301
Children: []ast.Node{
293302
&ast.UnorderedListItem{
294303
Symbol: tokenizer.Asterisk,
@@ -315,6 +324,7 @@ func TestParser(t *testing.T) {
315324
text: "* unordered list item\n\n1. ordered list item",
316325
nodes: []ast.Node{
317326
&ast.List{
327+
Kind: ast.UnorderedList,
318328
Children: []ast.Node{
319329
&ast.UnorderedListItem{
320330
Symbol: tokenizer.Asterisk,
@@ -329,6 +339,7 @@ func TestParser(t *testing.T) {
329339
&ast.LineBreak{},
330340
&ast.LineBreak{},
331341
&ast.List{
342+
Kind: ast.OrderedList,
332343
Children: []ast.Node{
333344
&ast.OrderedListItem{
334345
Number: "1",
@@ -347,6 +358,6 @@ func TestParser(t *testing.T) {
347358
for _, test := range tests {
348359
tokens := tokenizer.Tokenize(test.text)
349360
nodes, _ := parser.Parse(tokens)
350-
require.Equal(t, test.nodes, nodes)
361+
require.Equal(t, test.nodes, nodes, fmt.Sprintf("Test case: %s", test.text))
351362
}
352363
}

renderer/html/html.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,25 @@ func (r *HTMLRenderer) renderBlockquote(node *ast.Blockquote) {
152152
}
153153

154154
func (r *HTMLRenderer) renderList(node *ast.List) {
155-
r.output.WriteString("<dl>")
155+
switch node.Kind {
156+
case ast.OrderedList:
157+
r.output.WriteString("<ol>")
158+
case ast.UnorderedList:
159+
r.output.WriteString("<ul>")
160+
case ast.DescrpitionList:
161+
r.output.WriteString("<dl>")
162+
}
156163
for _, item := range node.Children {
157164
r.RenderNodes([]ast.Node{item})
158165
}
159-
r.output.WriteString("</dl>")
166+
switch node.Kind {
167+
case ast.OrderedList:
168+
r.output.WriteString("</ol>")
169+
case ast.UnorderedList:
170+
r.output.WriteString("</ul>")
171+
case ast.DescrpitionList:
172+
r.output.WriteString("</dl>")
173+
}
160174
}
161175

162176
func (r *HTMLRenderer) renderUnorderedListItem(node *ast.UnorderedListItem) {

renderer/html/html_test.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package html
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/require"
@@ -48,19 +49,11 @@ func TestHTMLRenderer(t *testing.T) {
4849
},
4950
{
5051
text: "* Hello\n* world!",
51-
expected: `<dl><li>Hello</li><br><li>world!</li></dl>`,
52-
},
53-
{
54-
text: "1. Hello\n2. world\n* !",
55-
expected: `<dl><li>Hello</li><br><li>world</li><br><li>!</li></dl>`,
52+
expected: `<ul><li>Hello</li><br><li>world!</li></ul>`,
5653
},
5754
{
5855
text: "- [ ] hello\n- [x] world",
59-
expected: `<dl><li><input type="checkbox" disabled />hello</li><br><li><input type="checkbox" checked disabled />world</li></dl>`,
60-
},
61-
{
62-
text: "1. ordered\n* unorder\n- [ ] checkbox\n- [x] checked",
63-
expected: `<dl><li>ordered</li><br><li>unorder</li><br><li><input type="checkbox" disabled />checkbox</li><br><li><input type="checkbox" checked disabled />checked</li></dl>`,
56+
expected: `<ul><li><input type="checkbox" disabled />hello</li><br><li><input type="checkbox" checked disabled />world</li></ul>`,
6457
},
6558
}
6659

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

0 commit comments

Comments
 (0)