Skip to content

Commit

Permalink
Fixes unintentional newline characters within lists with paragraphs
Browse files Browse the repository at this point in the history
This commit aims to fix a problem when parsing markup similar to:

```ruby
[8] pry(main)> ReverseMarkdown.convert("<ul><li> <p>a</p></li></ul>")
=> "-  \n\na\n\n"
```

Note that the list item has newlines between the markdown list item and
the actual text.

If you remove the space between the list item tag and the paragraph tag:

```ruby
[7] pry(main)> ReverseMarkdown.convert("<ul><li><p>a</p></li></ul>")
=> "- a\n\n"
```

Reverse markdown appears to assume what believe to be the intended
behaviour. There was already a test in place to account for this situation,
but it was `xit`'ed.

The proposed patch is to ask Nokogiri to find the first child element of
the list, instead of the first child (which might include stuff like a
newline).
  • Loading branch information
diogoosorio committed Oct 8, 2020
1 parent c664cad commit 3732a3b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/reverse_markdown/converters/li.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module ReverseMarkdown
module Converters
class Li < Base
def convert(node, state = {})
contains_child_paragraph = node.children.first ? node.children.first.name == 'p' : false
content_node = contains_child_paragraph ? node.children.first : node
contains_child_paragraph = node.first_element_child ? node.first_element_child.name == 'p' : false
content_node = contains_child_paragraph ? node.first_element_child : node
content = treat_children(content_node, state)
indentation = indentation_from(state)
prefix = prefix_for(node)
Expand Down
2 changes: 2 additions & 0 deletions spec/assets/lists.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
</li>
</ul>

<ul><li> <p>I don't want to cleanup after the party!</p> </li></ul>

<ul>
<li>
<p>li 1, p 1</p>
Expand Down
3 changes: 2 additions & 1 deletion spec/components/lists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
end

context "lists containing embedded <p> tags" do
xit { is_expected.to match /\n- I want to have a party at my house!\n/ }
it { is_expected.to match /\n- I want to have a party at my house!\n/ }
it { is_expected.to match /\n- I don't want to cleanup after the party!\n/ }
end

context "list item containing multiple <p> tags" do
Expand Down

0 comments on commit 3732a3b

Please sign in to comment.