FIX: generate valid markdown from <br></b> in an email (#5022)

* FIX: generate valid markdown from <br></b> in an email

* FIX: don't generate markdown for empty <strong> or <em> tags in emails
This commit is contained in:
Leo McArdle 2017-08-02 22:02:59 +01:00 committed by Régis Hanol
parent 902be91a5a
commit 65d5cd7239
2 changed files with 32 additions and 0 deletions

View File

@ -186,18 +186,22 @@ class HtmlToMarkdown
end end
def visit_strong(node) def visit_strong(node)
return if node.text.blank?
delimiter = node.text["*"] ? "__" : "**" delimiter = node.text["*"] ? "__" : "**"
@stack[-1].markdown << delimiter @stack[-1].markdown << delimiter
traverse(node) traverse(node)
@stack[-1].markdown.chomp!
@stack[-1].markdown << delimiter @stack[-1].markdown << delimiter
end end
alias :visit_b :visit_strong alias :visit_b :visit_strong
def visit_em(node) def visit_em(node)
return if node.text.blank?
delimiter = node.text["*"] ? "_" : "*" delimiter = node.text["*"] ? "_" : "*"
@stack[-1].markdown << delimiter @stack[-1].markdown << delimiter
traverse(node) traverse(node)
@stack[-1].markdown.chomp!
@stack[-1].markdown << delimiter @stack[-1].markdown << delimiter
end end

View File

@ -228,4 +228,32 @@ describe HtmlToMarkdown do
expect(html_to_markdown(html)).to eq("1st paragraph\n2nd paragraph") expect(html_to_markdown(html)).to eq("1st paragraph\n2nd paragraph")
end end
context "with an oddly placed <br>" do
it "handles <strong>" do
expect(html_to_markdown("<strong>Bold<br></strong>")).to eq("**Bold**")
expect(html_to_markdown("<strong>Bold<br>text</strong>")).to eq("**Bold\ntext**")
end
it "handles <em>" do
expect(html_to_markdown("<em>Italic<br></em>")).to eq("*Italic*")
expect(html_to_markdown("<em>Italic<br>text</em>")).to eq("*Italic\ntext*")
end
end
context "with an empty tag" do
it "handles <strong>" do
expect(html_to_markdown("<strong></strong>")).to eq("")
expect(html_to_markdown("<strong> </strong>")).to eq("")
end
it "handles <em>" do
expect(html_to_markdown("<em></em>")).to eq("")
expect(html_to_markdown("<em> </em>")).to eq("")
end
end
end end