Skip to content

Commit

Permalink
feat: add list kind
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Sep 19, 2024
1 parent e2e133b commit f5d2442
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
9 changes: 9 additions & 0 deletions ast/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 9 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 12 additions & 1 deletion parser/tests/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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",
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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))
}
}
18 changes: 16 additions & 2 deletions renderer/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,25 @@ func (r *HTMLRenderer) renderBlockquote(node *ast.Blockquote) {
}

func (r *HTMLRenderer) renderList(node *ast.List) {
r.output.WriteString("<dl>")
switch node.Kind {
case ast.OrderedList:
r.output.WriteString("<ol>")
case ast.UnorderedList:
r.output.WriteString("<ul>")
case ast.DescrpitionList:
r.output.WriteString("<dl>")
}
for _, item := range node.Children {
r.RenderNodes([]ast.Node{item})
}
r.output.WriteString("</dl>")
switch node.Kind {
case ast.OrderedList:
r.output.WriteString("</ol>")
case ast.UnorderedList:
r.output.WriteString("</ul>")
case ast.DescrpitionList:
r.output.WriteString("</dl>")
}
}

func (r *HTMLRenderer) renderUnorderedListItem(node *ast.UnorderedListItem) {
Expand Down
15 changes: 4 additions & 11 deletions renderer/html/html_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package html

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -48,19 +49,11 @@ func TestHTMLRenderer(t *testing.T) {
},
{
text: "* Hello\n* world!",
expected: `<dl><li>Hello</li><br><li>world!</li></dl>`,
},
{
text: "1. Hello\n2. world\n* !",
expected: `<dl><li>Hello</li><br><li>world</li><br><li>!</li></dl>`,
expected: `<ul><li>Hello</li><br><li>world!</li></ul>`,
},
{
text: "- [ ] hello\n- [x] world",
expected: `<dl><li><input type="checkbox" disabled />hello</li><br><li><input type="checkbox" checked disabled />world</li></dl>`,
},
{
text: "1. ordered\n* unorder\n- [ ] checkbox\n- [x] checked",
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>`,
expected: `<ul><li><input type="checkbox" disabled />hello</li><br><li><input type="checkbox" checked disabled />world</li></ul>`,
},
}

Expand All @@ -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))
}
}

0 comments on commit f5d2442

Please sign in to comment.