Skip to content

Commit

Permalink
bug(parser): failure to convert nested elements (#740)
Browse files Browse the repository at this point in the history
Fixes #739
  • Loading branch information
gdamore authored Aug 2, 2020
1 parent 70a1ec4 commit 6f07b44
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 9 deletions.
34 changes: 25 additions & 9 deletions pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,17 +499,33 @@ func normalParagraph(_ ...Option) paragraphSubstitution {
}
}

func serializeParagraph(lines []interface{}) (io.Reader, error) {
buf := strings.Builder{}
for i, line := range lines {
if r, ok := line.(types.RawLine); ok {
buf.WriteString(r.Content)
if i < len(lines)-1 {
buf.WriteString("\n")
func serializeRawLines(w *strings.Builder, line interface{}) error {
if line == nil {
return nil
}
switch line := line.(type) {
case types.RawLine:
if w.Len() > 0 {
w.WriteString("\n")
}
w.WriteString(line.Content)
case []interface{}:
for _, line := range line {
if err := serializeRawLines(w, line); err != nil {
return err
}
} else {
return nil, fmt.Errorf("unexpected type of element while serializing a paragraph: '%T'", line)
}
default:
return fmt.Errorf("unexpected element type serializing raw lines: '%T'", line)
}
return nil
}

func serializeParagraph(lines []interface{}) (io.Reader, error) {
buf := &strings.Builder{}
err := serializeRawLines(buf, lines)
if err != nil {
return nil, err
}
return strings.NewReader(buf.String()), nil
}
86 changes: 86 additions & 0 deletions pkg/parser/paragraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,5 +856,91 @@ a foo image:foo.png[]`
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
})

Context("list paragraphs", func() {

It("inline image within a quote", func() {
source := `item::
This is the first line of the first paragraph.
This is the second line of the first paragraph.
+
This is the first line of the continuation paragraph.
This is the second line of the continuation paragraph.
+
This is the next continuation paragraph.
+
TIP: We can embed admonitions too!
`
expected := types.Document{
Elements: []interface{}{
types.LabeledList{
Items: []types.LabeledListItem{
{
Level: 1,
Term: []interface{}{
types.StringElement{
Content: "item",
},
},
Elements: []interface{}{
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "This is the first line of the first paragraph.",
},
},
[]interface{}{
types.StringElement{
Content: "This is the second line of the first paragraph.",
},
},
},
},
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "This is the first line of the continuation paragraph.",
},
},
[]interface{}{
types.StringElement{
Content: "This is the second line of the continuation paragraph.",
},
},
},
},
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "This is the next continuation paragraph.",
},
},
},
},
types.Paragraph{
Attributes: types.Attributes{
types.AttrAdmonitionKind: types.Tip,
},
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "We can embed admonitions too!",
},
},
},
},
},
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
})

})
})

0 comments on commit 6f07b44

Please sign in to comment.