Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions ast/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,20 @@ type Italic struct {
BaseInline

// Symbol is "*" or "_".
Symbol string
Content string
Symbol string
Children []Node
}

func (*Italic) Type() NodeType {
return ItalicNode
}

func (n *Italic) Restore() string {
return fmt.Sprintf("%s%s%s", n.Symbol, n.Content, n.Symbol)
content := ""
for _, child := range n.Children {
content += child.Restore()
}
return fmt.Sprintf("%s%s%s", n.Symbol, content, n.Symbol)
}

type BoldItalic struct {
Expand Down
26 changes: 17 additions & 9 deletions parser/blockquote.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (*BlockquoteParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
rows := tokenizer.Split(tokens, tokenizer.NewLine)
contentRows := [][]*tokenizer.Token{}
for _, row := range rows {
if len(row) < 3 || row[0].Type != tokenizer.GreaterThan || row[1].Type != tokenizer.Space {
if len(row) < 2 || row[0].Type != tokenizer.GreaterThan || row[1].Type != tokenizer.Space {
break
}
contentRows = append(contentRows, row)
Expand All @@ -26,17 +26,25 @@ func (*BlockquoteParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {

children := []ast.Node{}
size := 0

for index, row := range contentRows {
contentTokens := row[2:]
nodes, err := ParseBlockWithParsers(contentTokens, []BlockParser{NewBlockquoteParser(), NewParagraphParser()})
if err != nil {
return nil, 0
}
if len(nodes) != 1 {
return nil, 0
var node ast.Node
if len(contentTokens) == 0 {
node = &ast.Paragraph{
Children: []ast.Node{&ast.Text{Content: " "}},
}
} else {
nodes, err := ParseBlockWithParsers(contentTokens, []BlockParser{NewBlockquoteParser(), NewParagraphParser()})
if err != nil {
return nil, 0
}
if len(nodes) != 1 {
return nil, 0
}
node = nodes[0]
}

children = append(children, nodes[0])
children = append(children, node)
size += len(row)
if index != len(contentRows)-1 {
size++ // NewLine.
Expand Down
2 changes: 1 addition & 1 deletion parser/bold.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (*BoldParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
return nil, 0
}
prefixTokenType := prefixTokens[0].Type
if prefixTokenType != tokenizer.Asterisk {
if prefixTokenType != tokenizer.Asterisk && prefixTokenType != tokenizer.Underscore {
return nil, 0
}

Expand Down
11 changes: 8 additions & 3 deletions parser/italic.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (*ItalicParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
}

prefixTokens := matchedTokens[:1]
if prefixTokens[0].Type != tokenizer.Asterisk {
if prefixTokens[0].Type != tokenizer.Asterisk && prefixTokens[0].Type != tokenizer.Underscore {
return nil, 0
}
prefixTokenType := prefixTokens[0].Type
Expand All @@ -37,8 +37,13 @@ func (*ItalicParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
return nil, 0
}

children, err := ParseInlineWithParsers(contentTokens, []InlineParser{NewLinkParser(), NewTextParser()})
if err != nil || len(children) == 0 {
return nil, 0
}

return &ast.Italic{
Symbol: prefixTokenType,
Content: tokenizer.Stringify(contentTokens),
Symbol: prefixTokenType,
Children: children,
}, len(contentTokens) + 2
}
30 changes: 30 additions & 0 deletions parser/tests/bold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ func TestBoldParser(t *testing.T) {
text: "* * Hello **",
node: nil,
},
{
text: "__Hello__",
node: &ast.Bold{
Symbol: "_",
Children: []ast.Node{
&ast.Text{
Content: "Hello",
},
},
},
},
{
text: "__ Hello __",
node: &ast.Bold{
Symbol: "_",
Children: []ast.Node{
&ast.Text{
Content: " Hello ",
},
},
},
},
{
text: "__ Hello _ _",
node: nil,
},
{
text: "_ _ Hello __",
node: nil,
},
}

for _, test := range tests {
Expand Down
73 changes: 67 additions & 6 deletions parser/tests/italic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,83 @@ func TestItalicParser(t *testing.T) {
{
text: "*Hello*",
node: &ast.Italic{
Symbol: "*",
Content: "Hello",
Symbol: "*",
Children: []ast.Node{
&ast.Text{
Content: "Hello",
},
},
},
},
{
text: "* Hello *",
node: &ast.Italic{
Symbol: "*",
Content: " Hello ",
Symbol: "*",
Children: []ast.Node{
&ast.Text{
Content: " Hello ",
},
},
},
},
{
text: "*1* Hello * *",
node: &ast.Italic{
Symbol: "*",
Content: "1",
Symbol: "*",
Children: []ast.Node{
&ast.Text{
Content: "1",
},
},
},
},
{
text: "_Hello_",
node: &ast.Italic{
Symbol: "_",
Children: []ast.Node{
&ast.Text{
Content: "Hello",
},
},
},
},
{
text: "_ Hello _",
node: &ast.Italic{
Symbol: "_",
Children: []ast.Node{
&ast.Text{
Content: " Hello ",
},
},
},
},
{
text: "_1_ Hello _ _",
node: &ast.Italic{
Symbol: "_",
Children: []ast.Node{
&ast.Text{
Content: "1",
},
},
},
},
{
text: "*[Hello](https://example.com)*",
node: &ast.Italic{
Symbol: "*",
Children: []ast.Node{
&ast.Link{
Content: []ast.Node{
&ast.Text{
Content: "Hello",
},
},
URL: "https://example.com",
},
},
},
},
}
Expand Down
8 changes: 6 additions & 2 deletions parser/tests/ordered_list_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ func TestOrderedListItemParser(t *testing.T) {
Content: "Hello ",
},
&ast.Italic{
Symbol: "*",
Content: "World",
Symbol: "*",
Children: []ast.Node{
&ast.Text{
Content: "World",
},
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion renderer/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (r *HTMLRenderer) renderBold(node *ast.Bold) {

func (r *HTMLRenderer) renderItalic(node *ast.Italic) {
r.output.WriteString("<em>")
r.output.WriteString(node.Content)
r.RenderNodes(node.Children)
r.output.WriteString("</em>")
}

Expand Down
2 changes: 1 addition & 1 deletion renderer/string/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (r *StringRenderer) renderBold(node *ast.Bold) {
}

func (r *StringRenderer) renderItalic(node *ast.Italic) {
r.output.WriteString(node.Content)
r.RenderNodes(node.Children)
}

func (r *StringRenderer) renderBoldItalic(node *ast.BoldItalic) {
Expand Down
Loading