require 'rails_helper' require 'html_to_markdown' describe HtmlToMarkdown do def html_to_markdown(html, opts = {}) HtmlToMarkdown.new(html, opts).to_markdown end it "remove whitespaces" do expect(html_to_markdown(<<-HTML
Hello
})).to eq("Hello World!") end it "converts " do expect(html_to_markdown("Strong")).to eq("**Strong**") expect(html_to_markdown("Str*ng")).to eq("__Str*ng__") end it "converts " do expect(html_to_markdown("Bold")).to eq("**Bold**") expect(html_to_markdown("B*ld")).to eq("__B*ld__") end it "converts " do expect(html_to_markdown("Emphasis")).to eq("*Emphasis*") expect(html_to_markdown("Emph*sis")).to eq("_Emph*sis_") end it "converts " do expect(html_to_markdown("Italic")).to eq("*Italic*") expect(html_to_markdown("It*lic")).to eq("_It*lic_") end it "converts " do expect(html_to_markdown(%Q{Discourse})).to eq("[Discourse](https://www.discourse.org)") end it "removes empty & invalid " do expect(html_to_markdown(%Q{Discourse})).to eq("Discourse") expect(html_to_markdown(%Q{Discourse})).to eq("Discourse") expect(html_to_markdown(%Q{Discourse})).to eq("Discourse") end HTML_WITH_IMG ||= %Q{} HTML_WITH_CID_IMG ||= %Q{} it "converts " do expect(html_to_markdown(HTML_WITH_IMG)).to eq("![Discourse Logo](https://www.discourse.org/logo.svg)") end it "keeps with 'keep_img_tags'" do expect(html_to_markdown(HTML_WITH_IMG, keep_img_tags: true)).to eq(HTML_WITH_IMG) end it "removes empty & invalid " do expect(html_to_markdown(%Q{})).to eq("") expect(html_to_markdown(%Q{})).to eq("") expect(html_to_markdown(%Q{})).to eq("") end it "keeps with src='cid:' whith 'keep_cid_imgs'" do expect(html_to_markdown(HTML_WITH_CID_IMG, keep_cid_imgs: true)).to eq("![Discourse Logo](cid:ii_1525434659ddb4cb)") expect(html_to_markdown(HTML_WITH_CID_IMG, keep_img_tags: true, keep_cid_imgs: true)).to eq("") end it "skips hidden " do expect(html_to_markdown(%Q{})).to eq("") expect(html_to_markdown(%Q{})).to eq("") expect(html_to_markdown(%Q{})).to eq("") expect(html_to_markdown(%Q{})).to eq("") end (1..6).each do |n| it "converts World!" do
expect(html_to_markdown("Code
")).to eq("`Code`")
end
it "supports " do
expect(html_to_markdown("This is an insertion")).to eq("This is an insertion")
end
it "supports " do
expect(html_to_markdown("This is a deletion")).to eq("This is a deletion")
end
it "supports " do
expect(html_to_markdown("H2O")).to eq("H2O")
end
it "supports " do
expect(html_to_markdown("Super Script!")).to eq("Super Script!")
end
it "supports " do
expect(html_to_markdown("Small")).to eq("Small")
end
it "supports " do
expect(html_to_markdown("CTRL+C")).to eq("CTRL+C")
end
it "supports " do
expect(html_to_markdown(%Q{CDCK})).to eq(%Q{CDCK})
end
it "supports " do
expect(html_to_markdown("Strike Through")).to eq("Strike Through")
end
it "supports " do
expect(html_to_markdown("Strike Through")).to eq("Strike Through")
end
it "supports " do
expect(html_to_markdown("Quote
")).to eq("> Quote")
end
it "supports " do
expect(html_to_markdown("- 🍏
- 🍐
- 🍌
")).to eq("- 🍏\n- 🍐\n- 🍌")
expect(html_to_markdown("\n- 🍏
\n- 🍐
\n- 🍌
\n
")).to eq("- 🍏\n- 🍐\n- 🍌")
end
it "supports " do
expect(html_to_markdown("- 🍆
- 🍅
- 🍄
")).to eq("1. 🍆\n1. 🍅\n1. 🍄")
end
it "supports inside
- " do
expect(html_to_markdown("
🍏
🍐
🍌
")).to eq("- 🍏\n\n- 🍐\n\n- 🍌")
end
it "supports inside " do
expect(html_to_markdown(<<-HTML
- Fruits
- 🍏
- 🍐
- 🍌
- Vegetables
- 🍆
- 🍅
- 🍄
HTML
)).to eq("- Fruits\n - 🍏\n - 🍐\n - 🍌\n- Vegetables\n - 🍆\n - 🍅\n - 🍄")
end
it "supports bare - " do
expect(html_to_markdown("
- I'm alone
")).to eq("- I'm alone")
end
it "supports
" do
expect(html_to_markdown("var foo = 'bar';
")).to eq("```\nvar foo = 'bar';\n```")
expect(html_to_markdown("var foo = 'bar';
")).to eq("```\nvar foo = 'bar';\n```")
expect(html_to_markdown(%Q{var foo = 'bar';
})).to eq("```javascript\nvar foo = 'bar';\n```")
end
it "supports inside " do
expect(html_to_markdown("var foo = 'bar';
")).to eq("> ```\n> var foo = 'bar';\n> ```")
end
it "works" do
expect(html_to_markdown("A list item with a blockquote:
This is a blockquote
inside a list item.
")).to eq("- A list item with a blockquote:\n\n > This is a **blockquote**\n > inside a list item.")
end
it "supports html document" do
expect(html_to_markdown("HelloWorld")).to eq("Hello\nWorld")
end
it "handles " do
expect(html_to_markdown("
1st paragraph
2nd paragraph
")).to eq("1st paragraph\n\n2nd paragraph")
end
it "handles " do
expect(html_to_markdown("1st div2nd div")).to eq("1st div\n\n2nd div")
end
it "swallows " do
expect(html_to_markdown("Span")).to eq("Span")
end
it "swallows " do
expect(html_to_markdown("Underline")).to eq("Underline")
end
it "removes ")).to eq("")
end
it "removes ")).to eq("")
end
it "handles divs within spans" do
html = "1st paragraph2nd paragraph"
expect(html_to_markdown(html)).to eq("1st paragraph\n2nd paragraph")
end
context "with an oddly placed
" do
it "handles " do
expect(html_to_markdown("
Bold")).to eq("**Bold**")
expect(html_to_markdown("Bold
")).to eq("**Bold**")
expect(html_to_markdown("Bold
text")).to eq("**Bold\ntext**")
end
it "handles " do
expect(html_to_markdown("
Italic")).to eq("*Italic*")
expect(html_to_markdown("Italic
")).to eq("*Italic*")
expect(html_to_markdown("Italic
text")).to eq("*Italic\ntext*")
end
end
context "with an empty tag" do
it "handles " do
expect(html_to_markdown("")).to eq("")
expect(html_to_markdown(" ")).to eq("")
expect(html_to_markdown("Some text")).to eq("Some text")
expect(html_to_markdown("Some text")).to eq("Some text")
end
it "handles " do
expect(html_to_markdown("")).to eq("")
expect(html_to_markdown(" ")).to eq("")
expect(html_to_markdown("Some text")).to eq("Some text")
expect(html_to_markdown("Some text")).to eq("Some text")
end
end
context "with spaces around text" do
it "handles " do
expect(html_to_markdown(" Bold")).to eq("**Bold**")
expect(html_to_markdown(" Bold")).to eq("**Bold**")
expect(html_to_markdown("Bold ")).to eq("**Bold**")
expect(html_to_markdown("Bold ")).to eq("**Bold**")
expect(html_to_markdown("Some bold text")).to eq("Some **bold** text")
expect(html_to_markdown("Some bold text")).to eq("Some **bold** text")
expect(html_to_markdown("Some bold text")).to eq("Some **bold** text")
expect(html_to_markdown("Some bold text")).to eq("Some **bold** text")
end
it "handles " do
expect(html_to_markdown(" Italic")).to eq("*Italic*")
expect(html_to_markdown(" Italic")).to eq("*Italic*")
expect(html_to_markdown("Italic ")).to eq("*Italic*")
expect(html_to_markdown("Italic ")).to eq("*Italic*")
expect(html_to_markdown("Some italic text")).to eq("Some *italic* text")
expect(html_to_markdown("Some italic text")).to eq("Some *italic* text")
expect(html_to_markdown("Some italic text")).to eq("Some *italic* text")
expect(html_to_markdown("Some italic text")).to eq("Some *italic* text")
end
end
end