FIX: Weird mixture of line breaks resulted in wrong email trimming

FIX: Weird mixture of line breaks resulted in wrong email trimming
diff --git a/lib/html_to_markdown.rb b/lib/html_to_markdown.rb
index a213611..86ab03d 100644
--- a/lib/html_to_markdown.rb
+++ b/lib/html_to_markdown.rb
@@ -32,6 +32,7 @@ class HtmlToMarkdown
         node.content = node.content.gsub(/\A[[:space:]]+/, "") if node.previous_element.nil? && node.parent.description&.block?
         node.content = node.content.gsub(/[[:space:]]+\z/, "") if node.next_element&.description&.block?
         node.content = node.content.gsub(/[[:space:]]+\z/, "") if node.next_element.nil? && node.parent.description&.block?
+        node.content = node.content.gsub(/\r\n?/, "\n")
         node.remove if node.content.empty?
       end
     end
@@ -200,7 +201,7 @@ class HtmlToMarkdown
         @stack[-1].markdown << " " if node.text[0] == " "
         @stack[-1].markdown << delimiter
         traverse(node)
-        @stack[-1].markdown.chomp!
+        @stack[-1].markdown.gsub!(/\n+$/, "")
         if @stack[-1].markdown[-1] == " "
           @stack[-1].markdown.chomp!(" ")
           append_space = true
diff --git a/spec/components/html_to_markdown_spec.rb b/spec/components/html_to_markdown_spec.rb
index 486baf4..1aef741 100644
--- a/spec/components/html_to_markdown_spec.rb
+++ b/spec/components/html_to_markdown_spec.rb
@@ -33,6 +33,15 @@ describe HtmlToMarkdown do
   it "converts <b>" do
     expect(html_to_markdown("<b>Bold</b>")).to eq("**Bold**")
     expect(html_to_markdown("<b>B*ld</b>")).to eq("__B*ld__")
+
+    html = <<~HTML
+      <p><b>Bold
+      <br>
+      <br>
+      </b>
+      </p>
+    HTML
+    expect(html_to_markdown(html)).to eq("**Bold**")
   end
 
   it "converts <em>" do

GitHub
sha: 37461a63

1 Like