From 37461a6398bf3251398bd0a17fcc0ed2abc52807 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Mon, 17 Dec 2018 18:38:11 +0100 Subject: [PATCH] FIX: Weird mixture of line breaks resulted in wrong email trimming --- lib/html_to_markdown.rb | 3 ++- spec/components/html_to_markdown_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/html_to_markdown.rb b/lib/html_to_markdown.rb index a213611ea3f..86ab03dda16 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 486baf40a19..1aef7411208 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 " do expect(html_to_markdown("Bold")).to eq("**Bold**") expect(html_to_markdown("B*ld")).to eq("__B*ld__") + + html = <<~HTML +

Bold +
+
+
+

+ HTML + expect(html_to_markdown(html)).to eq("**Bold**") end it "converts " do