Skip to content

Commit

Permalink
Better container collapsing and list support
Browse files Browse the repository at this point in the history
  • Loading branch information
belak committed Dec 9, 2024
1 parent 2c6da5e commit e859730
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
38 changes: 22 additions & 16 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ import (
"github.com/seabird-chat/seabird-go/pb"
)

func collapseContainers(block *pb.Block) *pb.Block {
container := block.GetContainer()
if container == nil {
return block
func maybeContainer(blocks ...*pb.Block) *pb.Block {
if len(blocks) == 1 {
return blocks[0]
}

if len(container.Inner) != 1 {
return block
}

return collapseContainers(container.Inner[0])
fmt.Println("adding container")
return seabird.NewContainerBlock(blocks...)
}

func TextToBlocks(data string) *pb.Block {
Expand Down Expand Up @@ -92,7 +88,7 @@ func TextToBlocks(data string) *pb.Block {
}
*/

return collapseContainers(seabird.NewContainerBlock(blocks...))
return maybeContainer(blocks...)
}

func nodeToBlocks(doc ast.Node, src []byte) []*pb.Block {
Expand All @@ -102,15 +98,11 @@ func nodeToBlocks(doc ast.Node, src []byte) []*pb.Block {
switch node := cur.(type) {
case *ast.Document:
fmt.Println("doc")
ret = append(ret, seabird.NewContainerBlock(
nodeToBlocks(cur.FirstChild(), src)...,
))
ret = append(ret, maybeContainer(nodeToBlocks(cur.FirstChild(), src)...))
fmt.Println("doc done")
case *ast.Paragraph:
fmt.Println("paragraph:")
ret = append(ret, seabird.NewContainerBlock(
nodeToBlocks(cur.FirstChild(), src)...,
))
ret = append(ret, maybeContainer(nodeToBlocks(cur.FirstChild(), src)...))
fmt.Println("paragraph done")
case *ast.Text:
fmt.Println("text:", string(node.Value(src)))
Expand All @@ -121,6 +113,20 @@ func nodeToBlocks(doc ast.Node, src []byte) []*pb.Block {
string(node.Destination),
nodeToBlocks(cur.FirstChild(), src)...,
))
case *ast.List:
fmt.Println("list:", node)
ret = append(ret, seabird.NewListBlock(
nodeToBlocks(cur.FirstChild(), src)...,
))
fmt.Println("list end")
case *ast.ListItem:
fmt.Println("list item")
ret = append(ret, maybeContainer(nodeToBlocks(cur.FirstChild(), src)...))
fmt.Println("list item end")
case *ast.TextBlock:
fmt.Println("text block")
ret = append(ret, maybeContainer(nodeToBlocks(cur.FirstChild(), src)...))
fmt.Println("text block end")
case *ast.Emphasis:
fmt.Println("emph", node.Level)
if node.Level == 2 {
Expand Down
26 changes: 26 additions & 0 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,34 @@ func TestTextToBlocks(t *testing.T) {
seabird.NewTextBlock("hello world"),
),
},
{
name: "list-simple",
input: "* hello\n* world",
expected: seabird.NewListBlock(
seabird.NewTextBlock("hello"),
seabird.NewTextBlock("world"),
),
},

// Complex Cases
{
name: "list-nested",
input: "* hello\n * world\n 1. ordered\n 2. list",
expected: seabird.NewListBlock(
seabird.NewContainerBlock(
seabird.NewTextBlock("hello"),
seabird.NewListBlock(
seabird.NewContainerBlock(
seabird.NewTextBlock("world"),
seabird.NewListBlock(
seabird.NewTextBlock("ordered"),
seabird.NewTextBlock("list"),
),
),
),
),
),
},
{
name: "strikethrough-complex",
input: "~a~ ~hello~ ~~~world~~~ ~~~~~asdf~~~~~",
Expand Down

0 comments on commit e859730

Please sign in to comment.